How to have 3 calls of fork() and create 6 processes without using a loop
I was given the task of creating exactly 6 processes using the c language and the command fork() and then have all 6 sleep using sleep() for a minute or two. The tougher part with this is that we are not allowed to use any loops at all, and only 3 calls to fork() total. I have managed to create 8 successfully but that is too many. So I was wondering if it is possible to fork or kill specific processes.I tried something I saw here but it is giving me 8 process not 6
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
/* fork a child process */
pid_t child = fork();
pid_t parent = getpid();
fork(); //2
if (fork() > 0) {//now we have 2 processes
//only the parent calls this fork because of the if, so we have 3 processes
fork(); //all 3 processes calls this fork, so we have 6 processes
wait(NULL);
printf("I am the child process");
printf("my PID = %d ",child);
printf("I am the parent process. My PID is %d\n", parent);
wait(NULL);
}
else if (child < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
}
out I am getting is:
I am the child processmy PID = 22150 I am the parent process. My PID is 22149
I am the child processmy PID = 22150 I am the parent process. My PID is 22149
I am the child processmy PID = 0 I am the parent process. My PID is 22150
I am the child processmy PID = 22150 I am the parent process. My PID is 22149
I am the child processmy PID = 0 I am the parent process. My PID is 22150
I am the child processmy PID = 0 I am the parent process. My PID is 22150
I am the child processmy PID = 0 I am the parent process. My PID is 22150
I am the child processmy PID = 22150 I am the parent process. My PID is 22149
Solution 1:
You can get a tree with 6 new processes (+1 the orginal) by making the third fork only in 3 out of the fourth processes that you'll have after the 1st 2 forks.
#include <unistd.h>
#include <stdio.h>
/*
Process tree:
/
/\
/\/
\
\ /
/\
\
*/
int main()
{
_Bool descended_from_parent, descended_from_child;
pid_t pid;
if(0>(pid=fork())) return perror("fork"),1;
descended_from_parent = pid == 0;
if(0>(pid=fork())) return perror("fork"),1;
descended_from_child = pid == 0;
if(!descended_from_parent || descended_from_child) if(0>(pid=fork())) return perror("fork"),1;
sleep(60);
}
// run as `./a.out &` and then do `ps --forest` to see the process tree
Solution 2:
#include <stdio.h>
#include <unistd.h>
int main(void) {
int f1 = -1, f2 = -1, f3 = -1;
printf("1 process running -- f1: %d; f2: %d; f3: %d\n", f1, f2, f3);
sleep(1); // try to prevent print lines mangling
f1 = fork(); // assume it worked
printf("2 processes running -- f1: %d; f2: %d; f3: %d\n", f1, f2, f3);
sleep(1); // try to prevent print lines mangling
f2 = fork(); // assume it worked for the 2 running processes
printf("4 processes running -- f1: %d; f2: %d; f3: %d\n", f1, f2, f3);
sleep(1); // try to prevent print lines mangling
if ((f1 > 0) || (f2 > 0)) f3 = fork(); // assume it worked for the 3 selected processes
printf("7 processes running -- f1: %d; f2: %d; f3: %d\n", f1, f2, f3);
return 0;
}