In depth interpretation of Linux process functions fork of vfork of execX of

  • 2020-06-23 02:33:28
  • OfStack

This paper mainly focuses on the Linux process functions fork(),vfork(),execX(), which are described as follows.

Function fork ()

fork function: Creates a new process

[1. After fork() succeeds, PCB and user memory space will be applied for the child process.
2. The child process will copy all data (code segment, data segment, BSS, heap, stack) and file descriptor of the parent process user space.
3. Copy most of the information in the father process PCB.
4. Although the child copies the file descriptor, the file table entries associated with the file descriptor (struct file structure) are Shared. ]

1 example:


#include <unistd.h> //fork fuction
#include <fcntl.h> //file operator
#include <sys/types.h>
#include <stdio.h> 
#include <stdlib.h> //exit fuction
#include <string.h>
int main() {
 pid_t pid;
 int i=1; 
 int status;
 char *ch1="hello",*ch2="world",*ch3="IN";
 int fd;
 if ((fd=open("fork.txt",O_RDWR|O_CREAT,0644))==-1) {
 perror("not open");
 exit(EXIT_FAILURE);
 }
 if (write(fd,ch1,strlen(ch1))==-1) { //write in fork.txt
 perror("not write");
 exit(EXIT_FAILURE);
 }
 if ((pid=fork())==-1) {
 perror("fork error"); 
 exit(EXIT_FAILURE);
 }
 else if(pid==0) {  //son process
 int i=2;   //change i
 printf("child:i=%d\n",i);
 if (write(fd,ch2,strlen(ch2))==-1)
 perror("child write");
 return 0;
 }
 else {
 sleep(1);
 printf("parent:i=%d\n",i);
 if (write(fd,ch3,strlen(ch3))==-1)
 perror("child write");
 wait(&status);
 return 0;
 }
}

Run:


[root@localhost linux]# gcc -o fork fork.c 
[root@localhost linux]# ./fork 
child:i=2 
parent:i=1

You can see that the value of i is changed in the child process, while the parent process i is still 1, so the child and parent have their own user space. Open the created ES30en. txt to obtain hellowordIN. The data written by the parent and child processes to one file operation is not cross-covered, indicating that the parent and child processes share file offset and share file table entries once.

Function vfork ()

Unlike the fork() function, the vfork() function does not copy the parent's address space when creating a process, but instead requests new storage space when necessary, thus making vfork() more efficient.

Note in particular that vfork() is sharing the parent process code to data segments.

1 Example:


#include <unistd.h> //fork fuction
#include <fcntl.h> //file operator
#include <sys/types.h>
#include <stdio.h> 
#include <stdlib.h> //exit fuction
#include <string.h>
int i=10;
int main() {
 pid_t pid;
 if ((pid=fork())==-1) {
 perror("fork error"); 
 exit(EXIT_FAILURE);
 }
 else if(pid==0) {  //son process
 i++;
 printf("child:i=%d\n",i);
 _exit(0);  
 }
 else {
 sleep(1);
 printf("parent:i=%d\n",i);
 return 0;
 }
}

Note: The code above recycles the child using _exit(0), if using return 0; It recycles user space, so a segment error occurs when the parent process calls.

Here is the output of the call:


 If the fork() Create will output:  
[root@localhost linux]# ./fork 
child:i=11 
parent:i=10 
 If instead vfork() , then:  
child:i=11 
parent:i=11

Functions exec X() series of functions

Once you have created the Forbidden City with the fork() function, you can call the execX family of functions if you want to run a new program in the current child process.
Note: When a process calls the exec function, the process's user space resource is completely replaced by a new program.
The difference between these functions is:

[

1, indicates the location of the new program is the path or file name
2, when using parameters is to use the parameter list harshi argv[] array
3. The suffix l(list) means to use the parameter list, and v means to use the argv[] array

]

The details are as follows:


#include<unistd.h>

int execl(const char *pathname,const char *arg0,.../*(char *) 0 */);
int execv(const char *pathname,char *const argv[]);
int execle(const char *pathname,const char *arg0,.../*(char *) 0
 ,char *const envp[] */);
int execve(const char *pathname,char *const argv[],char *const envp[]);
int execlp(const char *filename,const char*arg0,.../*(char *) 0*/);
int execvp(const char *filename, char *const argv[]);
int fexecve(int fd,char *const argv[],char *const evnp[]);

1 example:


#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
int main(int argc ,char* argv[]) {
 pid_t pid;
 if ((pid=fork())==-1)
 printf("error");
 else if (pid==0) 
 execl("/bin/ls","ls","-l",argv[1],(char *)0);
 else
 printf("father ok\n");
}

You can see that the ls command was executed in the child process.


[yqtao@localhost linux]$ gcc -o exec execX.c
[yqtao@localhost linux]$ ./exec /home father ok

//execlp() function is used


#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
int main(int argc ,char* argv[]) {
 execlp("ls","ls","-l","/home",(char*)0);
}

// Use of execv() function


#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
int main(int argc ,char* argv[]) {
 char* argv1[]={"ls","-l","/home",0};
 execv("/bin/ls",argv1);
}

ecvp() looks for the filename as the first argument from the directory specified by the environment variable PATH, the second and later arguments from the argument list, noting that the last member must be NULL


#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
int main(int argc ,char* argv[]) {
 char* argv1[]={"ls","-l","/home",0};
 execvp("ls",argv1);
}

conclusion

That is the end of this article on the in-depth interpretation of the Linux process functions fork(),vfork(),execX(), hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: