Header Ads

Fork() System Call

System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of fork():

·        If fork() returns a negative value, the creation of a child process was unsuccessful.
·        fork() returns a zero to the newly created child process.
·        fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.

·        Therefore, after the system call to fork(), a simple test can tell which process is the child. Please note that Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces.

Predict the Output of the following program.

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main() {
    // make two process which run same
    // program after this instruction
    fork();
    printf("Hello world!\n");
    return 0;
}


Write a program that creates child process of a process i.e. process P having child process P1

#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
        pid_t  p;
        p=fork(); // creates duplicate process P1
        switch(p)
        {
                case -1:
                        printf("Error\n");
                        break;
                case 0:  // executed by child
                        printf(“I am child having id:%d\n”,getpid()); // prints pid of process
                        printf(“My parent’s id is:%d\n”,getppid());  // prints pid of parent process
                        break;
                default:  // executed by parent
                        printf(“I am parent having id:%d\n”,getpid());
                        printf(“My child’s id is: %d\n”,p);                                             
                        break;
        }
}

Write a program to create a child process. The parent process prints 20-29 and child process prints 0-9. Also both the process prints a common message.


#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
        pid_t p;
        int i,j;
        p=fork();
                switch(p)
                {
                        case -1:
                                printf("Error\n");
                                break;
                        case 0:               
                                for(i=0;i<10;i++)
                                {
                                printf("%d\n",i);
                                sleep(1);
                                }
                                break;
                        default:
                                wait(); //makes the parent wait for the child to finish
                                for(j=20;j<30;j++)
                                {
                                printf("%d\n",j);
                                sleep(1);
                                }
                                break;
                }
printf("common\n"); // Any statement outside switch is executed by both parent and child

}

Example 

#include <stdio.h>
#include <unistd.h>
int main()
{
    int id;
    printf("Hello, World!\n");
    id=fork();
    if(id>0)
    {
        /*parent process*/
        printf("This is parent section [Process id: %d].\n",getpid());
    }
    else if(id==0)
    {
        /*child process*/
        printf("fork created [Process id: %d].\n",getpid());
        printf("fork parent process id: %d.\n",getppid());
    }
    else
    {
        /*fork creation faile*/
        printf("fork creation failed!!!\n");
    }

    return 0;
}

 OUTPUT 

Hello, World!
This is parent section [Process id: 1252].
fork created [Process id: 1253].
fork parent process id: 1252.

Explanation

This is parent section [Process id: 1252].
1252 is the parent process id which is the process id of the main function too;
this is the parent section of new created fork.
fork created [Process id: 1253].
This message will print under the fork section and assigns a new id to the newly
created child process.
fork parent process id: 1252.
This message will also print under the fork section, it shows parent process id
of the newly created child process which is equivalent to parent process’s id
(main process id). Hence we can say newly created child process is the child
process of main which is known as a parent process.


Program :

/*
           P1
         /      \ 
       P2    P3
        |        |    
       P4    P5
*/ 
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
int main(){
  pid_t pid;
  printf("This is pid %d\n",getpid());
  int first=fork();
  wait();
  if(first==0){
    printf("this is the pid %d and parent ppid %d\n",getpid(),getppid());
    int third=fork();
    wait();
    if (third==0) {
      printf("this is the pid %d and parent ppid %d\n",getpid(),getppid());
    }
  }
  else{
      int second=fork();
    if(second==0){
     printf("this is the pid %d and parent ppid %d\n",getpid(),getppid());
      int four=fork();
      wait();
      if (four==0) {
        printf("this is the pid %d and parent ppid %d\n",getpid(),getppid());
      }
      }
      }
      return 0;
  }


Powered by Blogger.