exec function:
The child process calls the EXEC function to execute another program, and the EXEC function process is completely replaced by the new program, replacing the original program body, data, heap, and stack
#include <unistd.h>extern Char**environ;intEXECL (Const Char*path,Const Char*arg, ...);intEXECLP (Const Char*file,Const Char*arg, ...);intExecle (Const Char*path,Const Char*arg, ...,Char*Constenvp[]);intExecvConst Char*path,Char*Constargv[]);intEXECVP (Const Char*file,Char*Constargv[]);intExecve (Const Char*file,Char*ConstArgv[],Char*Constenvp[]); Exec function Error returned-1, success does not return
Execve are system functions, others are library functions
System functions
#include <stdlib.h>int system ( char *command) Return: Command state returned successfully, error returned -1 function: The simplify EXEC function constructs a child process inside the system function, Calling the EXEC function by a child process is equivalent to /bin/bash-c cmd or exec (" bash , " -C , " CMD )
#include <unistd.h><stdlib.h><stdio.h>char *cmd1="/bin /date"; int Main (void) { system ("clear"); System (CMD1); return 0 ;}
system function Source code
#include <sys/types.h>#include<sys/wait.h>#include<errno.h>#include<unistd.h>intSystemConst Char*cmdstring) {pid_t pid; intstatus; if(Cmdstring = =NULL) { return(1); } if(PID = fork ()) <0) {Status= -1; } Else if(PID =0) {execl ("/bin/sh","SH","- C", Cmdstring, (Char*)0); Exit (127); } Else{ while(Waitpid (PID, &status,0) <0){ if(errno! =einter) {Status= -1; Break; } } } returnstatus;}
The EXEC function of process and signal system function