1

I want to write a C program that asks the user for an integer and stores it in a variable n. Then, the main process creates two child processes (both must be children of the main process). One child exits successfully if n>10 and unsuccessfully otherwise, whereas the other child exits successfully if n>20 and unsuccessfully otherwise. The main process must print how many of its children ended successfully.

This is what I have so far.

#include <stdio.h>

void main (void)
{

int n;

printf("Give me a number: ");

scanf("%d", &n);

pid_t child_a, child_b;

child_a = fork();

if (child_a == 0) {


         if (n>10) {
                exit(1)
         }else{
                exit(0)
        }
} else {


child_b = fork();

if (child_b == 0) {
        if (n>20){
                exit(1)
        }else{
                exit(0)
}
    } else {
        /* Parent Code */
    }
}





}

But how do I count how many child processes end successfully?

koll
  • 11
  • 3
  • I recommend you [read a tutorial on *signals*](https://lasr.cs.ucla.edu/vahab/resources/signals.html), or perhaps a couple of them. When you can handle signals in general and understand the concept, do a search for the `SIGCHLD` signal, and read about it. That way you can count the number of children that exist if the number of `fork` call is not deterministic. However in your case it *is*. You start *two* child processes, and that's it. That means you could [`wait`](http://man7.org/linux/man-pages/man2/wait.2.html) for each child process to exit, and through that call get the exit status – Some programmer dude Nov 27 '17 at 04:31
  • The `printf()` statement is woefully botched; it won't compile. It's hard to tell what you have in mind. You can loop and collect the dead bodies of the children. For example: `int count = 0; int corpse; int status; while ((corpse = wait(&status)) > 0) { printf("PID %d status 0x%.4X\n", corpse, status); if (status == 0) count++; }` — etc. Print more information; it will help you understand (if you do it sensibly). – Jonathan Leffler Nov 27 '17 at 04:45
  • Pasted it wrong should be fixed now. – koll Nov 27 '17 at 06:03
  • Ignore anyone's advice about signals. You do not need to know anything about signals to do this. Read documentation on `wait` – William Pursell Nov 27 '17 at 10:50
  • Why are you using `scanf`? Do *not* read the parameter from stdin. It makes far more sense to take it as a command line argument. – William Pursell Nov 27 '17 at 10:51

1 Answers1

1

You need to use wait() command, system call wait() function blocks the calling process until one of its child processes exits or a signal is received.

int status;
pid_t pid = wait(&status);
printf("Exit = %d, child = %d\n", WEXITSTATUS(status), pid);

see example at:

fork() and wait() with two child processes

and 2nd example using wait() and WEXITSTATUS()

Linux fork() and wait()

Also more about what happen with multiple cores scheduling of processes:

fork() and wait() calls

alk
  • 69,737
  • 10
  • 105
  • 255
Mark
  • 78
  • 6
  • Please note that `WEXITSTATUS` shall only be used if `WEXITED` evaluated to true. Please see the docs here: https://man7.org/linux/man-pages/man2/waitpid.2.html – alk Nov 27 '17 at 11:01