
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
fork() to execute processes from bottom to up using wait() in C++
We know that the fork() system call is used to divide the process into two processes. If the function fork() returns 0, then it is child process, and otherwise it is parent process.
In this example we will see how to split processes four times, and use them in bottom up manner. So at first we will use fork() function two times. So it will generate a child process, then from the next fork it will generate another child. After that from the inner fork it will automatically generates a grandchild of them.
We will use wait() function to generate some delay and execute the processes as bottom up manner.
Example Code
#include <iostream> #include <sys/wait.h> #include <unistd.h> using namespace std; int main() { pid_t id1 = fork(); //make 4 process using two consecutive fork. The main process, two children and one grand child pid_t id2 = fork(); if (id1 > 0 && id2 > 0) { //when both ids are non zero, then it is parent process wait(NULL); wait(NULL); cout << "Ending of parent process" << endl; }else if (id1 == 0 && id2 > 0) { //When first id is 0, then it is first child sleep(2); //wait 2 seconds to execute second child first wait(NULL); cout << "Ending of First Child" << endl; }else if (id1 > 0 && id2 == 0) { //When second id is 0, then it is second child sleep(1); //wait 2 seconds cout << "Ending of Second child process" << endl; }else { cout << "Ending of grand child" << endl; } return 0; }
Output
soumyadeep@soumyadeep-VirtualBox:~$ ./a.out Ending of grand child Ending of Second child process Ending of First Child Ending of parent process soumyadeep@soumyadeep-VirtualBox:~$
- Related Articles
- How to implement Fibonacci using bottom-up approach using C#?
- How to implement minimum step to one using bottom-up approach using C#?
- How to implement coin change problem using bottom-up approach using C#?
- Creating multiple process using fork() in C
- fork() in C
- Python Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach
- Calculation in parent and child process using fork() in C++
- C program to demonstrate fork() and pipe()
- What is Bottom-up Parsing?
- Maximum sum path in a matrix from top to bottom in C++
- How to Execute Programs From Anywhere in Linux?
- Maximum mirrors which can transfer light from bottom to right in C++
- Maximum sum path in a matrix from top to bottom in C++ Program
- How to execute a single test from a large testing suite using TestNG.xml?
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed in C++

Advertisements