XV6 Describe the life of getpid globl getpid getpid movl 11
XV6
Describe the life of getpid()
.globl getpid;
getpid:
movl $11, %eax;
int $64;
ret
where the return value is coming from for the getpid, so I can print the ppid in the following code:
void
mem(void)
int ppid;
ppid=getpid();
printf(1,\"mem=%d\ \",ppid);
}
int
main (int argc, char *argv [])
{
printf (1, \"mem starting \ \");
mem ();
exit ();
}
Solution
getpid() returns the process id of calling process. Every process is managed by a kernel, which means the ketnel is aware of running, scheduled or invoked process. It stores process information in kernel address space in data structures (task struct). So a process calling getpid() means the kernel has to look at the process\'s task struct to get that information. The kernel is always running. For a process invoked in userspace, the kernel is running and scheduling in that process\'s context. Hence it knows the pid to answer for this call. Also a call to getpid() is always successsul, because there will always be a process that called it or else it would not be invoked!
