title: How assign a new terminal window to each child process
body: <p>I want to fork multiple processes and assign each child process it's own terminal window so that IPC can be demonstrated easily.
  Forking goes on fine, and if I run the child processes on the same terminal it runs fine.
  However to make each child process have its own terminal window, I do </p>
  
  <pre><code>execl("/usr/bin/xterm", "xterm", "-e", "yourprogram", NULL);
  </code></pre>
  
  <p>The program runs in a new window, but its PID is different from that shown by the parent which forks the process. What am I doing wrong?</p>
  
  <p>Thanks</p>
  
  <p>edit1 - this is my main function (the parent process). I fork 4 child processes. I want each child process to have its own terminal window. However the child process just exits, and a new process with a different PID continues to run in the new terminal.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  
  int main()
  {
  
  pid_t pid[4]; 
  int i = 0;
  int status;
  
  //Fork four new processes
  for(i=0; i&lt;4; i++)
  {
      pid[i] = fork();
  
      if(pid[i] == 0 &amp;&amp; i == 0)
      {
          execl("/usr/bin/xterm", "xterm", "./child1", NULL);
          exit(1);
      }
      else if(pid[i] == 0 &amp;&amp; i == 1)
      {
          execl("/usr/bin/xterm", "xterm", "./child2", NULL);
          exit(1);
      }
      else if(pid[i] == 0 &amp;&amp; i == 2)
      {
          execl("/usr/bin/xterm", "xterm", "./child3", NULL);
          exit(1);
      }
      else if(pid[i] == 0 &amp;&amp; i == 3)
      {
          execl("/usr/bin/xterm", "xterm", "./child4", NULL);
          exit(1);
      }
      else
      {
          //Parent process
          printf("The main function has forked a process with pid: %d\n", pid[i]);
      }
  }
  
  for(i=0;i&lt;4;i++)
  {
      status = waitpid(pid[i], NULL, 0);
      if(status == pid[i])
          printf("%d: Process Terminated Successfully\n", pid[i]);
      else
      {
          perror("waitpid");
          exit(1);
      }
  }
  
  return 1;
  }
  </code></pre>
  
  <p>edit2 - added ps -u output:</p>
  
  <pre><code>USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
  dell-pc   3024  0.1  0.0  26872  5480 pts/0    Ss   16:54   0:00 bash
  dell-pc   3038  0.0  0.0   4200   632 pts/0    S+   16:54   0:00 ./main
  dell-pc   3039 22.5  0.1 109240 11116 pts/0    S+   16:54   0:01 xterm ./child1
  dell-pc   3040 26.1  0.1 109240 11268 pts/0    R+   16:54   0:02 xterm ./child2
  dell-pc   3041 28.7  0.1 109240 11180 pts/0    S+   16:54   0:02 xterm ./child3
  dell-pc   3042 27.0  0.1 109240 11288 pts/0    S+   16:54   0:02 xterm ./child4
  dell-pc   3044  4.1  0.0   4200   648 pts/24   Ss+  16:55   0:00 child3
  dell-pc   3046  3.7  0.0   4200   680 pts/26   Ss+  16:55   0:00 child4
  dell-pc   3048  3.8  0.0   4200   792 pts/25   Ss+  16:55   0:00 child2
  dell-pc   3050  3.3  0.0   4200   660 pts/14   Ss+  16:55   0:00 child1
  dell-pc   3060  2.0  0.0  26816  5412 pts/27   Ss   16:55   0:00 bash
  dell-pc   3072  0.0  0.0  22648  2688 pts/27   R+   16:55   0:00 ps -u
  </code></pre>
  
  <p>edit3: added output of main:</p>
  
  <pre><code>The main function has forked a process with pid: 3491
  The main function has forked a process with pid: 3492
  The main function has forked a process with pid: 3493
  The main function has forked a process with pid: 3494
  3491: Process Terminated Successfully
  3492: Process Terminated Successfully
  3493: Process Terminated Successfully
  3494: Process Terminated Successfully
  </code></pre>