Multi-Process Compilation Specifications

Avoiding Parent-Child Process Nesting

Distinguish the responsibilities of parent and child processes in advance. A parent process creates child processes, while child processes are responsible for scheduling and inference. This avoids resource preemption caused by simultaneous scheduling and inference performed by parent and child processes. In practice, perform model inference in the returned parent and child processes after fork() is executed. Figure 1 shows the program logic.

Figure 1 Avoiding parent-child process nesting

Using Different Devices to Execute an Inference Job

If the parent and child processes need to be nested, specify different devices for executing an inference job. This prevents the parent and child processes from performing inference on the same device at the same time. Figure 2 and Figure 3 show the program logic.

Figure 2 Specifying different devices for inference
Figure 3 Setting the same device ID for the created child processes

Calling the Child Process and then Parent Process for Inference

If the service requires nesting of parent and child processes, you are advised to call the child process first to perform inference and then call the parent process. However, the calling logic of this solution is difficult to understand. Therefore, this solution is not recommended. Figure 4 shows the program logic.

Figure 4 Calling the child process and then parent process for inference

Sample Code

int main(){
  // The func() function represents a complete inference job.
  // The following is the recommended multi-process calling mode. Perform inference in the returned parent and child processes after fork() is executed.
  pid_t pid;
  int i;
  for (i = 0; i < 5; ++i)
  {
     pid = fork(); // Create a child process.
     if (pid == -1)
     {
        perror("fork failed!");
        exit(1);
     }
     if (pid==0)
     {
        break;
     }
  }
  // The process whose value is greater than 0 is the parent process.
  if(pid>0)  // Parent process
  {
     printf("Parent process: pid= %d , ppid=%d,child process: %d \n", getpid(),getppid(),pid);
     func(); // Perform inference after fork() is executed.
     sleep(1); // Call the parent process after the child process is executed.
  }
  else if(pid == 0)  // Child process
  {  
     func(); // Perform inference after fork() is executed.
     printf("Variable i: %d child process: pid= %d , ppid=%d \n", i,getpid(),getppid());
  }
  return 0;
}