0

I want to make 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()) 
         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
  • yeah but there some changes in code after for..loop – JKLM Sep 28 '17 at 01:23
  • you starting children recursively. Is this what you want? So, when you say 'parent' process, which process did you mean, the **top** parent or the **immediate** parent? In the latter case you can `waitpid` for a child. – Serge Sep 28 '17 at 01:57
  • parent means TOP...can you show in code? – JKLM Sep 28 '17 at 02:04

0 Answers0