title: Child processes with fork
body: <p>I am trying to make a simple C program that will call the <code>fork</code> method three times and display identifiers of child processes (UID, GID, PID, PPID, PGID). And I am struggling with proper understanding what is really happening. There is my code in which I use separate methods for method <code>fork()</code> and for displaying identifiers, and also in for for parent process I am trying to use the <code>waitpid</code> method to wait for all child processes to die. I am really confused now because I cannot get it working in a way that clearly says it works in proper way. Can you give me any suggestions or show a better way for my problem? </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  void identifiers();
  void forkMethod();
  
  int main(void)
  { 
      forkMethod();
      return 0;
  }
  
  void forkMethod()
  {
      int k;
      int status;
      for (k=0;k&lt;3;k++){
          switch (fork()) {
          case -1:
              perror("fork error");
              exit(EXIT_FAILURE);
              break;
          case 0:         
              identifiers();  
              break;
          default:
              //wait(&amp;status);
              waitpid(getpid(), &amp;status, WNOHANG);
              sleep(1);
              break;
          }
      }
  }
  
  void identifiers()
  {
      pid_t pid = getpid();
      pid_t ppid = getppid();
      pid_t pgid = getpgid(pid);
      pid_t uid = getuid();
      pid_t gid = getgid();
      printf("UID:%d GID:%d PID:%d PPID:%d PGID:%d\n", uid, gid, pid, ppid, pgid);
  }
  </code></pre>
  
  <p>// ===========================================================</p>
  
  <p>First thank you all for answers and pointing all of these mistakes I made,</p>
  
  <p>I know the code is horrible and probably still have a few issues but now i think i have what i wanted to.</p>
  
  <p>As @John Bollinger ask what is main functionality that is my list:
  1. Write a function that displays identifiers UID, GUID, PID, PPID, PGID for specified process
  2. call fork() function 3 times and display these identifiers for child processes
  3. Use the sleep function to display processes in order from the oldest
  4. display a processes tree based on results.</p>
  
  <p>Thank you @juhist and @Jonathan Leffler for sharp and simple explanation. If there any significant issues in code please post </p>
  
  <p>and know the final code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  void identifiers(); // function that display identifiers
  void forkMethod(); // function that calls fork() function 3 times
  void tree(int); // function displaying processes tree
  
  int main(void)
  { 
  identifiers(); // displaying ID's for parent process
  printf("Parent pid: %d\n", getpid());   
  printf("Child processes: \n");      
  
  forkMethod();   
  
  return 0;
  }
  
  void forkMethod()
  {
  int k;
  int status;
  int pid;
  
  for (k=0;k&lt;3;k++){
  pid = fork();
      switch (pid) {
      case -1:
          perror("fork error");
          exit(EXIT_FAILURE);
          break;
      case 0: 
      identifiers(); 
      exit(0);             
      default:           
      tree(getpid());            
      wait(&amp;status); 
          sleep(1);   
      break;
      }
  }
  }
  
  void identifiers()
  {
  pid_t pid = getpid();
  pid_t ppid = getppid();
  pid_t pgid = getpgid(pid);
  pid_t uid = getuid();
  pid_t gid = getgid();
  printf("\nUID:%d GID:%d PID:%d PPID:%d PGID:%d\n", uid, gid, pid, ppid, pgid);
  }
  
  void tree(int pid)
  {
  char pstree[] = "pstree -np "; 
  char cmd[12];
  sprintf(cmd, "%s%d", pstree, pid);
  system(cmd);
  }           
  </code></pre>