/** * fork.c * * Demonstrates using the fork() system call. * */ #include #include #include #include #include int main(void) { pid_t my_pid = getpid(); printf("Starting up, my PID is %d\n", my_pid); pid_t child = fork(); if (child == -1) { perror("fork"); } else if (child == 0) { /* I am the child */ pid_t my_pid = getpid(); printf("Hello from the child! PID = %d. Going to sleep.\n", my_pid); sleep(5); execl("/bin/ls", "ls", "-l", (char *) 0); printf("hi\n"); /* Will this print? */ } else { /* I am the parent */ pid_t my_pid = getpid(); printf("Hello from the parent! PID = %d\n", my_pid); printf("PID %d waiting for its child (%d)!\n", my_pid, child); int status; wait(&status); printf("Child finished executing. Parent exiting.\n"); } return 0; }