+ 3

How to solve the below question?

Give all the possible display results obtained by running the below program?? Assume that the parent id Pid = 100; /* If you can explain how fork() works with + and * */ int main(){ int x; x = ( fork() + fork() ) * fork() ); print("%d", x); }

11th Nov 2018, 6:35 PM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
5 Antworten
+ 7
Im not an expert, but fork would create a copied unix process which executee from fork call (same happen for original parent process) return 0 to child process and process PID to parent process. In your case, i think, this happen at (fork() + fork()) * fork(): - at first fork call, A process (the main process) create a child process B -at second fork call, either in A and B will forked other child 2 processes (C and D) - at third fork call, other 4 processes will be forked from A, B, C and D (E,F,G,H) at end you will have 8 processes (in practice 2^n where n is fork calls count). Anyway, about the x value, all depends on which process you want see... In main process (the original A) x will be (100+100)*100=20000 while in forked processes all depends by own PID and from parent process. Anyway, im not sure, maybe someone more expert will help you better
11th Nov 2018, 8:25 PM
KrOW
KrOW - avatar
+ 4
Try Solve yourself
11th Nov 2018, 6:51 PM
Izaak GOLDSTEIN
Izaak GOLDSTEIN - avatar
+ 3
KrOW thank you for your reply
12th Nov 2018, 2:06 PM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 3
👍👍👍
12th Nov 2018, 2:07 PM
KrOW
KrOW - avatar
+ 2
I don't know how fork() works with the two operators "+" and "*"
11th Nov 2018, 7:12 PM
Mohammad Dakdouk
Mohammad Dakdouk - avatar