3.7 Terminating a Process

When a process is terminated, the PCB is erased and the address space and resources used by the terminated process are deallocated. An exit code is placed in its entry in the main process table. The entry is removed once the parent has accepted the exit code. The termination of the process can occur under several conditions:

  • All instructions have executed.

  • The process makes an explicit return or makes a system call that terminates the process.

  • Child processes may automatically terminate when the parent has terminated.

  • The parent sends a signal to terminate its child processes.

Abnormal termination of a process can occur when the process itself does something that it shouldn't:

  • The process requires more memory than the system can provide.

  • The process attempts to access resources it is not allowed to access.

  • The process attempts to perform an invalid instruction or a prohibited computation.

The termination of a process can also be initiated by a user when the process is interactive.

The parent process is responsible for the termination/deallocation of its children. The parent process should wait until all its child processes have terminated. When a parent process retrieves a child process's exit code, the child process exits the system normally. The process is in a zombied state until the parent accepts the signal. If the parent never accepts the signal because it has already terminated and exited the system or because it is not waiting for the child process, the child remains in the zombied state until the init process (the original system process) accepts its exit code. Many zombied processes can negatively affect the performance of the system.

3.7.1 The exit() , kill() and abort() Calls

There are two functions a process can call for self termination, exit() and abort() . The exit() function causes a normal termination of the calling process. All open file descriptors associated with the process will be closed. The function will flush all open streams that contain unwritten buffered data, then the open streams are closed. The status parameter is the process's exit status. It is returned to the waiting parent process, which is then restarted. The value of status may be , EXIT_FAILURE , or EXIT_SUCCESS . The value means the process has terminated successfully. The waiting parent process only has access to the lower 8 bits of status . If the parent process is not waiting for the process to terminate, the zombied process is adopted by the init process.

The abort() function causes an abnormal termination of the calling process. An abnormal termination of the process causes the same effect as fclose() on all open streams. A waiting parent process will receive a signal that the child process aborted. A process should only abort when it encounters an error that it cannot deal with programmatically.

Synopsis

 #include <stdlib.h> void exit(int status); void abort(void); 

The kill() function can be used to cause the termination of another process. The kill() function sends a signal to the processes specified or indicated by the parameter pid . The parameter sig is the signal to be sent to the specified process. The signals are listed in the header <signal.h> . To kill a process, sig has the value SIGKILL . The calling process must have the appropriate privileges to send a signal to the process, or it has a real or effective user id that matches the real or saved set user-ID of the process that receives the signal. The calling process may have permission to send only certain signals to processes and not others. If the function successfully sends the signal, is returned to the calling process. If it fails, -1 is returned.

The calling process can send the signal to one or several processes under these conditions:

pid > 0

The signal will be sent to the process whose PID is equal to the pid .

pid = 0

The signal will be sent to all the processes whose process group id is the same as the calling process.

pid = -1

The signal will be sent to all processes for which the calling process has permission to send that signal.

pid < -1

The signal will be sent to all processes whose process id group is equal to the absolute value of pid and for which the calling process has permission to send that signal.

Synopsis

 #include <signal.h> int kill(pid_t pid, int sig); 



Parallel and Distributed Programming Using C++
Parallel and Distributed Programming Using C++
ISBN: 0131013769
EAN: 2147483647
Year: 2002
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net