During fork system call which execute first parent or child [closed]

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


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.


Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *