Assume each POSIX API call (fork, write, etc.) does not fail. Which of the following are possible outputs? write(STDOUT_FILENO, "A", 1); int value = 0; pid_t pid1 = fork(); if (pid1 == 0) { write(STDOUT_FILENO, "B", 1); value += 1; } else { write(STDOUT_FILENO, "C", 1); value += 2; } pid_t_pid2 = fork(); char value_as_string[2]; sprintf(value_as_string, "%d", value); write(STDOUT_FILENO, value_as_string, 1);
1. ABC1122
2. ACB1122
3. ABC3333
4. AC22B11
5. AC22C33
6. AC221B1
Please explain how you got the possible outputs. Thanks!
Option 4.AC22B11 is the right answer.
Explanation:
It will print A as per write command. pid1 is the childprocess created. But when the program is executed first the if condition fails and it will print the C value in the else part and since value is 2 there number 2 gets printed.The sprintf statement holds the value 2 hence again 2 is printed(AC22). Then second time it goes inside the child process and prints the B character and the value is 1 there so 1 is printed.
Get Answers For Free
Most questions answered within 1 hours.