1

How to modify this code so that the parent process prints out its information, after all, child process done with execution.

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    int main (int argc, char *argv[]) {
       pid_t childpid = 0; 
       int i, n;

       if (argc != 2){   /* check for valid number of command-line arguments */ 
          fprintf(stderr, "Usage: %s processes\n", argv[0]);
          return 1; 
       }     
       n = atoi(argv[1]);  
       for (i = 1; i < n; i++)
          if ((childpid = fork()) <= 0)
             break;

       fprintf(stderr, "i:%d  process ID:%ld  parent ID:%ld  child ID:%ld\n",
               i, (long)getpid(), (long)getppid(), (long)childpid);
       return 0; 
    }
JKLM
  • 1,470
  • 3
  • 28
  • 66

1 Answers1

4

To synchronize a parent process with child processes, you want to make use of wait or waitpid

https://linux.die.net/man/3/wait

To start off simple, you could call wait in a loop that would iterate as many times as the number of children you spawned that you wish to wait for. Perhaps something along these lines after you've finished spawning all the children could get you started:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main (int argc, char *argv[]) {
   pid_t childpid = 0; 
   int i, n;

   if (argc != 2){   /* check for valid number of command-line arguments */ 
      fprintf(stderr, "Usage: %s processes\n", argv[0]);
      return 1; 
   }     
   n = atoi(argv[1]);  
   for (i = 1; i < n; i++)
         if ((childpid = fork()) <= 0)
            break;

   if (childpid>0){ // parent process
       while (i>1) {
           wait(NULL);
           i--;
       }
   }

   fprintf(stderr, "i:%d  process ID:%ld  parent ID:%ld  child ID:%ld\n",
           i, (long)getpid(), (long)getppid(), (long)childpid);
   return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
  • I read this already but not able to understand..can you help me with code? – JKLM Sep 27 '17 at 23:35
  • I have added a little code sample that I think should work at least on a basic level. – Christian Gibbons Sep 27 '17 at 23:55
  • is it possible you to show with the example I mentioned in the question, im learning this, showing in code will help – JKLM Sep 28 '17 at 00:03
  • That code is intended to be inserted into your own code sample after you break out of the `for-loop` and before `fprintf` – Christian Gibbons Sep 28 '17 at 00:05
  • im not getting it :( – JKLM Sep 28 '17 at 00:06
  • edited one more time to insert it into your sample code. – Christian Gibbons Sep 28 '17 at 00:11
  • @JonathanLeffler I do not disagree with any of your comment. Missing the argument in `wait()` was an error on my part. As for the robustness of the code, I was only trying to illustrate a simple sample to give the OP an basic understanding of its usage. You are more than welcome to improve upon it if you think it necessary and proper. – Christian Gibbons Sep 28 '17 at 04:11
  • I'll remove my previous comment shortly, but I strongly counsel that you ensure that all functions are declared before writing code in answers. It's a good idea to test what you write — speaking from experience, it is too damn easy to make silly mistakes. AFAIK, all my code on SO compiles cleanly under GCC using `gcc -O3 -std=c11 -g -Werror -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes …` unless there's a comment to the contrary. It's possible that some earlier code (from 2008 to 2011, roughly) only compiles with `-std=c99`, or no longer compiles cleanly under a more modern GCC. – Jonathan Leffler Sep 28 '17 at 04:18
  • The prototype options are stringent; you'll find I declare most functions (other than `main()`) as static because of them, and I never use `int main()`, for example — it's always `int main(void)`. The basic rule, though, is that you shouldn't use a function that doesn't have a prototype in scope — so all system calls such as `wait()` should be properly prototyped. – Jonathan Leffler Sep 28 '17 at 04:20
  • If it were my code, the `wait()` loop would not be protected by `if (childpid > 0)` and would look more like: `int status; int corpse; while ((corpse = wait(&status)) > 0) printf("PID %d exited with status 0x%.4X\n", corpse, status);`. There are extremely unusual circumstances under which a program can inherit children it didn't create. Note that I was careful to use 'program' and not 'process' — the circumstances involve a first program, `program1`, forking some children and then executing the second program, `program2`, which has children it didn't know about because it didn't create them. – Jonathan Leffler Sep 28 '17 at 04:26