How to create a certain number of child processes using fork()
I need to create a certain number of concurrent child processes. I also want each child process to modify a global variable so the main parent process can print it in its last modified version. When I run the program below, the final value for 'k' will be 5, so the global variable does not change. If I remove the "exit(0)" part, then the global variable changes but this time the number of child processes created gets bigger.
Using fork(), how would I create an X number of child processes that can modify the 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;
}
As Kevin commented, what you really want is threads. Doing IPC for this is overkill. Look at the following link.
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
You cannot do this in this manner. Fork will create a new process, which will copy (or copy-on-write) the memory pages into the new process. That means that each of your child processes will get their own copy of "k", and each copy of "k" will only get incremented once.
To have all the processes communicate back to the "same" k variable, you need to perform some sort of interprocess communications.
Examples of good inter-process communications:
Have the parent process create a shared memory segment which stores the value of k. Have the children processes wait for an exclusive grab of the shared memory segment (via a parent process created mutex). When the child has the exclusive lock, have the child read the value of k and store the value of k + 1.
Create a per-process pipe between the parent an children processes. Have the parent read the pipe for a message indicating the desire to increment k. Have the parent process then increment k on the child's behalf.
Examples of poor inter-process communications:
- Any solution which fails to ensure that changes in K are atomic (meaning that two contending children might both increment K to the same value). Such a lack of care would result in a value of K that appears to be incremented fewer times than the number of children processes, as the set values might look like (2, 3, 3, 4, 5). This means that file i/o is mostly useless, unless you create a framework around it that ensures atomic operations in locking the file for exclusive access.
Child processes cannot by default change anything in the parent's address space. You need shared memory for that, as well as a mutex mechanism to prevent race conditions.
Processes by definition can't modify resources of other processes (such as global vars, etc) directly. You want to use threads rather than processes for this. Look into pthread_create(3) or clone(2)
A child process gets a copy of parents address space. In other words, every child process has its own copy of global variables.
You need to put your variable in a shared memory mapping to allow all child processes and the parent process share the same variable.
精彩评论