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

int main(int argc, char **argv)
{
  pid_t pid;

  pid = fork();

  if (pid < 0) {
    fprintf(stderr,"Error: can't fork a process\n");
    perror("fork()");
    exit(1);
  } else if (pid) { // I am the parent
    int status;

    // wait for the child
    if (waitpid(pid,&status,0) == -1) {
      perror("waitpid()");
    } else {
      // print its exit code
      fprintf(stdout,"I am the parent. My child exited with code %d\n",
         WEXITSTATUS(status));
    }
  } else {
    fprintf(stdout,"I am the child and I am exec-ing \"ps\"\n");
    char *const argv[] = {"ps","-a",NULL};
    if (execv("/bin/ps",argv) == -1) {
      perror("execv");
      exit(1);
    }
  }
  exit(0);
}

