I'm in the middle of taking my first OS class, so hopefully I don't have any big misconceptions here.
I was wondering why getpid() is implemented as a system call in Linux. As I understand it, certain functions are made into system calls because they access or change information that the OS might want to protect, so they are implemented as a system call in order to transfer control to the kernel.
But as I understand it, getpid() is just returning the process id of the calling process. Is there any case where permission for this information wouldn't be granted? Wouldn't it be safe to simply let getpid() be a normal user function?
Thanks for the help.
The only way to implement getpid() without a system call, is doing one system call first and caching its result. Then every call to getpid() will return that caching value without a system call.
However, the Linux man-pages project explains why getpid() is not cached:
From glibc version 2.3.4 up to and including version 2.24, the glibc wrapper function for getpid() cached PIDs, with the goal of avoiding additional system calls when a process calls getpid() repeatedly. Normally this caching was invisible, but its correct operation relied on support in the wrapper functions for fork(2), vfork(2), and clone(2): if an application bypassed the glibc wrappers for these system calls by using syscall(2), then a call to getpid() in the child would return the wrong value (to be precise: it would return the PID of the parent process). In addition, there were cases where getpid() could return the wrong value even when invoking clone(2) via the glibc wrapper function. (For a discussion of one such case, see BUGS in clone(2).) Furthermore, the complexity of the caching code had been the source of a few bugs within glibc over the years. Because of the aforementioned problems, since glibc version 2.25, the PID cache is removed: calls to getpid() always invoke the actual system call, rather than returning a cached value.
In summary, if getpid() were cached it may return wrong values (even if the cache were done perfectly without allowing any program to write, etc...) and it was a source of bugs in the past.
Normally you only need a single getpid() call in any process, if you are using the result more than once, save it in a variable (application-level caching!).
Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With