01-08-2012, 11:02 AM
PROCESS CONTROL
PROCESS_CONTROL.pptx (Size: 756.56 KB / Downloads: 28)
Process Identifiers:
Every process has a unique process ID, a non-negative integer.
Although unique, process IDs are reused.
As processes terminate, their IDs become candidates for reuse.
Process ID 0 is usually the scheduler process and is often known as the swapper.
No program on disk corresponds to this process, which is part of the kernel and is known as a system process.
Process ID 1 is usually the init process and is invoked by the kernel at the end of the bootstrap procedure.
The init process never dies. It is a normal user process, not a system process within the kernel, like the swapper, although it does run with superuser privileges.
In addition to the process ID, there are other identifiers for every process.
The fork Function:
An existing process can create a new one by calling the fork function.
The new process created by fork is called the child process.
This function is called once but returns twice.
The only difference in the returns is that the return value in the child is 0, whereas the return value in the parent is the process ID of the new child.
Both the child and the parent continue executing with the instruction that follows the call to fork.
The child is a copy of the parent. For example, the child gets a copy of the parent's data space, heap, and stack.
The parent and the child share the text segment, but do not share data space, heap, & stack portions of memory.
File Sharing:
One characteristic of fork is that all file descriptors that are open in the parent are duplicated in the child.
The parent and the child share a file table entry for every open descriptor.
Consider a process that has three different files opened for standard input, standard output, and standard error.
On return from fork, the arrangement is as shown in figure.
It is important that the parent and the child share the same file offset.
Consider a process that forks a child, then waits for the child to complete.
Assume that both processes write to standard output as part of their normal processing.
The vfork Function:
The function vfork has the same calling sequence and same return values as fork.
But the semantics of the two functions differ.
The vfork function creates the new process, just like fork, without copying the address space of the parent into the child.
The child simply calls exec (or exit) right after the vfork.
While the child is running and until it calls either exec or exit, the child runs in the address space of the parent.
Another difference between the two functions is that vfork guarantees that the child runs first, until the child calls exec or exit.
Terminating Processes:
What happens if the parent terminates before the child?
The answer is that the init process becomes the parent process of any process whose parent terminates.
It is termed as the process has been inherited by init.
What normally happens is that whenever a process terminates, the kernel goes through all active processes to see whether the terminating process is the parent of any process that still exists.
If so, the parent process ID of the surviving process is changed to be 1 (the process ID of init).
This way, it is guaranteed that every process has a parent.
The exec Functions:
One use of the fork function is to create a new process (the child) that then causes another program to be executed by calling one of the exec functions.
When a process calls one of the exec functions, that process is completely replaced by the new program, and the new program starts executing at its main function.
The process ID does not change across an exec, because a new process is not created; exec merely replaces the current process—its text, data, heap, and stack segments—with a brand new program from disk.
There are six different exec functions available on UNIX platform.
With fork, one can create new processes; and with the exec functions, one can initiate new programs.
The exit function and the wait functions handle termination and waiting for termination.
These are the only process control primitives needed.