POSIX Functions in PHP
If you’re a PHP developer, chances are you know that the POSIX library is a powerful tool for interacting with the underlying operating system at a lower level and controlling processes and signal handling. However, many people don’t realize the extent to which POSIX offers functionality that allows for even more sophisticated control of the operating system, including retrieving information about the users of your process and manipulating the standard Unix signal model.
The posix_getpid() function allows the PHP programmer to retrieve the process identifier of the current program. This function accepts no parameters and returns the identifier as an int value.
In addition to retrieving the process identifier, the posix_getpid() function also supports retrieval of the current user’s group information. This function is identical to its sister function posix_getpwuid(), except that it uses the lookup method to return the information rather than the comparison.
Another POSIX function that supports retrieval of the process’s group information is posix_getgroups(). This function also accepts no parameters and returns an indexed array of the groups that the process belongs to.
Lastly, the pcntl_waitpid() function allows PHP to wait for child processes to exit, if the parent process’s status has changed. This function takes a single argument, the process group identifier of the current process, and returns the process identifier of any children that have not yet exited.
It’s worth noting that if you’re using the pcntl_waitpid() functions in your code, you will need to be running your script as a root user in order to take advantage of some of the functionality offered by the POSIX extension. In particular, the ability to change the effective or real user ID of the PHP process is a privileged function that requires PHP to be running as a root user.