We need to report the result of the following C program:
main() { int pid, k, som; som = 0; k = 2; pid = fork(); if(pid == 0) k=5; else wait(0); for(int i = 1; i <= k; i++) som += i; printf("%d", som); }
My first expectation is 3. When the fork call is made, the process memory is copied and both programs start. Then the child process is executed, but k is still equal to 2. Thus, in the end it executes 1 + 2 = 3;
But when this program is executed, it outputs 153. I do not have the closest key why it outputs it.
Can someone say why?
source share