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

Parent and Child PID

The following snapshot (Fig 5 above) displays the output of this program.
As mentioned earlier, Linux operates on the principle of time slices. Usually, after the execution of fork() system call, the time slice is given to the child process. Because of the switching, let us assume that the parent gets the time slice before the child completes its execution. Consider a situation where the parent process terminates before the child process. Then what will happen to the child process? We will have a fatherless child and we call such process as orphan process. Linux provides a system call called sleep() and it is used to introduce a specific amount of delay in a process (see Fig 6, 7 below).

Man page details of sleep system callUsage details of sleep

Let us explain this concept with an example:

int pid;
pid = fork();
if (pid == 0)
printf(“Child process and pid is %dn”, getpid());
printf(“ Child’s parent process id is %dn”,getppid());
printf(“Child process and pid is %dn”,getpid());
printf(“Child’s parent process id is %dn”,getppid());
printf(“Parent process and pid is %dn”,getpid());
printf(“Parent’s parent process id is %dn”,getppid());