Monday, June 6, 2011

Converting fork() and exec() Usage to spawn()Converting fork() and exec() Usage to spawn()

The spawn() function provides a fast, low-overhead mechanism for creating a new POSIX process to run a new program. This is the typical usage of the POSIX.1 fork() function. OpenExtensions includes the POSIX.1d spawn() definition, which was included in the standard to handle the following operations in one function:

1. Create a new process.
2. Perform operations typically done in the new process to prepare to run a new program. This includes file descriptor mapping, changing process group membership, job control, and altering the signal handling environment.
3. Invoke the new program through exec().

To convert an application from using fork() and exec() to using spawn(), the following steps should be followed:

1. Replace the call to fork() with a call to spawn(), using the program name and program parameters from the exec() call.
2. Delete the call to exec().
3. Determine the other parameters to spawn() by examining the calls made between the fork() and the subsequent exec() to change the environment for the new program:
* Calls to dup2() should be replaced by entries in the file descriptor array.
* The mask value in any sigmask() calls should be used in the signal mask member of the inheritance structure.
* Signals whose actions are defaulted through sigaction() calls should be included in the sigdefault member of the inheritance structure.
* A call to setpgid() should be replaced by an entry in the process group member of the inheritance structure.
* A call to tcsetpgrp should be replaced by an entry in the inheritance structure.

No comments:

Post a Comment