in Operating System
1,061 views
4 votes
4 votes
if(fork()||fork()) {
    fork();
    fork();
}
printf("ABC")

 

HOW MANY TIMES ABC CALLED?

in Operating System
1.1k views

4 Comments

I'm also getting 9
0
0
edited by

YEPP IT'S 9  .

@ Shaik Masthan @Magma

PLEASE ANYONE EXPLAIN IT WITH THE TREE METHOD

0
0
16?
0
0

1 Answer

7 votes
7 votes
Best answer

Basics:-

The || operator, if First operand gives true then Second operand doesn't evaluate

For clear image https://drive.google.com/open?id=1S538Oa9z0Bm4YtBZeJHxARH2pqPxtLLz 



you can also check following links for better understanding variations on fork() system call

https://gateoverflow.in/232839/fork-system-call

https://gateoverflow.in/231896/fork-system-call

selected by

4 Comments

One thing tell me, how many number of child and parent process created here?
0
0

here means, LOGICAL OR or LOGICAL AND , in which question?

 

  • So, 1st fork() executed , 2 process created 1) Parent process(fork1() !=0) and 2) child process (fork1()=0) 

i agree.

 

Now, when 2nd fork2() calls- only fork1()!=0 will be executed,So,again 2 process will be created [Here fork1()=0 only be executed, iff there is some else part in program, but that is not the case here]

it is wrong. fork1()!=0 ===> directly enter into IF BLOCK, due to it is logical OR.

fork1() = 0 ===> it will be executed on 2nd fork()

 

Please revise your basics on LOGICAL OR and LOGICAL AND operators.

https://www.geeksforgeeks.org/operators-in-c-set-2-relational-and-logical-operators/

1
1

See I have applied this logic only

The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied

Have u understand properly? 

0
0