4 votes 4 votes if(fork()||fork()) { fork(); fork(); } printf("ABC") HOW MANY TIMES ABC CALLED? Deepanshu asked Sep 13, 2018 Deepanshu 1.6k views answer comment Share Follow See all 7 Comments See all 7 7 Comments reply Show 4 previous comments Magma commented Sep 13, 2018 reply Follow Share I'm also getting 9 0 votes 0 votes Deepanshu commented Sep 13, 2018 i edited by Deepanshu Sep 13, 2018 reply Follow Share YEPP IT'S 9 . @ Shaik Masthan @Magma PLEASE ANYONE EXPLAIN IT WITH THE TREE METHOD 0 votes 0 votes neeti commented Sep 13, 2018 reply Follow Share 16? 0 votes 0 votes Please log in or register to add a comment.
Best answer 8 votes 8 votes 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 Shaik Masthan answered Sep 13, 2018 selected Sep 13, 2018 by Deepanshu Shaik Masthan comment Share Follow See all 15 Comments See all 15 15 Comments reply Deepanshu commented Sep 13, 2018 reply Follow Share @Shaik Masthan I AM NOT GETTING THE IDEA OF CHILD PROCESS, CHILD PROCESS (P1) RETURN 0 THEN WHY WE ARE CALLING FORK() ON THAT 0 votes 0 votes Shaik Masthan commented Sep 13, 2018 reply Follow Share Program running is a Process, let it P0, when you call fork(), then one more process creates, let the new process is P1 P1 is child of P0 ( due to it is created by P0 when fork(), statement execute. ) the fork(), doesn't only creates the process but also return value. the return value is 1) < 0 ====> failed the fork() system call 2) = 0 ====> indicate it is child 3) > 0 ====> indicate it is parent 0 votes 0 votes srestha commented Sep 13, 2018 reply Follow Share why P5 not creating any process? 0 votes 0 votes Shaik Masthan commented Sep 13, 2018 reply Follow Share P5 can't enter into IF block. No fork statements out of IF block ==> No more child processes created by P5. 0 votes 0 votes Deepanshu commented Sep 13, 2018 reply Follow Share @Shaik Masthan YOU ARE SAYING THAT P1 WHICH IS THE CHILD OF P0 IS 0 OKK THEN WE ARE CALLING FORK() ON P0 AND WE GET P1 AND P5 . AND HERE U R SAYING P1 IS >=0 IFP1 IS 0 THEN HOW AND WHY P1 >= 0 IN NEXT STATEMENT WHEN IT DECOMPOSE 0 votes 0 votes Shaik Masthan commented Sep 13, 2018 reply Follow Share note that, those fork() are different at first fork(), P1 = 0 at second fork executed by P1, then P1 > 0. 0 votes 0 votes srestha commented Sep 14, 2018 reply Follow Share yes, it will be 9 but fork call will be like this 0 votes 0 votes Shaik Masthan commented Sep 14, 2018 reply Follow Share @srestha, mam given that logical OR ==> on 1st fork(), parent returns non-zero, then How it will execute 2nd fork() statement. logical OR ===> (e1 || e2 ), if e1 returns non-zero, then e2 can't execute. 1 votes 1 votes srestha commented Sep 14, 2018 reply Follow Share if execute on nonzero value that is why 2nd fork will be there I am pretty sure, this pic is correct plz carefully chk it 0 votes 0 votes srestha commented Sep 14, 2018 reply Follow Share See if(fork()||fork()) Now for getting inside if() block atleast one fork "inside if block" must be true. So, 1st fork() executed , 2 process created 1) Parent process(fork1() !=0) and 2) child process (fork1()=0) 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] Now fork3() will be executed when fork1()!=0, it will not depend on fork2() value . Because if block will be executed either fork1() or fork2() anyone value is 1. So, here 4 process will be created Next again fork4() will be executed when fork1() is 1, so 8 process will be created by fork4() So, for fork4() there will be 8 process and fork1()=0 there is 1 process. Total 9 process will be created 0 votes 0 votes Shaik Masthan commented Sep 15, 2018 reply Follow Share ok mam, just replace logical OR by logical AND, then how it evaluates? 0 votes 0 votes srestha commented Sep 15, 2018 reply Follow Share then fork1() and fork2() both fork()!=0 will be printed And total no of process will be 6 0 votes 0 votes srestha commented Sep 15, 2018 reply Follow Share One thing tell me, how many number of child and parent process created here? 0 votes 0 votes Shaik Masthan commented Sep 15, 2018 reply Follow Share 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 votes 1 votes srestha commented Sep 15, 2018 reply Follow Share 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 votes 0 votes Please log in or register to add a comment.