Open In App
Related Articles

Creating multiple process using fork()

Improve Article
Improve
Save Article
Save
Like Article
Like

Prerequisite – Introduction of fork, getpid() and getppid()
Problem statement – Write a program to create one parent with three child using fork() function where each process find its Id. For example :

Output :parent
28808 28809 
 my id is 28807 
First child
0 28810 
my id is 28808  
Second child
28808 0 
my id is 28809  
third child
0 0 
 

Explanation – Here, we had used fork() function to create four processes one Parent and three child processes.

  • An existing process can create a new one by calling the fork( ) function.
  • The new process created by fork() is called the child process.
  • We are using here getpid() to get the process id
  • In fork() the total process created is = 2^number of fork()

Note – At some instance of time, it is not necessary that child process will execute first or parent process will be first allotted CPU, any process may get CPU assigned, at some quantum time. Moreover process id may differ during different executions.

Code 1 – Using getpid()




// C++ program to demonstrate creating processes using fork()
#include <unistd.h>
#include <stdio.h>
  
int main()
{
    // Creating first child
    int n1 = fork();
  
    // Creating second child. First child
    // also executes this line and creates
    // grandchild.
    int n2 = fork();
  
    if (n1 > 0 && n2 > 0) {
        printf("parent\n");
        printf("%d %d \n", n1, n2);
        printf(" my id is %d \n", getpid());
    }
    else if (n1 == 0 && n2 > 0)
    {
        printf("First child\n");
        printf("%d %d \n", n1, n2);
        printf("my id is %d  \n", getpid());
    }
    else if (n1 > 0 && n2 == 0)
    {
        printf("Second child\n");
        printf("%d %d \n", n1, n2);
        printf("my id is %d  \n", getpid());
    }
    else {
        printf("third child\n");
        printf("%d %d \n", n1, n2);
        printf(" my id is %d \n", getpid());
    }
    return 0;
}


Output –

parent
28808 28809 
 my id is 28807 
First child
0 28810 
my id is 28808  
Second child
28808 0 
my id is 28809  
third child
0 0 

Code 2 – Using getppid()




// C++ program to demonstrate creating processes using fork()
#include <unistd.h>
#include <stdio.h>
  
int main()
{
    // Creating first child
    int n1 = fork();
  
    // Creating second child. First child
    // also executes this line and creates
    // grandchild.
    int n2 = fork();
  
    if (n1 > 0 && n2 > 0)
    {
        printf("parent\n");
        printf("%d %d \n", n1, n2);
        printf(" my id is %d \n", getpid());
        printf(" my parentid is %d \n", getppid());
    }
    else if (n1 == 0 && n2 > 0)
    {
        printf("First child\n");
        printf("%d %d \n", n1, n2);
        printf("my id is %d  \n", getpid());
        printf(" my parentid is %d \n", getppid());
    }
    else if (n1 > 0 && n2 == 0)
    {
        printf("second child\n");
        printf("%d %d \n", n1, n2);
        printf("my id is %d  \n", getpid());
        printf(" my parentid is %d \n", getppid());
    }
    else {
        printf("third child\n");
        printf("%d %d \n", n1, n2);
        printf(" my id is %d \n", getpid());
        printf(" my parentid is %d \n", getppid());
    }
  
    return 0;
}


Output –

parent
5219 5220 
 my id is 5217 
 my parentid is 5215 
First child
0 5221 
my id is 5219  
 my parentid is 1 
second child
5219 0 
my id is 5220  
my parentid is 1 
third child
0 0 
 my id is 5221 
 my parentid is 1 

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 09 Oct, 2017
Like Article
Save Article
Previous
Next
Similar Reads