-1

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.

  • 1
    If there was a deterministic answer to this question, what would be the point of forking off a new process? – Ixrec Apr 30 '16 at 16:50
  • Are you sure to be exactly able to define what does "execute first" means? – Basile Starynkevitch Apr 30 '16 at 17:33
  • Not a software engineering or design question. – Andres F. Feb 16 '18 at 17:55
3

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.

  • Even on a single core system, time slicing could result in a non-deterministic processing order. In general, you cannot make assumptions about the relative ordering of instructions in different threads or processes unless you enforce it with mutexes or semaphores. – Kyle A Feb 15 '18 at 3:07
-3

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.

  • Completely wrong in general – Basile Starynkevitch Feb 15 '18 at 5:47
  • 1
    There is no "X executes before Y" relation between parent and child processes (as there is with normal functions where "callee executes before the caller). Processes are executed concurrently (at least conceptually). As @BasileStarynkevitch noted, this is a completely bogus answer. – Mael Feb 15 '18 at 6:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.