/* This program creates a new process.
 * The parent reports the idenfication numbers of it and the child.
 * The child executes an expanded hello-world program.
 *
 * Created by Henry Walker
 */

#include <sys/types.h>       /* file of data types needed for many compilers */
#include <unistd.h>          /* needed for fork, getpid procedures */
#include <stdio.h>

int main (void)
{  pid_t pid;                /* variable to record process id of child */
   
   pid = fork();             /* create new process */
   if ( -1 == pid)           /* check for error in spawning child process */
     { perror ("error in fork");  
       exit (1);
     }

   if (0 == pid)             /* check if this is the new child process */
     { /* processing for child */
       char *argv[6];
       printf ("This output comes from the child process\n");
       printf ("Preparing to execute \"Hello World\" program\n");
       
       /* setting up command */
       argv[0] = "hello-world";
       argv[1] = "this";
       argv[2] = "is";
       argv[3] = "a";
       argv[4] = "test";
       argv[5] = NULL;

       /* executing the program */
       execv (argv[0], argv);

       printf ("this line, after the exec command, is never executed\n");

     } 
  else 
     { /* processing for parent */
       printf ("This output comes from the parent process.\n");
       printf ("Parent report:  my pid = %d   child's pid = %d\n", 
                getpid(), pid);     
     }

   exit (0);                 /* quit by reporting no error */
}
