Program to create four processes (1 parent and 3 children) where they terminates in a sequence as follows :
(a) Parent process terminates at last
(b) First child terminates before parent and after second child.
(c) Second child terminates after last and before first child.
(d) Third child terminates first.
Prerequisite : fork(),
// CPP code to create three child // process of a parent #include <stdio.h> #include <stdlib.h> #include <unistd.h> // Driver code int main() { int pid, pid1, pid2; // variable pid will store the // value returned from fork() system call pid = fork(); // If fork() returns zero then it // means it is child process. if (pid == 0) { // First child needs to be printed // later hence this process is made // to sleep for 3 seconds. sleep(3); // This is first child process // getpid() gives the process // id and getppid() gives the // parent id of that process. printf("child[1] --> pid = %d and ppid = %d\n", getpid(), getppid()); } else { pid1 = fork(); if (pid1 == 0) { sleep(2); printf("child[2] --> pid = %d and ppid = %d\n", getpid(), getppid()); } else { pid2 = fork(); if (pid2 == 0) { // This is third child which is // needed to be printed first. printf("child[3] --> pid = %d and ppid = %d\n", getpid(), getppid()); } // If value returned from fork() // in not zero and >0 that means // this is parent process. else { // This is asked to be printed at last // hence made to sleep for 3 seconds. sleep(3); printf("parent --> pid = %d\n", getpid()); } } } return 0; } |
chevron_right
filter_none
Output:
child[3]-->pid=50 and ppid=47 child[2]-->pid=49 and ppid=47 child[1]-->pid=48 and ppid=47 parent-->pid=47
This code runs on Linux platform.
Recommended Posts:
- Create n-child process from same parent process using fork() in C
- Pass the value from child process to parent process
- Chat application between two processes using signals and shared memory
- Zombie Processes and their Prevention
- Program to show that Linux provides time sharing environment to processes
- Exit status of a child process in Linux
- Fork() Bomb
- Fork() - Practice questions
- Header files in C/C++ and its uses
- Types of C files after its compilation
- Getting System and Process Information Using C Programming and Shell in Linux
- Operations on Audio/Video files using ffmpeg, avconv, and youtube-dl
- Python | System hardening and compliance reports using Lynis
- C/C++ program to print Hello World without using main() and semicolon
- C program for Time Complexity plot of Bubble, Insertion and Selection Sort using Gnuplot
- Encrypting and Decrypting the Files Using GnuPG in Linux
- Two way communication between Client and Server using Win32 Threads
- Create your own secure Home Network using Pi-hole and Docker
- Exposing Localhost Server over Internet Using ngrok and VS Code
- Formatted text in Linux Terminal using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

