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");
}- A. hi and x, twice each
- B. hi once
- C. hi four times
- 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
- In a traditional Unix file system (such as ext2/ext4 without very large directories), directory /d contains 3 subdirectories and 5 regular…
- A process with umask 027 creates a regular file requesting mode 0666 and a directory requesting mode 0777. What permissions do they get?
- A process writes to a pipe whose read end has been closed by every process. What happens by default?
- A device raises 10,000 interrupts per second, and each interrupt costs 50 µs of CPU time to enter, service and return. What fraction of…
- A file is stored with linked allocation (each block holds a pointer to the next) and nothing is cached. How many disk reads does it take…
- Requests arrive at a server at 20 per second, and Little's law applies in steady state. If on average 6 requests are in the system, what…