Question: Fork Consider the program below that runs without errors: int main() { int i; printf(Mangoes ); int r = fork(); printf(Apples ); if (r ==

Fork

Consider the program below that runs without errors:

int main() {

int i;

printf("Mangoes ");

int r = fork();

printf("Apples ");

if (r == 0) {

printf("Oranges ");

int k = fork();

if (k >= 0) {

printf("Bananas ");

}

} else if (r > 0) {

printf("Peaches ");

for(i = 0; i < 3; i++) {

if ((r = fork()) == 0) {

printf("Pears ");

exit(0);

printf("Nectarines ");

} else if (r > 0) {

printf("Plums ");

}

}

}

}

1. In the table below, indicate how many times the name of each fruit is printed.

Fruit name

Times printed

Mangoes

Apples

Oranges

Bananas

Peaches

Pears

Nectarines

Plums

2. Several orderings of the fruit names are possible valid output. Some of these orderings even have the unix prompt displaying before the final fruit name (or names). Explain why this happens.

3. Not all of the fruit names could appear after the prompt in a valid output. For example the word Mangoes will never appear after the prompt. List all the fruit names that could occur after the prompt.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!