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.
Getpid() might be just READING from a location but someone has to WRITE to that location. In order to provide any old process from writing garbage to the location used by the operating system, it need to be protected from user mode access. In order for an application to access that location, it needs to do so in kernel mode. Therefore, it has to be done in as a system call.