Linux进程理解与实践(三)进程终止函数和exec函数族的使用

进程的几种终止方式(Termination)

(1)正常退出

   从main函数返回[return]

   调用exit

   调用_exit或者_Exit

   最后一个线程从其启动处返回

   从最后一个线程调用pthread_exit (最后两点见后面博客)

(2)异常退出

   调用abort   产生SIGABOUT信号

   由信号终止  Ctrl+C [SIGINT]

   最后一个线程对取消请求做出响应


从图中可以看出,_exit 函数的作用是:直接使进程停止运行,清除其使用的内存空间,并清除其在内核的各种数据结构;exit 函数则在这些基础上做了一些小动作,在执行退出之前还加了若干道工序。exit() 函数与 _exit() 函数的最大区别在于exit()函数在调用exit  系统调用前要检查文件的打开情况,把文件缓冲区中的内容写回文件。也就是图中的“清理I/O缓冲”。另外注意_exit是一个系统调用,exit是一个c库函数。

  1. int main()  
  2. {  
  3.     pid_t result;  
  4.     result=fork();  
  5.     if(result<0)  
  6.         ERR_EXIT("fork error");  
  7.     if(result==0)  
  8.     {  
  9.         printf("This is the _exit test.Child pid=%d ",getpid());  
  10.         printf("Output the content in Child!");  
  11.         _exit(0);  
  12.     }  
  13.     else  
  14.     {  
  15.        printf("This is the exit test.Parent pid=%d ",getpid());  
  16.        printf("Output the content in Parent!");  
  17.        exit(0);  
  18.     }  
  19.     return 0;  
  20. }  


结果分析:

子进程中运行_exit(0)并未将This is the content in Child打印出来,而父进程中运行的exit(0)This is the content in Parent打印出来了。说明,exit(0)会在终止进程前,将缓冲I/O内容清理掉,所以即使printf里面没有  也会被打印出来,而_exit(0)是直接终止进程,并未将缓冲I/O内容清理掉,所以不会被打印出来。

终止处理程序:atexit

按照ISO C的规定,一个进程可以登记多至32个函数,这些函数将由exit自动调用,我们称这些函数为终止处理函数,并调用atexit函数来登记这些函数

  1. void exitHa1()  
  2. {  
  3.     printf("when exit,the num is one ");  
  4. }  
  5. void exitHa2()  
  6. {  
  7.     printf("when exit,the num is two ");  
  8. }  
  9. int main()  
  10. {      
  11.     printf("In main ,pid=%d ",getpid());  
  12.     atexit(exitHa1);  
  13.     atexit(exitHa2);  
  14.     return 0;  
  15.    
  16. }  

我们可以看出的是:先注册的后执行。


如果将return -0替换成abort()异常退出的话,那么程序运行的结果如上图。

exec函数族

exec替换进程印象

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

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

   exec只是用磁盘上的一个新程序替换了当前进程的正文段, 数据段, 堆段和栈段.并没有创建新进程,所以进程的ID是不变的。

exec函数族:

  1. int execve(const char *filename, char *const argv[],    
  2.                   char *const envp[]);    
  3.     
  4. #include <unistd.h>    
  5. extern char **environ;    
  6. int execl(const char *path, const char *arg, ...);    
  7. int execlp(const char *file, const char *arg, ...);    
  8. int execle(const char *path, const char *arg,    
  9.            ..., char * const envp[]);    
  10. int execv(const char *path, char *const argv[]);    
  11. int execvp(const char *file, char *const argv[]);    
  12. int execvpe(const char *file, char *const argv[],    
  13.             char *const envp[]);    

说明:

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

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

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

/*总结:l代表可变参数列表,与v互斥;v表示该函数取一个argv[]矢量;p代表在path环境变量中搜索file文件;e表示该函数取envp[]数组,而不使用当前环境*/


  1. int main()  
  2. {  
  3.     printf("Test the execlp: ");  
  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","-l",NULL)==-1)  
  16.             ERR_EXIT("execlp ls -l error");  
  17.     }  
  18.     wait(NULL);  
  19.        
  20.     printf("Test the execve: ");  
  21.    
  22.     char *argv_execve[]={"/bin/data","+%F",NULL};  
  23.     pid=fork();  
  24.     if(pid==0)  
  25.     {  
  26.         if(execve("/bin/date",argv_execve,NULL)==-1)  
  27.             ERR_EXIT("execve error");  
  28.     }  
  29.     wait(NULL);  
  30.     return 0;  
  31.    
  32. }  

另外,如果你理解execv, 那么execle和他的区别就是, 前者的调用参数是以数组形式给的,而后者则是以列表方式给,也就是execle(path, arg1, arg2, ..., envp), 并且提供了环境变量参数;

  1. #include<unistd.h>  
  2. #include<stdio.h>  
  3. #include<stdlib.h>  
  4.   
  5. int main()  
  6. {  
  7.     char *envp[]={"PATH=/tmp","USER=shan",NULL};  
  8.     if(fork()==0)  
  9.     {  
  10.         if(execle("/bin/dir","dir",NULL,envp)<0)  
  11.             perror("execle error!");  
  12.     }  
  13.     return 0;  
  14. }  

System系统调用

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

原型:

  1. int system(const char *command);  

返回值:

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

自己利用execl实现system:

  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);//宏,子进程如果正常结束返回0    
  25. }    
  26.     
  27. int main()    
  28. {    
  29.     mySystem("ls -la");    
  30.     
  31.     return 0;    
  32. }    




原文:http://blog.csdn.net/nk_test/article/details/48324609

原文地址:https://www.cnblogs.com/Zoran-/p/5819255.html