#include #include #include #include int main() { pid_t pid; pid = fork(); /* fork another process */ if (pid < 0) /* Forking error! */ { exit(-1); } else if (0 == pid) /* Child process */ { // The exec() call replaces the process image of the child // with a new process image; instead of running myFirstFork, // the child will contain the 'ls' program. execlp("/bin/ls", "ls", NULL); // Notice that if the exec() function works, nothing else below // this line should be executed by the child process. The // exec() call replaces the program that the child process // is running with the 'ls' program, and the child process // never comes back to the old code. fprintf(stderr, "Why am I still alive? Exec() must have failed!\n"); } else /* pid > 0: Parent process */ { printf("Child process is %d\n", pid); wait(NULL); exit(0); } }