0 votes 0 votes Consider the following program segment of C-programming language: #include<stdio.h> int main() { if (fork() || fork()) fork(); printf("GATE\n"); return 0; } Number of times the above program prints 'GATE' is_? Operating System threads operating-system process + – jayadev asked Feb 1, 2022 jayadev 987 views answer comment Share Follow See all 11 Comments See all 11 11 Comments reply palashbehra5 commented Feb 1, 2022 reply Follow Share is it 7? 0 votes 0 votes Kabir5454 commented Feb 1, 2022 reply Follow Share 5 is the answer. 1 votes 1 votes palashbehra5 commented Feb 1, 2022 reply Follow Share Right, 2nd fork wont be invoked due to short circuiting. 0 votes 0 votes jayadev commented Feb 1, 2022 reply Follow Share Can you explain in detail? 0 votes 0 votes jayadev commented Feb 1, 2022 reply Follow Share no 5 @palashbehra5 0 votes 0 votes palashbehra5 commented Feb 1, 2022 reply Follow Share See if you find anything incorrect.https://www.geeksforgeeks.org/short-circuiting-in-c-and-linux/ 0 votes 0 votes Kabir5454 commented Feb 1, 2022 reply Follow Share first say parent process P call first fork() in if statement. it then create its its child c1 so till now we have P and C1. now P return some non zero number and C1 return 0 as child is C1 which return 0 after the fork() call. now P has some non zero number and if has OR it will check the another fork() in if block. now P call fork() again which is after the If statement. now P will create another child C2 . So as of now we have 3 process P,C1,C2. now come to C1 which is still in if block . it return 0 so it will call the next fork() in if statement. so C1 create another child C12 . now C12 return 0 so it will not execute the fork() after if statement but C1 will execute the fork() after the If statement which will create another process C13. So total we have 5 process P,C1,C2,C12,C13. Each of them execute the print statement. so 5 is the answer. you can try this using tree diagram but i like to do this way. 0 votes 0 votes jayadev commented Feb 1, 2022 reply Follow Share in child c1 why the first fork value which is in if statement will be 0 ? @Kabir5454 @palashbehra5 0 votes 0 votes Kabir5454 commented Feb 1, 2022 reply Follow Share Because C1 was created because of P’s fork() calling. So in the first fork() C1 being child so return 0 and P which is parent return non zero value in the first fork(). 2 votes 2 votes palashbehra5 commented Feb 1, 2022 reply Follow Share https://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html 1 votes 1 votes jayadev commented Apr 7, 2022 reply Follow Share Here in this problem the program counter stores the if statement for all child and parent process and both forks in if statement are executed by all child and parent process. Is it correct? 0 votes 0 votes Please log in or register to add a comment.
2 votes 2 votes @jayadev The answer is 5 ( 1 1 1 1 1 )...… 1. It will create two process one parent P (has process ID of child process)and other is child C1 (process ID = 0).... 2. In if statement we used OR operator( || ) and in this case second condition is evaluated when first condition is false.... 3. Parent process P will return positive integer so it directly execute statement and create two more processes (one parent P and other is child C2)… Child process C1 will return 0 so it checks for second condition and second condition again create two more processes(one parent C1 and other is child C3)… 4. C1 return positive integer so it will further create two more processes (one parent C1 and other is child C4). Child C3 return 0 so it will directly print 1… aaa 1 answered Feb 1, 2022 aaa 1 comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes p (>0) p--------------------------------c(=0) (>0)p----------------c1(=0)-----------------c(>0)-----------------c2(=0) print(gate) print(gate) c(>0)-----------c3(=0) print(gate) print(gate) print(gate) 5 times gate will be printed aaaakash001 answered Oct 20, 2022 aaaakash001 comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes Ans 7.....because we don't know which process execute first so we must execute on both sides Kalle vignesh answered Sep 25, 2023 Kalle vignesh comment Share Follow See all 0 reply Please log in or register to add a comment.