1,277 views

2 Answers

3 votes
3 votes
The given answer is wrong. The output may be "HiHi" or "Hi". At the time of fork, printf might not have been executed and hence the code of printing might be copied to child as well. (printf is a non-blocking call). To avoid this behaviour, either we have to use fflush or use the return value of printf before calling fork.
0 votes
0 votes
when fork() is executed, it creates two same copies of program (1 for parent and another for child) with different address space. hence both will print "Hi". hence output is :HiHi. No matter which "Hi" comes first.(order is unspecified)

Related questions

2 votes
2 votes
3 answers
2
Philosophical_Virus asked Dec 10, 2023
884 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
0 votes
0 votes
1 answer
3
Erwin Smith asked Apr 11, 2023
782 views
void main() { int n = 1; if(fork()==0) { n = n<<1; printf(“%d, “, n); n = n <<1; } if(fork()==0) n=n+700; printf(“%d, “,n); }Which of the following output is not ...