1,268 views
4 votes
4 votes
what if the program like this(t1 is copied to all or printf will copied to all child)

main()

{

t1=0,t2=0;

t2=fork();

Fork();

Printf("Hi");

Fork();

t1=Fork();

Printf("hi");

}

Arjun sir , I don't know the buffer concept.please explain with tree method. How many child and how many time Hi printed.

4 Answers

Best answer
5 votes
5 votes

20 "hi" will be printed  and 15 child process will be created , here is an image how to visualize it. The best way is to write value on branches, the parent is which have all the values of fork! =0;

sorry the right hand side will be the parent. i just misplaced it. well the concept is right.

selected by
3 votes
3 votes
For the first "hi" first 2 forks will work...

Hence 4 times "hi"

For the next "hi" all 4 forks will work

So 16 times "hi"

Total 20 times....
1 votes
1 votes

t2 = fork()   ===== two processes (1 child + 1 parent)

fork()   ====    total 4 process at this point

hi       ====  4 hi printed

fork() =     processes becomes 8

t1= fork()   == ==  processes becomes  16

hi         == 16 hi

therfore total 20 "hi" printed

total processes = 16 (15 child , 1 parent)

0 votes
0 votes
The answer to the question is

Child processes created = 15

No. Of times 'hi' is printed = 16

No. Of times 'Hi' is printed = 16

This is because every process has 1 hi and 1 Hi printed statement, and stdout is buffered (not printed immediately and carried/copied over to its child process) so both hi and Hi is printed for 16 times rather than 4 times.

Related questions

2 votes
2 votes
3 answers
3
Philosophical_Virus asked Dec 10, 2023
879 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
3 votes
3 votes
1 answer
4
24aaaa23 asked Oct 1, 2023
772 views
Consider the following code segment :int main (void) { for (int i=2; i<=5; i++) { fork( ); }printf (“GOCLASSES”);How many times “GOCLASSES” is printed by the ab...