News

Process synchronisation in Linux

The two fundamental concepts in a Linux operating system are processes and time slice. A process is an instance of a program that is being executed by the computer’s operating system. When we say that two computers are communicating with each other, we mean that two processes, one running on

The various statuses for a process are:

1. Running (R)
2. Orphan (O)
3. Sleeping (S)
4. Zombie (Z)

Output of ps -el command

The snapshot (Fig 11 above) displays the output of the ‘ps –el’ command.
As mentioned earlier, synchronisation between processes is very important. And there is always a need to prevent a process becoming an orphan or a zombie. Linux provides an important system call – wait() (Fig 12 below)– that will help in synchronising the processes. This system call will also ensure that a process will not become an orphan or a zombie.
Man page details of wait system call

Whenever we make a call to the wait() function, it results in a number of things happening. A check is first made to see if the parent process has any children. If it does not, then a -1 value is returned by wait(). If the parent process has a child that has terminated (a zombie), that child’s PID is returned and its entry is removed from the process table. However, if the parent process has a child or children that have not terminated, it (the parent process) is suspended till it receives a signal. This signal is received as soon as the child dies.

Let us consider an example to understand the concept of process synchronisation:

#include<stdio.h>
main()
{
int i=0;
int pid;
pid = fork();
if (pid == 0)
{
printf(“Child startsn”);
for(i=0;i<1000;i++)
printf(“%dt”,i);
printf(“Child endsn”);
}
else
{
wait(0);
for(i=0;i<1000;i++)
printf(”%dt”,i);
printf(”Parent endsn”);
}
}

×