Question

Assume each POSIX API call (fork, write, etc.) does not fail. Which of the following are...

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!

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The following program calls the fork() system call to create a child process. Suppose that the...
The following program calls the fork() system call to create a child process. Suppose that the actual pids of the parent process and child process are as follows: Parent process: 801 Child process:   802 (as returned by fork) Assume all supporting libraries have been included. => Use the answer text field to write the values of the pids printed at lines 1, 2, 3, and 4 (indicate each line number and the value printed). int main() {      pid_t pid,...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this lab assignment, please use a Word document to include the screenshots and write your answer. 1. Run the following C program, and submit a screenshot of the result. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main( ) { pid_t pid; if ( (pid = fork()) == 0 ) { printf (“I am the child, my pid = %d and my parent pid = %d\n”,...
(5 pts.) Consider the C program below. Recall that fflush() just forces the printed value to...
(5 pts.) Consider the C program below. Recall that fflush() just forces the printed value to be output immediately. (For space reasons, we are not checking error return codes, so assume that all functions return normally.) int main () {    if (fork() == 0) {        if (fork() == 0) {           printf("3"); fflush(stdout);        }        else {           pid_t pid; int status;           if ((pid = wait(&status)) > 0) {                  printf("4"); fflush(stdout);           }        }...
For each of the following propositions, write in all comparisons that make it always true among...
For each of the following propositions, write in all comparisons that make it always true among the four possibilities: < > == != If none are guaranteed to hold, please indicate that explicitly by marking it with an X. Assume the variables are declared with int x,y; and initialized to some unknown values. You may assume that int’s are 32 bits wide, char’s are 8 bits wide and that right shift is arithmetical on signed numbers and logical on unsigned...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
You need to write a program that reads in two integers from cin and outputs a...
You need to write a program that reads in two integers from cin and outputs a horribly inefficient calculation of the median value. First, count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number. Enter: 3 9 Out: 3 4 5 6 7 8 9 9 8 7 6 5 4 4 5 6 7 8 8 7...
Give the output of the following program. Then explain what the foo() method does, in general,...
Give the output of the following program. Then explain what the foo() method does, in general, for a given input. Is foo() a tail-recursive method? Why or why not? class SomeThing {     public static void main(String args[]) {         System.out.println(foo(6));     }     private static int foo(int input) {         if (input <= 0) {             throw new RuntimeException("invalid input");         }         else if (input == 1) {             return 1;         }         else if (input %...
1. Please write the following in C++ also please show all output code and comment on...
1. Please write the following in C++ also please show all output code and comment on code. 2. Also, use CPPUnitLite to write all test and show outputs for each test. Write CppUnitLite tests to verify correct behavior for all the exercises. The modifications are aimed at making the exercises more conducive to unit tests. Write a function that swaps (exchanges the values of two integers). Use int* as the argument type. Write a second swap function using a reference...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
in C language only. Write a program called gamecards.c that has a card game logic by...
in C language only. Write a program called gamecards.c that has a card game logic by comparing the cards from 4 people and calculating which player has the best card. 1. Every card must be represented by exactly two chars representing a rank and a suit. ranks: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. ('T' = 10) Suits: 'H', 'D', 'S', 'C'. 7D represents the “7 of Diamond” etc.. 2. Write a function called...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT