several ways to terminate a process
(1) Normal exit
return from main function [Return]
Call exit
Call _exit/_exit
(2) Abnormal exit
call Abort to generate Sigabout signal
terminated by signal CTRL + C [SIGINT]
... (not completely, such as return/pthread_exit, etc.)
Test [Exit/_exit]
Try to view the printout of the program int main () { cout << "in main, pid =" << getpid (); Removed the Endl;/ /principle: associated with the terminal, stdout for the row buffer, in the file, for full buffering; //For more information, refer to "Advanced Programming for UNIX Environment" (third edition) section 8.5, P188 //exit (0); For C library functions, Explained in detail below _exit (0);}
The graph shows that the system call _exit directly into the kernel, and the C library function is through a series of system cleanup work, and then call the Linux kernel;
int main () { cout << "in main, pid =" << getpid (); Fflush (stdout);//increased refresh buffer work _exit (0);}
Summary: Exit and _exit differences
1) _exit is a system call, Exit is a C library function
2)exit performs an I/O cache cleanup
3) exit executes the call termination handler//termination handler as follows
Termination handler: Atexit
#include <stdlib.h>int atexit (void (*function) (void));
Test void ExitHandler1 (void) { cout << "If exit with Exit, the function Exithandler'll be called1" << Endl ;} void ExitHandler2 (void) { cout << "If exit with Exit, the function Exithandler would be called2" << Endl;} int main () { cout << "in main, pid =" << getpid () << Endl; Atexit (exitHandler1);//Note that the first registered post-execution atexit (exitHandler2); Exit (0);}
abnormal termination
int main () { cout << "in main, pid =" << getpid () << Endl; Atexit (exitHandler1); Atexit (exitHandler2); Abort (); Exit (0);}
exec function Family
EXEC Replacement Process Impressions
on the creation of a process, UNIX takes a unique approach, which separates the process creation from the loading of a new process image . The advantage is that there is more room to manage the two operations.
When we create a process, we usually replace the child process with a new process image, which can be done using the EXEC series function. Of course, the Exec series functions can also replace the current process.
exec simply replaces the current process's body segment, data segment, heap segment, and stack segment with a new program on the disk.
Function Family Information
#include <unistd.h>int execve (const char *filename, char *const argv[], char *const envp[]); #include < Unistd.h>extern Char **environ;int execl (const char *path, const char *arg, ...); int EXECLP (const char *file, const char *arg, ...); int execle (const char *path, const char *arg, ..., char * const envp[]); int execv (const char *path, char *const argv[] int EXECVP (const char *file, char *const argv[]); int execvpe (const char *file, char *const argv[], char *const envp[ ]);
Description
The number of arguments for execl,execlp,execle (all with "L", which represents the list) is Variable , and the parameter must end with a null pointer .
The second parameter of EXECV and EXECVP is an array of strings ("V" stands for "vector", the string array must be null-terminated), and the new program will pass the parameters given in the argv array to main at startup.
The last name of the "P" function searches the PATH environment variable to find the executable file for the new program. If the executable file is not on path definition, the absolute file name including subdirectories must be passed as a parameter to these functions;
/* Summary: L represents a mutable parameter list, and p represents the search for a file in the PATH environment variable. ENVP represents environment variable */
Example Execlpint Main () { pid_t pid = fork (); if (PID = = 0) { if (EXECLP ("/bin/pwd", "pwd", NULL) = =-1) err_exit ("EXECLP pwd error"); } Wait (NULL); PID = fork (); if (PID = = 0) { if (EXECLP ("/bin/ls", "ls", "-l", NULL) = =-1) err_exit ("EXECLP ls-l error"); } Wait (NULL); cout << "After EXECLP" << Endl;}
Example Execveint Main () { char *const args[] = { (char *) "/bin/date", (char *) "+%f", NULL }; Execve ("/bin/date", args,null); cout << "after fork ..." << Endl; return 0;}
Example Execle//1:main.cppint Main () { cout << "in main, pid =" << getpid () << Endl; Char *const environ[] = { "aa=11", "bb=22", "cc=33", NULL }; Execle ("./hello", "./hello", null,environ);//When environ is filled with NULL, nothing is passed cout << "after fork ..." << Endl ; return 0;}
extern char **environ;int main () { cout << "in Hello, pid =" << getpid () << Endl; cout << "environ:" << Endl; for (int i = 0; Environ[i]! = NULL; ++i) { cout << "\ T" << Environ[i] << Endl; }}
/*in Main, PID = 3572//pid remains constant in Hello, pid = 3572environ:aa=11bb=22cc=33*/
//Example: Execve With Execlpint Main () {pid_t pid = fork (); if (PID = =-1) err_exit ("fork Error"); else if (PID = = 0) {//example Execve char *const args[] = {"Echoall", "Myarg1", "MY ARG2", NULL}; Char *const env[] = {"User=unknown", "Path=/tmp", NULL}; Execve ("./echoall", args,env); } wait (NULL); PID = fork (); if (PID = =-1) err_exit ("fork Error"); else if (PID = = 0) {//Example EXECLP EXECLP ("./echoall", "Echoall", "Only one arg", NULL); } wait (NULL); return 0;} Echoallint Main (int argc, char *argv[]) {for (int i = 0; i < argc; ++i) printf ("argv[%d]:%s\t", I, argv[i ]); printf ("\ n"); for (char **ptr = environ; *ptr! = NULL; + + ptr) printf ("%s\n", *ptr); Exit (0);}
system Call
System () function Call "/bin/sh-c Command" executes a specific command that blocks the current process until Command Command Execution Complete , the system when the function executes, it calls the Fork , Execve , Waitpid and other functions.
Prototype:
int system (const char *command);
return value:
If the shell Run command cannot be started, System returns 127, and returns 1 if there are other errors that cannot be performed on the system call. Returns the exit code for the command if the system can execute smoothly.
Example int main () { system ("Ls-la"); return 0;}
Write your own system
int Mysystem (const char *command) { if (command = = NULL) { errno = Eagain; return-1; } pid_t pid = fork (); if (PID = =-1) { perror ("fork"); Exit ( -1); } else if (PID = = 0) { execl ("/bin/sh", "sh", "-C", command,null); Exit (127); } int status; Waitpid (pid,&status,0); Wait (&status); return Wexitstatus (status);} int main () { mysystem ("Ls-la"); return 0;}
Linux process Practice (3)--process termination and EXEC function family