Some Unix Process Functions


This document describes the following functions available in Unix for handling processes:


fork


All processes in Unix start their life via the fork system call. The fork function creates a copy of the calling process. The only distinction between the 2 processes is that the value returned to one of them (referred to as the parent process) will be the process ID of the copy. The copy is usually referred to as the child process. The value returned to the child process will be zero.

So you can think of fork as a function call that has 2 return values. The child gets a value of 0 and the parent gets the process ID (PID) of the child. Both the parent and the child process continue execution from the line of code immediately following the call to fork. So the child starts life in the middle of a program!

Here is the prototype:

#include <unistd.h>

int fork( void );
fork returns one of three values:
  1. -1 if the call itself fails
  2. 0 if the return is to the child process
  3. > 0 if the return is to the parent process and the value is the PID of the child




 
getpid


The getpid function simply returns the process ID (PID) of the caller. The prototype is:
#include <unistd.h>

pid_t getpid( void );
The type pid_t is simply an integer.



 
getppid


The getppid function returns the process ID of the caller's parent process. The prototype is:
#include <unistd.h>

pid_t getppid( void );

execl (and many variations)


The exec family of functions overlay a new process image on an old process. The new process image is constructed from an ordinary, executable file. This file is either an executable object file, or a file of data for an interpreter. There can be no return from a successful exec because the calling process image is overlaid by the new process image.

Unlike fork, after the call to an exec function, the new process does not execute from the line after the call to exec, but starts with the first executable instruction of the new program. All programs in Unix are created via a combination of calls to fork and exec.

A call to exec should not return since the process that called it is replaced with another process. However, if something goes wrong and the new process can't be started, then exec returns -1.

See the man pages for the details on the variations of this call and on the specific parameters required.



 
wait and waitpid


The wait function suspends execution of the current process until a child has exited. If a child has already exited by the time of the call (a so-called "zombie" process), the function returns immediately. Any system resources used by the child are freed.

The wait function allows a pointer to an integer to be passed. If this pointer is not NULL, then it will receive the exit status of the child process. If it is NULL, then the exit status is ignored. wait returns the process ID of the process that exited or -1 on error or if the parent calls wait more times than it has children.

The waitpid function behaves similarly but allows for more options. See the man page for all of the details. The prototypes are

#include <sys/wait.h>

pid_t wait( int* status );
pid_t waitpid( pid_t pid, int *stat_loc, int options );

kill


The kill function allows one process to send a signal to another process. The prototype is:
#include <signal.h>

int kill( pid_t pid, int sig );
The first parameter specifies the process ID of the process to which to send the signal. The second parameter specifies the number of the signal to send. Despite the name of the function, kill is not used only to terminate other processes. Signals can be used for interprocess communication (though they are not very reliable, so I wouldn't recommend it).

kill returns 0 on success and -1 on error.