As i am executing a one pipe example pgm from libc.pdf,I noticed that the parent process first completed then only child process completed.From my knowledged about os the child process must complete before the parent completes.so what happens here
closed as unclear what you're asking by gnat, 8bittree, Newtopian, Andres F., Doc Brown Feb 17 '18 at 19:50
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
You cannot know, and on a multi-core system both processes could run simultaneously (on different cores). Notice that preemption (and context switches) can happen at any time (even "in the middle" of a C statement, compiled to a dozen of machine code instrutions), be it on a single-core system (very rare today) or a multi-core one, because interrupts making preemption and context switches occur at any machine code instruction.
(you should think as if (when
fork succeeds) both parent and child are running exactly simultaneously; what is actually happening is an unimportant, and difficult to observe, implementation detail)
Read wikipage on fork, then read carefully man page fork(2). Read also Advanced Linux Programming which has several chapters about forking and processes. Read also signal(7) and signal-safety(7). On Linux, run
cat /proc/interrupts a few times (read proc(5)) to understand how often context switches happen (usually every few milliseconds).
Read also Operating Systems: Three Easy Pieces.
On most systems, the child executes first. This is to avoid the overhead that comes with copy on write, since the parent does not write to the address space.