4

I know there's another thread with the same name, but this is actually a different question.

When a process forks multiple times, does the parent finish executing before the children? Vice versa? Concurrently?

Here's an example. Lets say I have a for loop that forks 1 parent process into 4 children. At the end of that for loop, I want the parent process to feed some data to the children via pipes. The data is written to each child process' respective stdin.

Will the parent send the data first, before any of the children execute their code? This is important, because we don't want it to start working from an invalid stdin.

Bob
  • 715
  • 2
  • 11
  • 32
  • 1
    When a process forks multiple times, does the parent finish executing before the children? Vice versa? Concurrently? - Concurrently and depends on the scheduler and its unpredictable. – Dilip Kumar Sep 28 '16 at 19:54
  • you can use wait function call to wait until child process is finished and then the parent process will execute – Dilip Kumar Sep 28 '16 at 19:57
  • How are you getting the pipes of the child processes? Isn't this what `popen()` was designed for? (Also, after `fork()` returns, two processes should exist, so at that point, whichever process you're in, a fully formed child process should exist with valid stdin and stdout) – ebyrob Sep 28 '16 at 20:04
  • Why do you think the child will work from invalid stdin? If it tries to read the pipe before the parent has written anything, it will just wait, it won't return invalid data. – Barmar Sep 28 '16 at 20:51
  • It depends on how the forked processes are written. Beginner shells (often seen on SO — starting in October, usually) often make the mistake of trying to run a pipeline so that the shell waits for the first child to finish before launching the second. This is bad in general, though they often get away with because the child doesn't write more data than fits in a pipe buffer. You can arrange for the children not to do anything until the parent finishes; you can arrange for the parent not to do anything until the children finish; you can arrange for fully concurrent execution of them all, etc. – Jonathan Leffler Sep 29 '16 at 06:43
  • Possible duplicate of [Can the order of execution of fork() be determined?](https://stackoverflow.com/q/6696959/608639), [In fork() which will run first, parent or child?](https://stackoverflow.com/q/21586292/608639), [Is there a good way to alter execution order of child processes created with fork()?](https://stackoverflow.com/q/46523164/608639), [Using fork(), how can I make child process run always first?](https://stackoverflow.com/q/30276244/608639), etc. – jww Oct 28 '19 at 21:42

3 Answers3

7

The order of the execution is determined by the specific OS scheduling policy and not guaranteed by anything. In order to synchronize the processes there are special facilities for the inter-process communication (IPC) which are designed for this purpose. The mentioned pipes are one example. They make the reading process to actually wait for the other process to write it, creating a (one-way) synchronization point. The other examples would be FIFOs and sockets. For simpler tasks the wait() family of functions or signals can be used.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
1

When a process forks multiple times, does the parent finish executing before the children? Vice versa? Concurrently? -

Concurrently and depends on the scheduler and its unpredictable.

Using pipe to pass integer values between parent and child

This link explains in detail about sharing data between parent process and child. Since you have four child process you may need to create different individual pipes between each child process.

Each byte of data written to a pipe will be read exactly once. It isn't duplicated to every process with the read end of the pipe open.

Multiple child processes reading/writing on the same pipe

Alternatively you can try shared memory for the data transfer.

Community
  • 1
  • 1
Dilip Kumar
  • 1,736
  • 11
  • 22
0

They will execute concurrently. This is basically the point of processes.

Look into mutexes or other ways to deal with concurrency.

Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78