Linux uses the exec function to implement code sharing for process replacement

黄舟
Release: 2017-05-28 11:31:22
Original
1991 people have browsed it

This article mainly introduces the relevant information of Linux process replacement (execfunction) implementation code. Friends in need can refer to

Linux Process replacement (exec function) implementation code

  # 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;                                              }
Copy after login

The picture below is the result picture of execl execlp execv execvp


The picture below is execle Result graph of execve

The above is the detailed content of Linux uses the exec function to implement code sharing for process replacement. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template