Monday, May 16, 2011

System calls - fork() , exec() and wait()


The fork system call does not take an argument.The process that invokes the fork() is known as the parent and the new process is called the child.If the fork system call fails, it will return a -1.If the fork system call is successful, the process ID of the child process is returned in the parent process and a 0 is returned in the child process.When a fork() system call is made, the operating system generates a copy of the parent process which becomes the child process.The operating system will pass to the child process most of the parent's process information.However, some information is unique to the child process:
    • The child has its own process ID (PID)
    • The child will have a different PPID than its parent
    • System imposed process limits are reset to zero
    • All recorded locks on files are reset
    • The action to be taken when receiving signals is different

If the fork system call is successful a child process is produced that continues execution at the point where it was called by the parent process.After the fork system call, both the parent and child processes are running and continue their execution at the next statement in the parent process.A summary of fork() return values follows:

fork_return > 0: this is the parent
fork_return == 0: this is the child
fork_return == -1: fork() failed and there is no child.

The function call sleep will result in a process "sleeping" a specified number of seconds. It can be used to prevent the process from running to completion within one time slice.One process will always end before the other. If there is enough intervening time before the second process ends, the system call will redisplay the prompt, producing the last line of output where the output from the child process is appended to the end of the prompt (ie. %child)


A parent process usually needs to synchronize its actions by waiting until the child process has either stopped or terminated its actions.The wait() system call allows the parent process to suspend its activities until one of these actions has occurred.The wait() system call accepts a single argument, which is a pointer to an integer and returns a value defined as type pid_t.If the calling process does not have any child associated with it, wait will return immediately with a value of -1.If any child processes are still active, the calling process will suspend its activity until a child process terminates.


"The exec family of functions replaces the current process image with a new process image." (man pages).Commonly a process generates a child process because it would like to transform the child process by changing the program code the child process is executing.The text, data and stack segment of the process are replaced and only the u (user) area of the process remains the same.If successful, the exec system calls do not return to the invoking program as the calling image is lost.It is possible for a user at the command line to issue an exec system call, but it takes over the current shell and terminates the shell.

The versions of exec are:
  • execl
  • execv
  • execle
  • execve
  • execlp
  • execvp
The naming convention: exec*
  • 'l' indicates a list arrangement (a series of null terminated arguments)
  • 'v' indicate the array or vector arrangement (like the argv structure).
  • 'e' indicates the programmer will construct (in the array/vector format) and pass their own environment variable list
  • 'p' indicates the current PATH string should be used when the system searches for executable files.
  • NOTE:
  • In the four system calls where the PATH string is not used (execl, execv, execle, and execve) the path to the program to be executed must be fully specified.
exec system call functionality
Library Call NameArgument ListPass Current Environment VariablesSearch PATH automatic?


  • this system call is used when the number of arguments to be passed to the program to be executed is known in advance
  • this system call is used when the numbers of arguments for the program to be executed is dynamic

Things to remember about exec*:
  • this system call simply replaces the current process with a new program -- the pid does not change
  • the exec() is issued by the calling process and what is exec'ed is referred to as the new program -- not the new process since no new process is created
  • it is important to realize that control is not passed back to the calling process unless an error occurred with the exec() call
  • in the case of an error, the exec() returns a value back to the calling process
  • if no error occurs, the calling process is lost

No comments:

Post a Comment