Home >Operation and Maintenance >Linux Operation and Maintenance >Linux uses the exec function to implement code sharing for process replacement

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

黄舟
黄舟Original
2017-05-28 11:31:222023browse

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;                                              }

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!

Statement:
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