For problems A-C, indicate how many “hello” output lines the program would print
ID: 3628002 • Letter: F
Question
For problems A-C, indicate how many “hello” output lines the program would print.
Caution: Don’t overlook the printf function in main.
Problem A
void doit() {
fork();
fork();
printf("hello ");
return;
}
int main() {
doit();
printf("hello ");
exit(0);
}
Answer: _____ output lines.
Problem B
void doit() {
if (fork() == 0) {
fork();
printf("hello ");
exit(0);
}
return;
}
int main() {
doit();
printf("hello ");
exit(0);
}
Answer: _____ output lines.
Problem C
void doit() {
if (fork() == 0) {
fork();
printf("hello ");
return;
}
return;
}
int main() {
doit();
printf("hello ");
exit(0);
}
Answer: _____ output lines.
Page 3 of 21
For problem D, indicate the value of the counter variable that the program would print.
Problem D
int counter = 1;
int main() {
if (fork() == 0) {
counter--;
exit(0);
}
else {
wait(NULL);
counter++;
printf("counter = %d ", counter);
}
exit(0);
}
Answer: counter = _____.
Page 4
Explanation / Answer
PROBLEM A
hello
hello
hello
hello
hello
hello
hello
hello
Answer: ___8__ output lines.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.