Linux进程实践(3) --进程终止与exec函数族

来源:互联网 发布:监控矩阵键盘说明书 编辑:程序博客网 时间:2024/04/30 08:43

进程的几种终止方式

(1)正常退出

   从main函数返回[return]

   调用exit

   调用_exit/_Exit

(2)异常退出

   调用abort   产生SIGABOUT信号

   由信号终止  Ctrl+C [SIGINT]

   ...(并不完全, 如return/pthread_exit等)


测试[exit/_exit]

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //尝试查看该程序的打印输出  
  2. int main()  
  3. {  
  4.     cout << "In main, pid = " << getpid();    
  5.     //去掉了endl;  
  6.     //原理:与终端关联,stdout为行缓冲,在文件中,为全缓冲;  
  7.     //详细信息请参考《UNIX环境高级编程》(第三版)8.5节, P188  
  8.     //exit(0);为C库函数,详细解释如下  
  9.     _exit(0);  
  10. }  


由图可知,系统调用_exit直接陷入内核,而C语言库函数是经过一系列的系统清理工作,再调用Linux内核的;

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int main()  
  2. {  
  3.     cout << "In main, pid = " << getpid();  
  4.     fflush(stdout); //增加了刷新缓冲区工作  
  5.     _exit(0);  
  6. }  

小结:exit与_exit区别

   1)_exit是一个系统调用,exit是一个c库函数

   2)exit会执行清除I/O缓存

   3)exit会执行调用终止处理程序 //终止处理程序如下

 

终止处理程序:atexit

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdlib.h>  
  2. int atexit(void (*function)(void));  
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //测试  
  2. void exitHandler1(void)  
  3. {  
  4.     cout << "If exit with exit, the function exitHandler will be called1" << endl;  
  5. }  
  6. void exitHandler2(void)  
  7. {  
  8.     cout << "If exit with exit, the function exitHandler will be called2" << endl;  
  9. }  
  10.   
  11. int main()  
  12. {  
  13.     cout << "In main, pid = " << getpid() << endl;  
  14.     atexit(exitHandler1);   //注意,先注册的后执行  
  15.     atexit(exitHandler2);  
  16.     exit(0);  
  17. }  


异常终止

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int main()  
  2. {  
  3.     cout << "In main, pid = " << getpid() << endl;  
  4.     atexit(exitHandler1);  
  5.     atexit(exitHandler2);  
  6.     abort();  
  7.     //exit(0);  
  8. }  

exec函数族

exec替换进程印象

   在进程的创建上,Unix采用了一个独特的方法,它将进程创建与加载一个新进程映象分离。这样的好处是有更多的余地对两种操作进行管理。

   当我们创建了一个进程之后,通常将子进程替换成新的进程映象,这可以用exec系列的函数来进行。当然,exec系列的函数也可以将当前进程替换掉。

   exec只是用磁盘上的一个新程序替换了当前进程的正文段, 数据段, 堆段和栈段.

 

函数族信息

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <unistd.h>  
  2. int execve(const char *filename, char *const argv[],  
  3.                   char *const envp[]);  
  4.   
  5. #include <unistd.h>  
  6. extern char **environ;  
  7. int execl(const char *path, const char *arg, ...);  
  8. int execlp(const char *file, const char *arg, ...);  
  9. int execle(const char *path, const char *arg,  
  10.            ..., char * const envp[]);  
  11. int execv(const char *path, char *const argv[]);  
  12. int execvp(const char *file, char *const argv[]);  
  13. int execvpe(const char *file, char *const argv[],  
  14.             char *const envp[]);  

说明:

   execl,execlp,execle(都带“l”, 代表list)的参数个数是可变的,参数以必须一个空指针结束。

   execv和execvp的第二个参数是一个字符串数组(“v”代表“vector”,字符串数组必须以NULL结尾),新程序在启动时会把在argv数组中给定的参数传递到main。

   名字最后一个字母是“p”的函数会搜索PATH环境变量去查找新程序的可执行文件。如果可执行文件不在PATH定义的路径上,就必须把包括子目录在内的绝对文件名做为一个参数传递给这些函数;

 

/*总结:l代表可变参数列表,p代表在path环境变量中搜索file文件。envp代表环境变量*/

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //示例execlp  
  2. int main()  
  3. {  
  4.     pid_t pid = fork();  
  5.     if (pid == 0)  
  6.     {  
  7.         if (execlp("/bin/pwd""pwd", NULL) == -1)  
  8.             err_exit("execlp pwd error");  
  9.     }  
  10.     wait(NULL);  
  11.   
  12.     pid = fork();  
  13.     if (pid == 0)  
  14.     {  
  15.         if (execlp("/bin/ls""ls""-l", NULL) == -1)  
  16.             err_exit("execlp ls -l error");  
  17.     }  
  18.     wait(NULL);  
  19.     cout << "After execlp" << endl;  
  20. }  
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //示例execve  
  2. int main()  
  3. {  
  4.     char *const args[] =  
  5.     {  
  6.         (char *)"/bin/date",  
  7.         (char *)"+%F",  
  8.         NULL  
  9.     };  
  10.     execve("/bin/date",args,NULL);  
  11.     cout << "After fork..." << endl;  
  12.   
  13.     return 0;  
  14. }  
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //示例execle  
  2. //1:main.cpp  
  3. int main()  
  4. {  
  5.     cout << "In main, pid = " << getpid() << endl;  
  6.   
  7.     char *const environ[] =  
  8.     {  
  9.         "AA=11",  
  10.         "BB=22",  
  11.         "CC=33",  
  12.         NULL  
  13.     };  
  14.     execle("./hello","./hello",NULL,environ);   //当environ填为NULL时,则什么都不传递  
  15.     cout << "After fork..." << endl;  
  16.   
  17.     return 0;  
  18. }  
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. extern char **environ;  
  2. int main()  
  3. {  
  4.     cout << "In hello, pid = " << getpid() << endl;  
  5.   
  6.     cout << "environ:" << endl;  
  7.     for (int i = 0; environ[i] != NULL; ++i)  
  8.     {  
  9.         cout << "\t" << environ[i] << endl;  
  10.     }  
  11. }  
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /* 
  2. In main, pid = 3572 //PID保持不变 
  3. In hello, pid = 3572 
  4. environ: 
  5.     AA=11 
  6.     BB=22 
  7.     CC=33 
  8. */  

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //示例: execve 与 execlp  
  2. int main()  
  3. {  
  4.     pid_t pid = fork();  
  5.     if (pid == -1)  
  6.         err_exit("fork error");  
  7.     else if (pid == 0)  
  8.     {  
  9.         //示例execve  
  10.         char *const args[] =  
  11.         {  
  12.             "echoall",  
  13.             "myarg1",  
  14.             "MY ARG2",  
  15.             NULL  
  16.         };  
  17.         char *const env[] =  
  18.         {  
  19.             "USER=unknown",  
  20.             "PATH=/tmp",  
  21.             NULL  
  22.         };  
  23.   
  24.         execve("./echoall",args,env);  
  25.     }  
  26.     wait(NULL);  
  27.   
  28.     pid = fork();  
  29.     if (pid == -1)  
  30.         err_exit("fork error");  
  31.     else if (pid == 0)  
  32.     {  
  33.         //示例execlp  
  34.         execlp("./echoall""echoall""only one arg", NULL);  
  35.     }  
  36.     wait(NULL);  
  37.   
  38.     return 0;  
  39. }  
  40.   
  41. //echoall  
  42. int main(int argc, char *argv[])  
  43. {  
  44.     for (int i = 0; i < argc; ++i)  
  45.         printf("argv[%d]: %s\t", i , argv[i]);  
  46.     printf("\n");  
  47.   
  48.     for (char **ptr = environ; *ptr != NULL; ++ ptr)  
  49.         printf("%s\n", *ptr);  
  50.   
  51.     exit(0);  
  52. }  

System系统调用

  system()函数调用“/bin/sh -c command”执行特定的命令,阻塞当前进程直到command命令执行完毕,system函数执行时,会调用forkexecvewaitpid等函数。

原型:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int system(const char *command);  

返回值:

    如果无法启动shell运行命令,system将返回127;出现不能执行system调用的其他错误时返回-1。如果system能够顺利执行,返回那个命令的退出码。

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //示例  
  2. int main()  
  3. {  
  4.     system("ls -la");  
  5.   
  6.     return 0;  
  7. }  

自己动手写system

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. int mySystem(const char *command)  
  2. {  
  3.     if (command == NULL)  
  4.     {  
  5.         errno = EAGAIN;  
  6.         return -1;  
  7.     }  
  8.     pid_t pid = fork();  
  9.     if (pid == -1)  
  10.     {  
  11.         perror("fork");  
  12.         exit(-1);  
  13.     }  
  14.     else if (pid == 0)  
  15.     {  
  16.         execl("/bin/sh","sh","-c",command,NULL);  
  17.         exit(127);  
  18.     }  
  19.   
  20.     int status;  
  21.     waitpid(pid,&status,0);  
  22.     //wait(&status);  
  23.   
  24.     return WEXITSTATUS(status);  
  25. }  
  26.   
  27. int main()  
  28. {  
  29.     mySystem("ls -la");  
  30.   
  31.     return 0;  
  32. }  





0 0
原创粉丝点击