Parent and Child without IPC

In this scenario, the parent process creates the child, but there is no subsequent communication between the two (except maybe the parent waiting for termination of the child via use of the waitpid() function).

Rather than use the fork() / exec() combination to achieve this the posix_spawn() API can be used instead. This API will create a process by a single API call. Additionally, actions can be performed on inherited file descriptors to change access before the child's main is called. For more detailed information on the posix_spawn() function consult the relevant Open Group standard pages available at http:\\www.opengroup.org .

Parent process fork() and exec() functions

The following code shows how the creation of a child process can be implemented using the fork() and exec() functions. Note that inclusion of header files has been omitted for clarity.

      int main(int argc, char *argv[])
{
  pid_t Childpid = fork();
     
  if(Childpid == 0)
  {
      //Running in Child process

      //exec() to replace the child process executable
      char execFileName[] = "/root/PortDoc/Example0_c/Posix/child/ChildProg";

      execl(execFileName,NULL);
  }
  else
  {
     //Running in parent process

     //Wait for the child process to terminate
     waitpid(Childpid,NULL,0);
     
     printf("\r\n*** Child process finished ***\r\n");
     
  }
   
  return EXIT_SUCCESS;
}
     

Using the posix_spawn() function

The following code shows how the above can be modified to use the posix_spawn() operation.

      int main(int argc, char *argv[])
{
   pid_t Childpid;
   char execFileName[] = "/root/PortDoc/Example0_c/Symbian/child/ChildProg";
   
   int RetVal= posix_spawn(&Childpid,execFileName,NULL,NULL,NULL,NULL);
   
   (void)waitpid(Childpid,NULL,0);
   
   printf("\r\n*** Child process finished ***\r\n");
    
   return EXIT_SUCCESS;
}
     

Child process example

The child code shown is unaffected by the mechanism used to spawn it, that is, fork() / exec() or posix_spawn() , but an example is shown below for completeness.

      int main(void)
{
    printf("\r\n*** Child Running ***\r\n");
    
    return EXIT_SUCCESS;
}