How to create a certain number of child processes using fork ()

I need to create a certain number of parallel child processes. I also want every child process to change the global variable, so the main parent process can print it in the latest modified version. When I run the program below, the final value for "k" will be 5, so the global variable will not change. If I delete the "exit (0)" part, the global variable will change, but this time the number of child processes created will become larger.

Using fork (), how do I create an X number of child processes that can modify data (global variables, local variables, etc.) in the main parent process?

int k = 5; // global variable int main(){ int i=0; int status; for(i = 0; i<5; i++){ if(fork() == 0){ printf("child %d %d\n", i, ++k); sleep(5); printf("done %d\n",i); exit(0); } } return 0; } 
+4
source share
5 answers

As Kevin remarked, you really need threads. IPC implementation is redundant for this. Take a look at the following link.

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

+2
source

You cannot do it this way. Fork will create a new process that will copy (or copy to write) memory pages into the new process. This means that each of your child processes will receive its own copy of "k", and each copy of "k" will only increase once.

In order for all processes to bind to the "same" variable k, you need to perform some interprocess communication.

Examples of good interprocess communication:

  • Ask the parent process to create a shared memory segment that stores the value k. Ask the child processes to wait for the exclusive capture of the shared memory segment (via the parent process created by the mutex). When a child has an exclusive lock, ask the child to read the value of k and keep the value of k + 1.

  • Create a channel for each process between the parent child processes. Ask the parent to read the pipe for a message indicating a desire to increase k. Then the parent process will increment k by the name of the child.

Examples of poor communication between processes:

  • Any solution that does not guarantee that changes in K are atomic (this means that two competing children can simultaneously increase K to one value). Such a lack of care will lead to a K value, which, apparently, increases several times more than the number of child processes, since the set values ​​may look (2, 3, 3, 4, 5). This means that an I / O file is basically useless unless you create a framework around it that provides atomic file lock operations for exclusive access.
+2
source

By default, child processes cannot change anything in the parent address space. To do this, you need a shared memory, as well as a mutex mechanism to prevent race conditions.

+1
source

Processes, by definition, cannot directly modify the resources of other processes (such as global vars, etc.). For this you want to use threads, not processes. Look at pthread_create (3) or clone (2)

+1
source

The child process receives a copy of the address space of the parents. In other words, each child process has its own copy of global variables.

You need to put your variable in a shared memory mapping so that all child processes and the parent process have the same variable.

0
source

Source: https://habr.com/ru/post/1340049/


All Articles