问题描述:

I'm using this to set up a process in my main:

 pid = fork();

if (pid == 0)

ChildProcess();

else

ParentProcess();

How would I go about setting more processes, say for example x4 of them?

网友答案:

If you're in the parent process, call fork() again to get another child. If you put it in a loop then you're golden.

for (int i = 0; i < 4; ++i) {
    pids[i] = fork();
    if (pids[i] == 0) {
        ChildProcess();
        break;
    }
}

Make sure the child does not call fork(). Only the parent.

网友答案:

There is some ways to do this.

For example:
1. just repeate your fork() for 4 times.
2. use a loop to call fork() for 4 times.
3. wrapps those into a functions

相关阅读:
Top