Code sample

The following code is used to create two child processing tasks. The basic code framework shows how to use fork to create explicit child processing tasks.

int groupCount = 2; char *pTaskGroup[2]; pid_t pid = -1; for (int i = 0; i < groupCount; ++i) { pid = fork(); LogInfo("pid --------------11 [%d]", pid); if (pid == 0 || pid == -1) { LogInfo("This is children pid 1 [%d], ..." , getpid()); // If the child executes the code after the loop, avoid the child to execute the loop and fork() the child break; } else // parent process { for (int i = 0; i < groupCount; I++) {if (pTaskGroup [I] - > m_TaskPid = = 0) {/ / current in the parent process, variable pid said child process pid LogInfo (" pid -- -- -- -- -- -- -- -- -- -- -- -- -- -- 22 [% d] ", pid); PTaskGroup [I]->m_TaskPid = pid; break; } } } } if (pid == -1) { LogError("[%s] Fail to fork! \n", __func__); exit(0); } else if (pid == 0) // LogInfo("This is children pid 2 [%d],... , getpid()); startTask( ); } else // Parent process {for (int I = 0; i < groupCount; i++) { LogInfo("pid [%d]-------------- [%d]", i, pTaskGroup->m_TaskGroup[i]->m_TaskPid); } // visionLoop(); }Copy the code

Execution Result

[lv:2]: pid --------------11 [12234] <2019-03-21_17:34:20><.. /src/main.c:143> [lv:2]: pid --------------22 [12234] <2019-03-21_17:34:20><.. /src/main.c:157> [lv:2]: pid --------------11 [0] <2019-03-21_17:34:20><.. /src/main.c:143> [lv:2]: This is children pid 1 [12234], ... < 2019-03-21 _17: all > <.. /src/main.c:146> [lv:2]: This is children pid 2 [12234], ... < 2019-03-21 _17: all > <.. /src/main.c:172> [lv:2]: pid --------------11 [12235] <2019-03-21_17:34:20><.. /src/main.c:143> [lv:2]: pid --------------22 [12235] <2019-03-21_17:34:20><.. /src/main.c:157> [lv:2]: pid [0]-------------- [12234] <2019-03-21_17:34:20><.. /src/main.c:183> [lv:2]: pid --------------11 [0] <2019-03-21_17:34:20><.. /src/main.c:143> [lv:2]: pid [1]-------------- [12235] <2019-03-21_17:34:20><.. /src/main.c:183> [lv:2]: This is children pid 1 [12235], ... < 2019-03-21 _17: all > <.. /src/main.c:146> [lv:2]: This is children pid 2 [12235], ... < 2019-03-21 _17: all > <.. /src/main.c:172>Copy the code

Pid = fork(); After the child process has been created, the child process assigns all data to the master process, and both parent and child processes continue backward at the same time. Based on the log information, you can know that the parent process executes first

// The parent process prints the id of the child process saved by the pid variable in sequence, and the else part of the parent process is printed in the second line below. [lv:2]: pid --------------11 [12234] <2019-03-21_17:34:20><.. /src/main.c:143> [lv:2]: pid --------------22 [12234] <2019-03-21_17:34:20><.. /src/main.c:157>Copy the code

Then the child process starts executing

[lv:2]: pid --------------11 [0] <2019-03-21_17:34:20><.. /src/main.c:143> [lv:2]: This is children pid 1 [12234], ... < 2019-03-21 _17: all > <.. /src/main.c:146> [lv:2]: This is children pid 2 [12234], ... < 2019-03-21 _17: all > <.. /src/main.c:172>Copy the code

Since fork() is in the for loop, the child will execute the for loop if it does not exit the loop, which in turn will create the child, which is not what we intended to create in the first place. Hence the following code.

if (pid == 0 || pid == -1) { LogInfo("This is children pid 1 [%d], ..." , getpid()); // If the child executes the code after the loop, avoid the child to execute the loop and fork() the child break; }Copy the code

A second child process is then created in the same order and method as the first. After the main process has finished creating the child in the loop, it exits the loop and prints the PID of the created child in the main process.

[lv:2]: pid [0]-------------- [12234] <2019-03-21_17:34:20><.. /src/main.c:183> [lv:2]: pid [1]-------------- [12235] <2019-03-21_17:34:20><.. /src/main.c:183>Copy the code