#include <spawn.h> pid_t spawn( const char *path, const int fd_count, const int fd_map[], const struct inheritance *inherit, char * const argv[], char * const envp[]);
The spawn() function creates a child process that inherits specific attributes from the parent. The attributes inherited by the child process are file descriptors, the signal mask, the signal action vector, and environment variables, among others.
See QlgSpawn()--Spawn Process (using NLS-enabled path name) for a description and an example of supplying the path in any CCSID.
struct inheritance { flagset_t flags; int pgroup; sigset_t sigmask; sigset_t sigdefault; };
The flags field specifies the manner in which the child process should be created. Only the constants defined in <spawn.h> are allowed; otherwise, spawn() returns -1 with errno set to EINVAL. The allowed constants follow:
SPAWN_SETPGROUP | If this flag is set ON, spawn()
sets the process group ID of the child process to the value in
pgroup. In this case, the process group field, pgroup, must
be valid. If it is not valid, an error occurs. If this flag is set OFF, the
pgroup field is checked to determine what the process group ID of
the child process is set to. If the pgroup field is set to the
constant SPAWN_NEWPGROUP, the child process group ID is set to the child
process ID. If the pgroup field is not set to SPAWN_NEWPGROUP and
the flags field is not set to SPAWN_SETPGROUP, the process group
ID of the child process is set to the process group ID of the parent process.
If the pgroup field is set to SPAWN_NEWPGROUP and the
flags field is set to SPAWN_SETPGROUP, an error occurs. |
SPAWN_SETSIGMASK | If this flag is set ON, spawn()
sets the signal blocking mask of the child process to the value in
sigmask. In this case, the signal blocking mask must be valid. If it is
not valid, an error occurs. If this flag is set OFF, spawn()
sets the signal blocking mask of the child process to the signal blocking mask
of the calling thread. |
SPAWN_SETSIGDEF | If this flag is set ON, spawn()
sets the child process' signals identified in sigdefault to the
default actions. The sigdefault must be valid. If it is not valid,
an error occurs. If this flag is set OFF, spawn() sets the
child process' signal actions to those of the parent process. Any signals of
the parent process that have a catcher specified are set to default in the
child process. The child process' signal actions inherit the parent process'
ignore and default signal actions. |
SPAWN_SETTHREAD_NP | If this flag is set ON, spawn() will create the child process as multithread capable. The child process will be allowed to create threads. If this flag is set OFF, the child process will not be allowed to create threads. |
SPAWN_SETPJ_NP | If this flag is set ON, spawn()
attempts to use available i5/OS prestart jobs. The prestart job entries that
may be used follow:
The i5/OS prestart jobs must have been started using either QSYS/QP0ZSPWP or QSYS/QP0ZSPWT as the program that identifies a prestart job entry for the i5/OS subsystem that the parent process is running under. If a prestart job entry is not defined, the child process will run as a batch immediate job under the same subsystem as the parent process. If this flag (SPAWN_SETPJ_NP) is set OFF, the child process will run as a batch immediate job under the same subsystem as the parent process. Note:In order to more closely emulate POSIX semantics, spawn() will ignore the Maximum number of uses (MAXUSE) value specified for the prestart job entry. The prestart job will only be used once, behaving as if MAXUSE(1) was specified. |
SPAWN_SETCOMPMSG_NP | If this flag is set ON, spawn() causes the child process to send a completion message to the user's message queue when the child process ends. If this flag is set OFF, no completion message is sent to the user's message queue when the child process ends. If both the SPAWN_SETCOMPMSG_NP and SPAWN_SETPJ_NP flags are set ON, an error occurs. |
SPAWN_SETJOBNAMEPARENT_NP | If this flag is set ON, spawn() sets the child's i5/OS simple job name to that of the parent's. If this flag is set OFF, spawn() sets the child's i5/OS simple job name based on the path parameter. |
SPAWN_SETJOBNAMEARGV_NP | If this flag is set ON, spawn() sets the child's i5/OS simple job name to the name found in argv[0] of the argv[] parameter. If this flag is set OFF, spawn() sets the child's i5/OS simple job name based on the path parameter. |
SPAWN_SETLOGJOBMSGABN_NP | If this flag is set ON, the child process does not log the job started (CPF1124) message and will only log the job ended (CPF1164) message when the job ends abnormally (job ending code 30 or greater). This applies to the job log as well as the system history log (QHST). If this flag is set OFF, the child process logs the job started (CPF1124) and the job ended (CPF1164) messages. |
SPAWN_SETLOGJOBMSGNONE_NP | If this flag is set ON, the child process does not log the job started (CPF1124) and the job ended (CPF1164) messages to either the job log or to the system history log (QHST). If this flag is set OFF, the child process logs the job started (CPF1124) and the job ended (CPF1164) messages. |
SPAWN_SETAFFINITYID_NP | If this flag is set ON, spawn()
sets the resources affinity identifier of the child process to the 4-byte
integer value, treated as an unsigned int, that immediately
follows the inheritance structure in memory. A value of 0
indicates i5/OS selects the resources affinity identifier. The
caller must ensure the value immediately follows the inheritance
structure. For example:
struct extended_inheritance { struct inheritance inherit; unsigned int affinityID; }; struct extended_inheritance extended_inherit; If this flag is set OFF, i5/OS selects the resources affinity identifier of the child process. |
SPAWN_SETTHREADRUNPTY_NP | If this flag is set ON, spawn() sets the run priority of the child process to the current thread's run priority. If this flag is set OFF, spawn() sets the run priority of the child process to the current process' run priority. |
Authorization Required for spawn()
Object Referred to | Authority Required |
errno |
---|---|---|
Each directory in the path name preceding the executable file that will run in the new process | *X | EACCES |
Executable file that will run in the new process | *X | EACCES |
If executable file that will run in the new process is a shell script | *RX | EACCES |
value | spawn() was successful. The value returned is the process ID of the child process. |
-1 | spawn() was not successful. The errno variable is set to indicate the error. |
If spawn() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Argument list too long.
Permission denied.
An attempt was made to access an object in a way forbidden by its object access permissions.
The thread does not have access to the specified file, directory, component, or path.
If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.
Possible APAR condition or hardware failure.
Function parameter in the signal function is not set.
A given file descriptor or directory pointer is not valid for this operation. The specified descriptor is incorrect, or does not refer to an open file.
The object name specified is not correct.
Operation canceled.
Conversion error.
One or more characters could not be converted from the source CCSID to the target CCSID.
The specified path name is not in the CCSID of the job.
The address used for an argument is not correct.
In attempting to use an argument in a call, the system detected an address that is not valid.
While attempting to access a parameter passed to this function, the system detected an address that is not valid.
The value specified for the argument is not correct.
A function was passed incorrect argument values, or an operation was attempted on an object and the operation specified is not supported for that type of object.
An argument value is not valid, out of range, or NULL.
The flags field in the inherit parameter contains an invalid value.
Input/output error.
A physical I/O error occurred.
A referenced object may be damaged.
A loop exists in the symbolic links.
This error is issued if the number of symbolic links encountered is more than POSIX_SYMLOOP (defined in the limits.h header file). Symbolic links are encountered during resolution of the directory or path name.
A path name is too long.
A path name is longer than PATH_MAX characters or some component of the name is longer than NAME_MAX characters while _POSIX_NO_TRUNC is in effect. For symbolic links, the length of the name string substituted for a symbolic link exceeds PATH_MAX. The PATH_MAX and NAME_MAX values can be determined using the pathconf() function.
Too many open files in the system.
A system limit has been reached for the number of files that are allowed to be concurrently open in the system.
The entire system has too many other file descriptors already open.
No such path or directory.
The directory or a component of the path name specified does not exist.
A named file or directory does not exist or is an empty string.
Storage allocation request failed.
A function needed to allocate storage, but no storage is available.
There is not enough memory to perform the requested function.
Not a directory.
A component of the specified path name existed, but it was not a directory when a directory was expected.
Some component of the path name is not a directory, or is an empty string.
Function is not allowed in a job that is running with multiple threads.
Operation not supported.
The operation, though supported in general, is not supported for the requested object or the requested arguments.
Operation terminated.
System resources not available to complete request.
The child process failed to start. The maximum active jobs in a subsystem may have been reached. CHGSBSD and CHGJOBQE CL commands can be used to change the maximum active jobs.
Unknown system state.
The operation failed because of an unknown system state. See any messages in the job log and correct any errors that are indicated, then retry the operation.
/QSYS.LIB/MYLIB.LIB/MYPROG.PGM
See QlgSpawn()--Spawn Process (using NLS-enabled path name) for an example specifying the program using the Qlg_Path_Name_T structure. The Qlg_Path_Name_T structure is supported by QlgSpawn() and allows the program name to be specified in any CCSID.
Note: For more information about path syntaxes for the different file systems, see the Integrated file system information in the Files and file systems topic.
The child process has the same user profile as the calling thread. However, the i5/OS job attributes come from the job description specified for the prestart job entry, and the run attributes come from the class that is associated with the i5/OS subsystem used for the prestart job entry.
Notes:
ADDPJE SBSD(QSYS/QINTER) PGM(QSYS/QP0ZSPWP) INLJOBS(20) THRESHOLD(5) ADLJOBS(5) JOBD(QGPL/QDFTJOBD) MAXUSE(1) CLS(QGPL/QINTER)
ADDPJE SBSD(QSYS/QINTER) PGM(QSYS/QP0ZSPWT) INLJOBS(20) THRESHOLD(5) ADLJOBS(5) JOBD(QSYS/QAMTJOBD) MAXUSE(1) CLS(QGPL/QINTER)
Refer to the Work Management topic for complete details on prestart jobs.
#!interpreter_path <options>
where interpreter_path is the path to the shell interpreter program.
If the calling process is multithreaded, path (the first parameter to spawn()) must reference a threadsafe file system.
spawn() calls the shell interpreter, passing in the shell options and the arguments passed in as a parameter to spawn(). The argument list passed into the shell interpreter will look like Arguments to Shell Interpreter.
Arguments to Shell Interpreter
See About Shell Scripts for an example using spawn() and shell scripts.
int main(int argc, char *argv[]) { }
where the following are true:
The maximum number of arguments that can be specified is dependent on the following:
Note: If the user of spawn() specifies the extern variable environ as the envp[] parameter, the user must successfully call one of the following APIs before calling spawn():
The extern variable environ is not initialized until one of these APIs is called in the current activation group. If environ is used in a call to spawn() without first calling one of these APIs, spawn() returns an error.
Users and applications can enable descriptor-based standard I/O for child processes by setting environment variable QIBM_USE_DESCRIPTOR_STDIO to the value Y in the child process. This can be accomplished on the call to spawn() by either of the following:
The environment variable can be set by using one of the following:
If you enable descriptor-based standard I/O for child processes, file descriptors 0, 1, and 2 are automatically used for stdin, stdout, and stderr, respectively. However, spawn() must be called using a fd_map that has file descriptors 0, 1, and 2 properly allocated. See About Shell Scripts for an example that enables descriptor-based standard I/O for a child process. Refer to the WebSphere Development Studio: ILE C/C++ Programmer's Guide for complete details on this support.
To help the user start a debug session (when spawn() is the mechanism used to start the process), the user sets the environment variable QIBM_CHILD_JOB_SNDINQMSG.
If the environment variable is assigned a numerical value, it indicates the number of descendent levels that will be enabled for debugging. This support can be used to debug applications that create children, grandchildren, great-grandchildren, and so forth. When the environment variable has a value of 1, it enables debugging of all subsequent child processes. A value of 2 enables debugging of all subsequent child processes and grandchild processes.
When the environment variable has a value less than or equal to 0, or any non-numerical value, debugging will not occur.
Here are the steps a user would take to debug an application by using spawn():
Assume the user wants to debug child processes in an application called CHILDAPP found in library MYAPPLIB.
The environment variable can be set by using one of the following:
Note: Alternatively, a Display Messages (DSPMSG) command can be issued for the user, and the output searched for the specific CPAA980 *INQUIRY message.
Note: If the job's inquiry message reply specifies using the default message reply, the child process will not block since the default reply for the CPAA980 *INQUIRY message is G.
Note: If you reply with C to the CPAA980 message, the child process is ended before the child process' program ever receives control. In this case, on a subsequent call to wait() or waitpid(), the status information returned indicates WIFEXCEPTION(), which evaluates to a nonzero value, and WEXCEPTNUMBER() will evaluate to 0.
If SPAWN_SETJOBNAMEPARENT_NP is set in inherit.flags, the child's i5/OS simple job name would be the same as the parent's i5/OS simple job name.
If SPAWN_SETJOBNAMEARGV_NP is set in inherit.flags, the null-terminated character string represented by argv[0] of the argv[] parameter will be used for the child's i5/OS simple job name. The name can be up to 10 characters, must be uppercase, and must contain only those characters allowed for an i5/OS job name. For example, if path was set to /QSYS.LIB/MYLIB.LIB/CHILD.PGM, SPAWN_SETJOBNAMEARGV_NP was set ON, and argv[0] was set to SON, the child's i5/OS simple job name would be SON. If the first character of the name is invalid, the path will be used. If any of the remaining characters of the name are invalid, the valid characters up to that point will be used.
The child process inherits the following POSIX attributes from the parent:
Note: The following file descriptor table information does not apply to any of the scan descriptors which are created by the thread executing one of the scan-related exit programs (or any of its created threads). See Integrated File System Scan on Open Exit Programs and Integrated File System Scan on Close Exit Programs for more information.
Note: File descriptors that have the FD_CLOEXEC file descriptor flag set are not inherited. File descriptors that are created as a result of the opendir() API (to implement open directory streams) are not inherited.
Note: File descriptors that are specified in the fd_map array are inherited even if they have the FD_CLOEXEC file descriptor flag set. After inheritance, the FD_CLOEXEC flag in the child process' file descriptor is cleared.
Only open files managed by the Root, QOpenSys, or user-defined file systems support inheritance of their file descriptors. All other file systems will have their file descriptors closed in the child process.
Note: i5/OS does not support the ability to set the process group ID for the child process to a user-specified group ID. This is a deviation from the POSIX standard.
If the process group that the child process is attempting to join has received the SIGKILL signal, the child process is ended.
Note: i5/OS prestart jobs do not inherit priority.
Note: i5/OS prestart jobs inherit a subset of i5/OS-specific attributes.
See Code disclaimer information for information pertaining to code examples.
For an example of using this function, see Using the Spawn Process and Wait for Child Process APIs in the API Examples.
Top | UNIX-Type APIs | APIs by category |