[Solved] fork() example program. need explanation


First of all, in the core image of your program, you initialise two values, ret_val, and i which acts as a counter.

From there on, for 5 times, you fork() the program, creating another process with the same image (code). At this point I am assuming your code is wrong, because you are using the ret_val variable to check if it’s the child or parent process, but to do so, you need to assign it the value from fork() like this:

ret_val = fork();

if (ret_val == 0)
   // do something as child
else
   // parent code here

In essence, your code, for 5 times, increments the value of i and has each child process display the current value of i.

11

solved fork() example program. need explanation