The result of fork()
is that one process becomes two (by asexual reproduction). So while it is still the case that exactly one branch of the if/else block will be taken in a process, there are two processes, and one path will be taken by each.
More specifically, look at what fork()
returns: a PID to the parent, and 0 to the new child. Apart from that the two processes are almost identical. So the if (cpid == 0)
check is a common pattern after fork()
so that you can proceed with distinct logic in each process. In your case, that's reading in one process and writing in the other.