CS FundamentalsHardGATE CS · Amazon/Microsoft SDE MCQ

What does this program print? (Assume fork() succeeds and the exec of echo succeeds.)

What does this program print? (Assume fork() succeeds and the exec of echo succeeds.)
int main() {
    fork();
    execlp("echo", "echo", "hi", NULL);
    fork();
    printf("x");
}
  1. A. hi and x, twice each
  2. B. hi once
  3. C. hi four times
  4. D. hi twice and no x

Correct answer: D. hi twice and no x

fork() makes 2 processes, and each replaces its image with echo, which prints hi. A successful exec never returns, so the code after it, including the second fork and printf, never runs.

More Operating Systems questions

Exams that ask this topic