Assume the usual headers are included, fork succeeds, and the program runs from a terminal so that each printf line is written as a whole line as soon as it is called. Count only the five lettered lines, not any shell prompt.
int main(void) { printf("A\n"); pid_t pid = fork(); if (pid == 0) { printf("B\n"); printf("C\n"); exit(0); } printf("D\n"); waitpid(pid, NULL, 0); printf("E\n"); return 0; }
The scheduler may run the parent and child in any interleaving the code allows. Give the number of distinct possible outputs for the program as written, the number of distinct possible outputs if the waitpid line is deleted, and the letter of the line that is always printed last in the program as written.
Unlock full course notes by signing up
Sequence of events when a shell runs a command
A shell runs the command ./check, a program that finishes by calling exit(3). The shell waits for it with waitpid. Arrange the following events in the order they occur, starting with the shell creating the new process.
What survives a successful exec
A process with PID 5120 has file descriptor 3 open on log.txt, without the close-on-exec flag, and its parent is PID 5000. It calls execvp("sort", args) and the call succeeds. Which of the following statements are true?
Tracing a global variable through nested fork and wait
Assume the usual headers are included and every call to fork succeeds. The program below runs from a terminal, and each printf line appears as soon as it is called.
Here P is the original process, C is its child and G is the child's own child. For each process, enter the value of x it prints and the position of its line in the output, using 1 for the first line, 2 for the second and 3 for the third.
Zombies and exit statuses after selective waitpid
A parent process runs the code below. Assume every fork() succeeds, the parent has not changed how SIGCHLD is handled, and all four children have terminated before the parent begins sleep(60).
Find the number of zombie children of this parent while it sleeps, the value of WEXITSTATUS(s1), and the value of WEXITSTATUS(s2). Give each as a whole number.