欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux 进程替换(exec函数)实现代码

程序员文章站 2022-07-04 18:29:41
linux 进程替换(exec函数)实现代码 # include #include...

linux 进程替换(exec函数)实现代码

  # include<stdio.h>   
  #include<stdlib.h> 
  #include<unistd.h>  
  #include<fcntl.h>  
  #include<sys/types.h>
  #include<sys/stat.h>                                                  
  #include<string.h>  
  int main()     
   {         
    pid_t id=fork(); 
  if(id==0)      
   {        
   printf("child is running\n");
   sleep(1);    
  char* env[]={"myenv=/a/b/c/d/aaaa",null};
  char* argv[]={"ls","-l","-n","-i",null};
 // execl("/bin/ls","ls","-l","-n","-i",null);
  // execlp("ls","ls","-l","-n","-i",null);
 //  execv("/bin/ls",argv); 
  // execvp("ls",argv);
            
             
   // execle("./myenv","myenv",null,env);
      execve("./myenv",argv,env);    
      printf("child id done\n");  //exec成功的话不执行此语句
      exit(1);   
    }               
    else                                                        
    {         
       pid_t ret=waitpid(id, null,0);
      if(ret>0)  
       {      
        printf("father wait success\n"); }
         else{  
          printf("child quit not normal\n");
            
       }   }   return 0;                                              }
 

                  

下图就是execl execlp execv execvp的结果图

Linux 进程替换(exec函数)实现代码

下图是execle execve的结果图

Linux 进程替换(exec函数)实现代码

感谢阅读,希望能帮助到大家,谢谢大家对对本知道支持!