#include /* Symbolic Constants */ #include /* Primitive System Data Types */ #include /* Errors */ #include /* Input/Output */ #include /* Wait for Process Termination */ #include /* General Utilities */ int main() { pid_t childpid; /* variable to store the child's process id (basically an int) */ int retval; /* child process: user-provided return code */ int status; /* parent process: child's exit status */ printf("Process started, sleeping for 3 seconds...\n"); sleep(3); printf("ok, forking...\n"); /* now create new process */ childpid = fork(); if (childpid >= 0) /* fork succeeded */ { if (childpid == 0) /* fork() returns 0 to the child process */ { printf("CHILD: I am the child process!\n"); printf("CHILD: Here's my PID: %d\n", getpid()); printf("CHILD: My parent's PID is: %d\n", getppid()); printf("CHILD: The value of my copy of childpid is: %d\n", childpid); printf("CHILD: Sleeping for 5 seconds...\n"); sleep(5); /* sleep for 5 seconds */ printf("CHILD: Enter an exit value (0 to 255): "); scanf(" %d", &retval); printf("CHILD: Goodbye!\n"); exit(retval); /* child exits with user-provided return code */ } else /* fork() returns new pid to the parent process */ { printf("PARENT: I am the parent process!\n"); printf("PARENT: Here's my PID: %d\n", getpid()); printf("PARENT: The value of my copy of childpid is %d\n", childpid); printf("PARENT: I will now wait for my child to exit.\n"); wait(&status); /* wait for child to exit, and store its status */ printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status)); printf("PARENT: Goodbye!\n"); exit(0); /* parent exits */ } } else /* fork returns -1 on failure */ { perror("failed to fork"); /* display error message */ exit(0); } }