
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fork to Execute Processes Using Wait in C++
The fork() system call is used to create a process commonly known as a child process if the fork() returns 0. Otherwise, the created process is known as the parent process.
All processes created with fork() execute in parallel. But what if we want the last process to be executed first? In this case, the parent process would execute last because of bottom-to-top execution. This can be done using the wait() system call.
The wait system call is used to process handling. It pauses the execution of the calling process until the child process has finished its execution. It's commonly used in a scenario where the parent process creates a child process using fork().
Example of Bottom-up Execution of Processes using fork() and wait()
Following is the program to demonstrate bottom-up execution of processes using fork()
and wait()
?
#include <iostream> #include <sys/wait.h> #include <unistd.h> using namespace std; int main() { //Creates 4 process using two consecutive fork. The main process, two children and one grand child pid_t id1 = fork(); 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 1 seconds cout << "Ending of Second child process" << endl; } else { cout << "Ending of grand child" << endl; } return 0; }
Following is the output of the above code ?
Ending of grand child Ending of Second child process Ending of First Child Ending of parent process
Example for Creating Parent, Child, and Grandchild Processes
The following is another example that uses fork() to create multiple processes, following a parent-child-grandchild hierarchy. The grandchild completes first, followed by the child, and finally the parent process ?
#include <iostream> #include <sys/wait.h> #include <unistd.h> using namespace std; int main() { // First fork - creates child pid_t id1 = fork(); if (id1 < 0) { cerr << "Fork failed!" << endl; return 1; } if (id1 == 0) { // Second fork - creates grandchild pid_t id2 = fork(); if (id2 < 0) { cerr << "Fork failed!" << endl; return 1; } if (id2 == 0) { // Grandchild process cout << "Grandchild is calculating sum..." << endl; int sum = 0; for (int i = 1; i <= 10; i++) { sum += i; } cout << "Grandchild finished: Sum = " << sum << endl; } else { // First child waits for grandchild wait(NULL); cout << "Child is printing a message..." << endl; cout << "Hello from the Child Process!" << endl; } } else { // Parent process wait(NULL); cout << "Parent is finalizing..." << endl; cout << "Parent Process Terminated!" << endl; } return 0; }
Following is the output of the above code ?
Grandchild is calculating sum... Grandchild finished: Sum = 55 Child is printing a message... Hello from the Child Process! Parent is finalizing... Parent Process Terminated!