retagged by
1,052 views
6 votes
6 votes

Assume the following code is compiled and run on a modern Linux machine.

Assuming $\textsf{fork()}$ never fails, how many times will the message $\textsf{“Hello”}$ will be displayed?

#include <stdio.h>
#include <unistd.h>
int main() {
    int a = 0;
    int rc = fork();
    if (rc == 0) {
        rc = fork();
    } else {
        printf("Hello");
    }
    printf("Hello");
    return 0;
}
  1. $2$
  2. $3$
  3. $4$
  4. $6$
retagged by

2 Answers

2 votes
2 votes

The program starts and enters the main() function.

The variable a is declared and initialized to 0.

The fork() system call is invoked, creating a new process. Let's call the original process "Process A" and the newly created process "Process B."

In Process A, the value of rc is not equal to 0, so it proceeds to the else block and prints "Hello."

Process A continues execution and reaches the second printf("Hello") statement, resulting in another "Hello" message being printed by Process A.

In Process B, the value of rc is equal to 0, so it enters the if statement.

Inside the if block of Process B, another fork() is called, creating a new process. Let's call this newly created process "Process C."

Process B, does not enter the else block and proceeds and reaches the second printf("Hello") statement and prints "Hello."

Process C, being the child process of Process B, does not enter the if statement and does not enter the else block because it is created within the if block of Process B. C proceeds and reaches the second printf("Hello") statement and prints "Hello."

Process A, Process B, and Process C exit the main() function, and the program terminates.

Therefore, Process A prints "Hello" twice, while Process B and Process C each print "Hello" once.

0 votes
0 votes
upon successful exceution fork() returns 0 to child processes and processID to parent process. so the else case will never be executed. meanwhile 1 becomes 2 and 2 become 4 due to fork(). so 4 times HELLO will be printed.
Answer:

Related questions

856
views
2 answers
4 votes
GO Classes asked Jan 21
856 views
Consider the following jobs along with their arrival and execution time. ... these processes with the preemptive shortest remaining processing time first (SRPT) algorithm?
533
views
1 answers
6 votes
GO Classes asked Jan 21
533 views
What will be the output of the following C program?#include<stdio.h> void main() { int i=6; for(--i; --i; i--) { printf("%d",i); } }$42$31$Infinite loopNone of these
573
views
1 answers
1 votes
GO Classes asked Jan 21
573 views
Consider the following C program:-#include <stdio.h> void ubswap(int **a, int **b) { int* temp = *a; *a = *b; *b = temp; } int main() { int x = 1, y = 9; int ... $x$ and $y$u$ and $v$a$ and $b$None of the above
604
views
1 answers
3 votes
GO Classes asked Jan 21
604 views
You are given a bit-array $A[1 \ldots n]$ (i.e., $A[i] \in\{0,1\}$ for each $i$ ) and told that this is a "$0$ -to-$1$ bit-array. This means that ... $\Theta(n \log n)$\Theta(n)$\Theta\left(n^{2}\right)$