Inter Communication

Hello Friends in this article I will be explaining about IPC using C language and Linux as platform. Let first start from the functions we will be using in this articles

1 getpid(); 2 getppid(); 3 setpid(); 4 fork(); 5 exec() functions

Let see a very simple program

Main() { int pid; pid=getpid(); printf(—process id is %d\n“, pid); }

Now we will execute this program using gcc œo pid.out pid.c . Now execute this program using ./pid.out& . We will get a unique number which identifies the process ID. Every process in UNIX has a different process ID assigned to them. When any UNIX system is booted, a process with a PID of 0 is created. It manages memory allocation and CPU allocation issues. This process then creates three child processes with PID 1, 2, 3. This all is done by executing a file Init. The process with PID 1 is called process manager or dispatcher. This dispatcher then arises shell and therefore any new processes that are executed by shell are children‘s of Dispatcher.

UNIX keeps information about processes in Process Table.

Main() { int pid; pid=getppid(); printf(—process id is %d\n“, pid); }

This program will return the process ID of parent process

Lets now move to fork(); function

We can also make ours processes to create their own Childs. These children processes can be created using fork();. Lets see a very simple program using fork()

Main() { fork(); printf(—Hello India\n“); }

Now the output of above program will be :

Hello India Hello India

Yes I have written Hello India two times, now why this happen. The fork here creates a that is duplicate of parent process. Now this put two processes in memory and both printed Hello India.

Now the child process actually starts from fork(); statement and every line after a fork will be executed twice once by parent process and once by child process, if the Hello India line would have been before then it would have been executed only once by parent process.

Now let‘s see a little bit complex example

Main() { fork(); fork(); printf(—hello“); }

Now we have 3 forks(); so how many times will hello be printed. Of course it will be equal to number of processes that are created. If yours answer is 4 then you are right. Let‘s see how

See 1st call to fork will create one child process, now we will have in memory one child process created by 1st fork and one parent process which will start from 2nd fork. Both of them will give rise to other child process and hence second fork will give rise to a total of 4 processes. Similarly for 3 forks we will get 8 processes. The total number of processes that are created are 2^n where number of calls to fork()

When a call is made to fork(), a child process is created. The fork() returns value of child‘s PID to parent process and child gets a copy of PID but with value 0.

The following picture will make things more clear

Let‘s see a program to make thing clearer.

#include #include #include #include #include #include int main ( ) { int i; for (i = 0; i < 2; i++) { int pid; struct timeval now;

gettimeofday(&now,NULL); printf("Parent(%d) loop=%d\ttime=%lu:%lu\n", getpid(), i, now.tv_sec, now.tv_usec);

pid = fork(); if (pid == 0) { gettimeofday(&now,NULL); printf("child(%d) loop=%d\ttime=%lu:%lu\n", getpid(), i, now.tv_sec, now.tv_usec); } else { int status; (&status); gettimeofday(&now,NULL); printf("parent(%d) =%d\ttime=%lu:%lu\n", getpid(), i, now.tv_sec, now.tv_usec); } } return 0; }

The output of above program will be somewhat like :

Parent(1461) loop=0 time=1114436229:206796 child(1462) loop=0 time=1114436229:213364 Parent(1462) loop=1 time=1114436229:219950 child(1463) loop=1 time=1114436229:226670 parent(1462) exit=1 time=1114436229:233241 parent(1461) exit=0 time=1114436229:239971 Parent(1461) loop=1 time=1114436229:240124 child(1464) loop=1 time=1114436229:253341 parent(1461) exit=1 time=1114436229:260401

I hope this program cleared your concepts about fork()

Orphan process

After a call to fork(), generally its the child process that get time slice to execute. Now say if parent process terminates for some reason before child process, we will have a orphan process as it parent process have died. As every child process have some parent this must be made child of some process, in this condition Dispatcher i.e process with PID 1 is automatically made its parent

Let‘s see a simple example

Main() { int pid; pid = fork(); if(pid==0) { printf(—hello\n“); sleep(20); printf(—my parent pid is %d\n“,getppid();) printf(—child exit\n“); } else { printf(—hello from parent\n“); printf(—parent exit\n“); } }

The out put of this prog will be Hello Hello form parent Parent exit My parent pid is 1 Child exit

Now in this as soon as the child goes to sleep the parent get time slice and start executing and in less than 20 sec it execution is over. After child wakes up it gets time slice but it‘s now orphan and is adopted by Dispacher so it prints parent process id as 1

Zombies

If for some reason a process is dead but haven‘t been removed from the process table it is called zombie. Now this can happen in a situation when say a child process has terminated and parent process has gone in some kind of infinite loop which don‘t allow child process to exit.

Main() { if(fork()>0) printf(—parent“); sleep(1000); }

Now child process terminates as soon as it come into action but parent process is gone into sleep making child process zombie

Fork Bomb:

A fork bomb can be written in one line of C (main() {for(;;)fork();}) on any Unix system. A fork bomb process ”explodes‘ by recursively spawning copies of itself. Eventually it eats all the process table entries and effectively wedges the system.

I will like to end this article here. For any queries you can write to me at [email protected]