



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Detailed informtion about Fork in C Program , orphan process, processes, Forking multiple child processes in C program , Programms.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!
In this example, we will write a simple C program which will fork a child process. The "fork()" function will be used to do this. fork() returns 0 to the child process and the child pid to the parent process. Our parent process and child process will count up to ten. For process switching by operating system, we will observe in the output that the processes are not printing serially, rather they are printing in turn.. Type in the following C program in your editor: // forking.c #include <stdio.h> int main(void) { // Variable to hold the process id int pid; // Fork the process and get the process id in "pid" pid = fork(); // Check the value of pid if(pid == 0) { /* Child process:
Everything on the right of a "//" is a single line comment and everything in between a pair of "/" and "/" are multi-line comments. Comments are not part of the program. Save the file as "forking.c" and then from the terminal compile and build it with the following command: {cs1:~} gcc - o forking forking.c {cs1:~} ls - l
In C program, the task of creating multiple child processes is simple. After a pid check for parent process, we simply create another process. Here is an example which creates two child processes: // forking2.c #include <stdio.h> int main(void) { // Variable to hold the process id int pid; // Fork the process and get the process id in "pid" pid = fork(); // Check the value of pid if(pid == 0) { /* Child process:
{cs1:~} gcc - o forking2 forking2.c {cs1:~} ./forking child: 0 child: 0 parent: 0 child: 1 child: 1 parent: 1 parent: 2 child: 2 child: 2 child: 3 child: 3 parent: 3 parent: 4 child: 4 child: 4 child: 5 child: 5 parent: 5 parent: 6 child: 6 child: 6 child: 7 child: 7 parent: 7 parent: 8 child: 8 child: 8 child: 9 parent: 9 child: 9 The output will be similar to above. It may not be the same because it depends on process scheduling.
Synchronously waiting for specific child processes in a (specific) order may leave zombies present longer than the above-mentioned “short period of time”. It is not necessarily a program bug, but rather a programming paradigm that is not seen very often in the wild. #include <sys/wait.h> #include <stdlib.h> #include <unistd.h> int main(void) { pid_t pids[10]; int i; for (i = 9; i >= 0; --i) { pids[i] = fork(); if (pids[i] == 0) { sleep(i+1); _exit(0); } } for (i = 9; i >= 0; --i) waitpid(pids[i], NULL, 0); return 0; }