int system(const char *cmdstring);
Example: system("date > file");
Typically, a system is implemented by calling fork, exec, and waitpid ; there are three types of return values.
- If either fork fails or waitpid returns an error other than EINTR, the system returns -1 with errno to indicate an error.
- If exec does not work, assuming that the shell cannot be executed, the return value will be as if the shell had executed exit (127).
- Otherwise, all three functions-fork, exec and waitpid-success and the return value from the system are the shell completion status, in the format specified for waitpid.
The fork function is designed to create a new process (child), which then calls another program that calls one of the exec functions. When a process calls one of exec, this process is completely replaced by the new program, and the new program starts execution according to its main function. The process identifier does not change throughout exec because a new process is not created; Exec simply replaces the current process โ its text, data, heap, and stack segments โ with a completely new program from disk.
There are six different exec functions ,
int execl(const char *pathname, const char *arg0, ... ); int execv(const char *pathname, char *const argv []); int execle(const char *pathname, const char *arg0, ... ); int execve(const char *pathname, char *const argv[], char *const envp []); int execlp(const char *filename, const char *arg0,... ); int execvp(const char *filename, char *const argv []);
Madhavan G Sep 19 '16 at 9:02 2016-09-19 09:02
source share