Inter process communication

Friday, May 26, 2006

Process Synchronization

main(){
int i=0; pid;
pid=fork();

if(0 == pid){
printf(“Child Starts\n”);
for(i=0;i<1000;i++){
printf(“%d \t”, i);
}
printf(“Child Ends \n”);
}
else{
wait(0);
printf(“Parent\n”);
}
}


The call to the wait() function results in the following things:
1. A check is made to see if the parent process has any children. If it does not, a -1 is returned by wait().
2. If the parent process has a child that has terminated, its PID is returned to the parent process & the child process is removed from the PROCESS TABLE.
3. If the parent process has any children that have not yet terminated (a Zombie), the parent process is suspended till it receives a signal. The signal is received as soon as the child dies.

Thus in the above code, the parent process is suspended till the child process is terminated. After this, a signal is sent to the parent process which before resuming, checks for any zombie children in the process table & removes it.

To wait for all children to terminate, a wait(0) for each child should be added in the parents code.

Zombies

Processes that are dead but haven’t been removed from the Process Table are called Zombies.

If a process creates a child & the child terminates before the parent process, the child process cannot be removed from the Process Table till the parent process terminates. The child thus becomes a Zombie.

main(){
if ( fork() > 0){
printf(“Parent\n”);
sleep(50);
}
}

In the PROCESS TABLE (ps –el), Zombie process’ are represented by a Z

Orphan Process

If the parent process terminates before the child process, the child process is called an Orphan.

The process dispatcher immediately adopts an orphan. Thus the PID in an orphan process would be of the process dispatcher & not of the process that created it.

In the PROCESS TABLE (ps –el), orphan process’ are represented by an O.

Fork

User processes can in turn create children (for multitasking, perhaps) using the fork() function.


fork() creates a child that is duplicate of the parent process. All statements after the fork() function call are executed both in the parent as well as the child process. Any statements before the fork() would be executed only once.


In the following program, To the parent process fork() returns the PID of the child process or -1 in case of errors. In the child process the value of PID will always be 0.

main(){

int pid;
printf(“Before\n”);
pid = fork();
printf(“After\n”);
}

Output:

Before
After
After

main(){
fork();
fork();
printf(“Hello\n”);
}

Output:

Hello
Hello
Hello
Hello

Simply put, for n calls of fork(), there would be 2^n process’.

Note: Normally after a fork(), the time slice is given to the child process.

Sunday, May 21, 2006

PID – Process ID

Each process in unix has a parent process. When a system is booted, a special process called the swapper or scheduler is created with PID = 0.

Swapper creates three children using the etc/init file: 1. Process Dispatcher 2. vhand & 3. bdflush (1,2,3 being the process id respectively).

Process dispatcher in turn creates shell. All user processes are children of the shell.

Thus:
Swapper/scheduler
  • Process Dispatcher

    • Shell

      • All user processes

  • vhand

  • bdflush

getpid() – Gets process id
getppid() – Gets parent process id