fork() to execute processes from bottom to up using wait() Last Updated : 20 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report fork() system call is used to create a process generally known as child process and the process that created it is known as parent process. Now, all the processes that are created using fork() runs concurrently. But what if we want the last process created to execute first and in this manner bottom to up execution such that parent process executes last. This can be done by using wait() system call. The parent process may then issue a wait() system call, which suspends the execution of the parent process while the child executes and when the child finishes execution, it returns exit status to operating system. Now wait() suspend the process until any of its child process finish execution. NOTE: This is a linux system call, so it must be executed on linux or unix variant system. CPP // Program to demonstrate bottom to up execution // of processes using fork() and wait() #include <iostream> #include <sys/wait.h> // for wait() #include <unistd.h> // for fork() int main() { // creating 4 process using 2 fork calls // 1 parent : 2 child : 1 grand-child pid_t id1 = fork(); pid_t id2 = fork(); // parent process if (id1 > 0 && id2 > 0) { wait(NULL); wait(NULL); cout << "Parent Terminated" << endl; } // 1st child else if (id1 == 0 && id2 > 0) { // sleep the process for 2 seconds // to ensure 2nd child executes first sleep(2); wait(NULL); cout << "1st child Terminated" << endl; } // second child else if (id1 > 0 && id2 == 0) { // sleep the process for 1 second sleep(1); cout << "2nd Child Terminated" << endl; } // grand child else { cout << "Grand Child Terminated" << endl; } return 0; } Python3 # Program to demonstrate bottom to up execution # of processes using fork() and wait() import os import time if __name__ == "__main__": # creating 4 process using 2 fork calls # 1 parent : 2 child : 1 grand-child pid1 = os.fork() pid2 = os.fork() # Parent Process if pid1 > 0 and pid2 > 0: os.wait() os.wait() print("Parent terminated") # 1st Child elif pid1 == 0 and pid2 > 0: # sleep the process for 2 seconds # to ensure 2nd child executes first time.sleep(2) os.wait() print("1st child terminated") # 2nd Child elif pid1 > 0 and pid2 == 0: # sleep the process for 1 second time.sleep(1) print("2nd child terminated") # Grand Child else: print("Grand child terminated") Output: Grand Child Terminated 2nd Child Terminated 1st child Terminated Parent Terminated Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article fork() and memory shared b/w processes created using it S shubham_rana_77 Follow Improve Article Tags : Technical Scripter C++ system-programming Practice Tags : CPP Similar Reads 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 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 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 Creating multiple process using fork() 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 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 Factorial calculation using fork() in C for Linux Write a Unix C program using the fork() system call that generates the factorial and gives a sequence of series like 1, 2, 6, 24, 120... in the child process. The number of the sequence is provided in the command line. Examples: Input :gfg@ubuntu:~/$ gcc -o fork fork.c gfg@ubuntu:~/$ ./fork 6 Output 3 min read Like