Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

system - execute system command

&pagelevel(4)&pagelevel

Description

Description system() passes the system command given in the string command to a command interpreter for execution. Depending on which functionality is selected, command is interpreted as a POSIX or BS2000 command (see section "Selecting functionality").

If command is a POSIX command, the environment of the executed command will be as if a child process were created using fork() , and the child process invoked the sh command using execl() as follows:

execl( shell_path , "sh", "-c", command , (char *)0);

where shell_path must be replaced by the pathname of the sh command.

system() will not return until the child process has terminated and will not affect its termination status.

BS2000
If command is a BS2000 command, it will be executed in the same task in which the program that invokes system() is running. Note that if programs or procedures are started in the system call, the calling program will be unloaded (see "Notes"). (End)

Return val.

Exit status of the command interpreter



if command is not a null pointer and the command was successfully executed. The exit status of the command interpreter is returned in the format specified by waitpid(). It corresponds to the exit status of the sh command, except that if some error prevents the command interpreter from executing after the child process is created, the return value from system() will be as if the command interpreter had terminated using exit(127) or _exit(127).


!= 0

if command is a null pointer and a command interpreter exists.

 

-1  

if a child process cannot be created or if the command interpreter has no exit status. errno is set to indicate the error.


BS2000



0

if command was executed successfully (return value of the BS2000 command: 0).


-1

if the BS2000 command was not executed successfully (return value of the command: error code != 0).


undefined

if control is not returned to the program following the BS2000 command (see "Notes"). (End)

Errors

system() will fail if:


EAGAIN

The system does not have the resources required to create a further process or the system-specific limit for the maximum number of simultaneously executing processes for the system or an individual user ID {CHILD_MAX} would be exceeded.


Extension


EINTR

system() was interrupted by a signal.


ENOMEM

Not enough memory is available.

Notes

If the return value of system() is not -1, its value can be decoded by using the macros that are defined in both sys/wait.h as well as stdlib.h.

The following function can be used to determine whether or not an XPG4-conformant environment is present: sysconf(_SC_2_VERSION).

Note that, while system() must ignore SIGINT and SIGQUIT and block SIGCHLD while waiting for the child to terminate, the handling of signals in the executed command is as specified by fork() and exec. For example, if SIGINT is being caught or is set to SIG_DFL when system() is called, then the child will be started with SIGINT handling set to SIG_DFL.

Ignoring SIGINT and SIGQUIT in the parent process prevents coordination problems (two processes reading from the same terminal, for example) when the executed command ignores or catches one of the signals. It is also usually the correct action when the user has given a command to the application to be executed synchronously (as in the "!" command in many interactive applications). In either case, the signal should be delivered only to the child process, not to the application itself. There is one situation where ignoring the signals might have less than the desired effect. This is when the application uses system() to perform some task invisible to the user. If the user typed the interrupt character (^C, for example) while system() is being used in this way, one would expect the application to be killed, but only the executed command will be killed. Applications that use system() in this way should carefully check the return status from system() to see if the executed command was successful, and should take appropriate action when the command fails.

Blocking SIGCHLD while waiting for the child to terminate prevents the application from catching the signal and obtaining status from system() ́s child process before system() can get the status itself.

The context in which the command is ultimately executed may differ from that in which system() was called. For example, when file descriptors that have the FD_CLOEXEC flag set are closed, the process ID and parent process ID of system() and the command will be different. Furthermore, if the executed command changes its environment variables or its current working directory, that change will not be reflected in the caller ́s context.

sh may not be available following a call to chroot.

There is no defined way for an application to find the specific path for the shell. However, confstr() can provide a value for PATH that is guaranteed to find the sh command.

BS2000
The BS2000 command must not exceed a maximum length of 2048 characters and need not be specified with the system slash (/).

In the case of some BS2000 commands (e.g. START-PROG, LOAD-PROG, CALL-PROCEDURE, DO, HELP-SDF), control is not returned to the calling program after they are called. Programs that permit premature terminations should therefore flush all buffers (fflush()) and/or close files before the system call.

system() passes the command string as input to the BS2000 command processor MCLP without any changes (see also the manual "Executive Macros" [10]). No conversion to uppercase is performed.


See also

bs2system(), exec, fork(), pipe(), sysconf(), wait(), limits.h, signal.h, stdio.h, and the command sh in the manual "POSIX Commands" [2].