简体   繁体   中英

Program Of forking processes using switch statement in C

I'm trying to fork 2 processes from parent as following, but sometimes I get error(program don't finish) and I don't know why:

pid_t pidA, pidB;
pidA = fork();
switch (pidA) {
    case -1: 
        // error handling
        return -1;
    case 0:
        // first child code
        break;
    default: {
        // parent code 1
        pidB = fork();
        switch(pidB) {
            case -1:
                // error handling
                return -1;
            case 0:
                // second child code
                break;
            default:
                // parent code 2, I think it's the same like parent code 1. Am I right?
                waitpid(-1, NULL, 0);
                printf("parent\n");
                break;
         }
         // parent code 3, again same like parent code 1 and 2 ???
         // when I use printf("Parent\n"); here it prints Parent 2 times and I don't know why.
     }
 }

Can someone help me with this, find what is wrong here. Some explanation would be great. Thank you

Consider the process tree:

    P
   / \
  P   C1
 / \
P  C2

First fork : Splits into P and C1

Second fork : Splits into P and C2

C1 returns and does not print anything.

C2 breaks from switch, prints Parent (note capital p) and returns.

P prints parent and Parent .

So this explains why it prints Parent two times.


And as for the program not finishing, check the return value of waitpid() and try to debug it accordingly.

What exactly are you trying to do here? When you fork for the first time, your first child process will essentially do nothing. The parent will resume and fork again returning pidB and will then resume executing up until the waitpid() function is executed.

I assume you're trying to wait for the new child code to execute and then terminate before the parent continues to print "parent".

You're waiting on a PID of -1, which probably should be pidB .

If you put a printf() outside the switch block, the child will print "parent" on its way out, same with the parent that spawned it. You probably want a return statement instead of a break if you want the child to terminate immediately.

Running your code as-is, with the printf statement outside the second switch statement, this is my output, using pidB to show which process is printing:

parent
Parent: pid=25046
Parent: pid=0

The results show that the child is one of the ones printing as well.

If you add a return instead of a break in the inner switch case 0, this is what you get:

parent
Parent: pid=25095

Here is what I suggest

// parent code 1
    pidB = fork();
    switch(pidB) {
        case -1:
            // error handling
            return -1;
        case 0:
             // second child code
             break;
        default:
            // parent code 2, I think it's the same like parent code 1. Am I right?
            waitpid(pidB, NULL, 0); // wait for the child process to finish by using the PID of the child as the argument to waitpid
            printf("parent\n");
            break;
     }
     // the child will execute anything here unless you return or otherwise prevent the child process from getting here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM