Creating A Process in Linux (UNIX) - fork() Example Program

The system call fork() is used to  create a new process in UNIX based operating systems and Linux systems. The fork() system call creates a child process when called from a parent process. 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. Here is a C program which uses the fork() system call to create a process during execution. The program is commented well for better understanding.

Program

/*The program (parent process) reads an array from the user.
It sorts it in descending order.


The child process created by parent process sorts it ascending order
*/

#include<stdlib.h>
#include<stdio.h>
int main()
{
  int cpid;
  int temp,a[100],n,i,j;
  printf("\nEnter number of elements : ");
  scanf("%d",&n);
  printf("Enter elements\n");
  for(i=0;i<n;i++){
     scanf("%d",&a[i]);
  }

printf("\n");
  cpid=fork(); /*Creating child process using fork() system call */

if(cpid<0)

{

printf("Cannot create a new process\n");

exit(0);

}
  if(cpid != 0)

{

/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/

  printf("\nThis is parent with id: %d\n",getpid());

/* The UNIX system call getpid() return the process id of process being executed.*/
     for(i=0;i<n;i++){
          for(j=0;j<n-1;j++){
                  if(a[j]<a[j+1]){
                     temp = a[j];
                     a[j] = a[j+1];
                     a[j+1] = temp;
                  }
          }
    }
 printf("Parent sorted the array in descending order\n");

}else

{

/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/


    printf("\nThis is child with id: %d \n",getpid());
    for(i=0;i<n;i++){
          for(j=0;j<n-1;j++){
                  if(a[j]>a[j+1]){
                     temp = a[j];
                     a[j] = a[j+1];
                     a[j+1] = temp;
                  }
          }
     }
 printf("Child sorted the array in ascending order\n");

  }

/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/

 for(i=0;i<n;i++){
    printf("%d\n",a[i]);
  }
   return 0;
}



Output:

Enter number of elements : 5
Enter elements
8
6
10
2
12


This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2

This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12

Output of Linux program in C using fork() system call to create child process fork parent process start execution process id pid getpid return value
Output of the program


No comments :

Post a Comment