//* Start a new process in Posix *

/** * This program forks a separate process using the fork()/exec() system calls. * * Figure 3.10 * * @author Gagne, Galvin, Silberschatz * Operating System Concepts - Eighth Edition * Copyright John Wiley & Sons - 2008. */ #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <stdio.h> int main() { pid_t pid; /* fork a child process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed\n"); exit(-1); } else if (pid == 0) { /* child process */ fprintf(stdout, "I am the child %d\n",pid); execlp("/bin/ls","*",NULL); } else { /* parent process */ /* parent will wait for the child to complete */ fprintf(stdout, "I am the parent %d\n",pid); wait(); fprintf(stdout, "Child Complete\n"); exit(0); } }