Counting possible output orders with and without waitpid

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.


C
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.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Concurrent Programs Questions: