Creating multiple process using fork() Last Updated : 09 Oct, 2017 Summarize Comments Improve Suggest changes Share Like Article Like Report 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() CPP // 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() CPP // 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 Comment More infoAdvertise with us Next Article Create Processes with Fork in C++ P Pushpanjali chauhan Improve Article Tags : Misc C++ system-programming Practice Tags : CPPMisc Similar Reads Creating child process using fork() in Python Create a child process and display process id of both parent and child process. Fork system call use for creates a new process, which is called child process, which runs concurrently with process (which process called system call fork) and this process is called parent process. After a new child pro 2 min read Create Processes with Fork in C++ fork() is a system call that creates a child process from the parent process. Whenever we call fork() from the parent program, a child process is created that has the exact copy of the address space. The important thing to remember is it shares the copy of the address space, not the copy itself. Syn 3 min read Calculation in parent and child process using fork() Write a program to find sum of even numbers in parent process and sum of odd numbers in child process. Examples: Input : 1, 2, 3, 4, 5 Output : Parent process Sum of even no. is 6 Child process Sum of odd no. is 9 Explanation: Here, we had used fork() function to create two processes one child and o 2 min read fork() and memory shared b/w processes created using it Prerequisite: fork() in C So when we do a fork() what are the sections the two process actually share? Is the heap memory per process? Are the global variables shared? Will malloc return the same address to both? Let us run the program below and take a look at the output of it to clear the questions 4 min read Processes in Linux/Unix A program/command when executed, a special instance is provided by the system to the process. This instance consists of all the services/resources that may be utilized by the process under execution. Whenever a command is issued in Unix/Linux, it creates/starts a new process. For example, pwd when i 6 min read Process Control Commands in Unix/Linux Process control commands in Unix are: bg - put suspended process into background fg - bring process into foreground jobs - list processes bg Command : bg is a process control command that resumes suspended process while keeping them running in the background. User can run a job in the background by 3 min read Like