Raiana Salmin Zaman
Raiana Salmin Zaman

Reputation: 75

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

Upvotes: 2

Views: 1551

Answers (4)

Prabhat Mishra
Prabhat Mishra

Reputation: 1

Simplest way

#include <stdio.h>
int main()
{
    fork() && fork();
    fork();
    printf("%d %d\n",getpid(),getppid());
    sleep(1);
}

Upvotes: 0

William Pursell
William Pursell

Reputation: 212238

Your logic is just fine. I suspect the confusion is because of the initialization of the variable child, in which you invoke fork for the first time. Also, your output is odd, and you are not reporting output for each process. Simply refactoring your code to something simpler shows that your logic is correct:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>    
int
main(void)
{
        fork();
        if( fork() == 0 ) {
                fork();
        }
        printf("PID = %d\n", getpid());
}

The above is basically your code, and it gives exactly 6 lines of output, as desired. The only real difference is that I removed the spurious initial call to fork, and moved the printf to the right place. In your code, you have 4 processes when your comment claims there are only 2 (because of the spurious initialization fork), and all 4 of them fork again to make 8. Of those 8, 4 of them refork to give you a total of 12, of which only 8 print any output. (The 4 that forked and their new children.) The output that they give is most unusual, though, and does not reflect the actual pid of the process writing the output. They merely show the pids as the were after the initial fork in the initialization of child.

Upvotes: 0

pmg
pmg

Reputation: 108978

#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;
}

Upvotes: 1

Petr Skocik
Petr Skocik

Reputation: 60058

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

Upvotes: 1

Related Questions