1. system functions
# Include <stdlib. h>
INT system (const char * cmd );
If CMD is a null pointer, system returns a non-zero value only when the command handler is available.
Because system calls fork, exec, and waitpid in its implementation, there are three return values:
1) if fork fails or waitpid returns an error other than eintr, system returns-1 and errno sets the error type.
2). If exec fails (the shell cannot be executed), the return value is the same as that when the shell executes exit (127.
3). Otherwise, all three functions (fork, exec, and waitpid) are successfully executed, and the return value of system is the termination state of shell. The format is described in waitpid.
If a process is running with special permissions (Set User ID and set group ID) and it wants to generate another process to execute another program, it should directly use fork and exec, in addition, after fork, the general permission should be changed back before exec. The User ID setting and the group ID program should not call the system function, because special permissions will be held after fork and exec are executed in the system.
1.1. system function prototype (no signal processing)
The shell-C option tells the shell program to take the next command line parameter as the command input (instead of reading the command from the standard input or from a given file ). Shell parses the command strings terminated with null characters and divides them into command line parameters.
If we try to execute this command by ourselves instead of using shell, execlp instead of execl must be used to use the PATH variable like shell, the command string ending with null must be divided into command line parameters to call execlp. Finally, we cannot use any shell metacharacters.
Note that we call _ exit instead of exit, which prevents any standard I/O buffer (These buffers are copied from the parent process to the child process) from being flushed in the child process.
# Include <errno. h> <br/> # include <unistd. h> <br/> # include <sys/Wait. h> </P> <p> INT system (const char * command) <br/>{< br/> pid_t PID; <br/> int status; </P> <p> If (null = command) <br/>{< br/> return (1 ); <br/>}< br/> If (pid = fork () <0) <br/>{< br/> Status =-1; <br/>}< br/> else if (pid = 0) <br/>{< br/> execl ("/bin/sh", "sh ", "-c", command, (char *) 0); <br/> _ exit (127 ); <br/>}< br/> else <br/> {<br/> while (waitpid (PID, & status, 0) <0) <br/>{< br/> If (errno = eintr) <br/>{< br/> Status =-1; <br/> break; <br/>}< br/> return status; <br/>}
1.2. Complete system function prototype
# Include <sys/types. h> <br/> # include <signal. h> <br/> # include <stdlib. h> <br/> # include <unistd. h> <br/> # include <paths. h> <br/> # include <sys/Wait. h> </P> <p> extern char ** environ; </P> <p> INT system (const char * command) <br/>{ <br/> pid_t PID; <br/> sig_t intsave, quitsave; <br/> sigset_t mask, omask; <br/> int pstat; <br/> char * argp [] = {"sh", "-c", null, null}; </P> <p>/* just checking... */<br/> If (! Command) <br/>{< br/> return (1); <br/>}</P> <p> argp [2] = (char *) command; <br/> sigemptyset (& Mask); <br/> sigaddset (& Mask, sigchld); <br/> sigprocmask (sig_block, & Mask, & omask ); <br/> switch (pid = vfork () <br/> {<br/> case-1:/* Error */<br/> sigprocmask (sig_setmask, & omask, null); <br/> return (-1); <br/> case 0:/* child */<br/> sigprocmask (sig_setmask, & omask, null); <br/> execve (_ path_bs Hell, argp, Environ); <br/> _ exit (127); <br/>}</P> <p> intsave = (sig_t) bsd_signal (SIGINT, sig_ign); <br/> quitsave = (sig_t) bsd_signal (sigquit, sig_ign); <br/> pid = waitpid (PID, (int *) & pstat, 0 ); <br/> sigprocmask (sig_setmask, & omask, null); <br/> (void) bsd_signal (SIGINT, intsave); <br/> (void) bsd_signal (sigquit, quitsave); </P> <p> return (pid =-1? -1: pstat); <br/>}