מערכות הפעלה מערכות הפעלה

לחץ כאן לכל השאלות

What is the output of following code? #include <stdio.h> #include <unistd.h> int main() { if (fork() && (!fork())) { if (fork() || fork()) { fork(); } } printf("2 "); return 0; }

1
done
1. Fork will create two process one parent P (has process id of new child) and other one is child C1 (process id=0). 2. In if statement we are using AND operator (i.e, &&) and in this case if first condition is false then it will not evaluate second condition and print 2. Parent process P check for second condition and create two new processes (one parent P and other is child C2). In second condition we are using NOT operator which return true for child process C2 and it executes inner if statement. 3. Child C2 again create two new processes (one parent C2 and child C3) and we are using OR operator (i.e, ||) which evaluate second condition when first condition is false. Parent C2 execute if part and create two new processes (one parent C2 and child C4) whereas child C3 check for second condition and create two new processes (one parent C3 and child C5). 4. Parent C3 enters in if part and further create two new processes (one parent C3 and child C6).
by
מיין לפי

* השאלה נוספה בתאריך: 20-07-2020