Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Linux process functions fork (), vfork (), execX ()

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about the usefulness of the Linux process functions fork (), vfork (), execX (). The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Function fork ()

Fork function: create a new process

1. After fork () succeeds, the PCB and user memory space will be applied for the child process.

2. The child process will copy all the data (code snippet, data segment, BSS, stack) and file descriptor in the user space of the parent process.

3. Copy most of the information in the parent process PCB.

4. Although the child process copies the file descriptor, the file table items related to the file descriptor (struct file structure) are shared.

An example:

# include / / fork fuction#include / / file operator#include # include # include / / exit fuction#include int main () {pid_t pid; int item1; int status; char * ch2= "hello", * ch3= "world", * ch4= "IN"; int fd; if ((fd=open ("fork.txt", O_RDWR | Olympiad 0644)) =-1) {perror ("not open"); exit (EXIT_FAILURE) } if (write (fd,ch2,strlen (ch2)) = =-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 item2; / / change i printf ("child:i=%d\ n", I) If (write (fd,ch3,strlen (ch3)) = =-1) perror ("child write"); return 0;} else {sleep (1); printf ("parent:i=%d\ n", I); if (write (fd,ch4,strlen (ch4)) = =-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, but the parent process I is still 1, so the child process and the parent process have their own user space. If you open the created fork.txt, you can get the hellowordIN, and the data written by the parent and child processes for a file operation are not cross-overwritten, indicating that the parent and child processes share the file offset and share the file table items at once.

Function vfork ()

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

Note in particular that vfork () shares the code of the parent process to the data segment.

An example:

# include / / fork fuction#include / / file operator#include # include # include / / exit fuction#include int iTunes 10 political int main () {pid_t pid; if ((pid=fork ()) = =-1) {perror ("fork error"); exit (EXIT_FAILURE);} else if (pid==0) {/ / son process iTunes; printf ("child:i=%d\ n", I); _ exit (0);} else {sleep (1) Printf ("parent:i=%d\ n", I); return 0;}}

Note: the recycling child process in the above code uses _ exit (0). If you use return 0;, it reclaims user space, so a segment error occurs when the parent process invokes it.

Here is the result of the call:

If it is created with fork (), it will output: [root@localhost linux] #. / fork child:i=11 parent:i=10 if changed to vfork (), then: child:i=11 parent:i=11 function exec X () series functions

After creating the Forbidden City with the fork () function, you can call the execX series functions if you want to run the new program in the current child process.

Note: when a process calls the exec function, the process's user space resources are completely replaced by a new program.

The differences between these functions are:

1. Indicates whether the location of the new program is a path or a file name

2. Use the parameter list when using parameters. Hashi uses the argv [] array.

3. The suffix l (list) indicates using the parameter list, and v means using the argv [] array.

The details are as follows:

# includeint execl (const char* pathname,const char*arg0,.../* (char*) 0 steps /); 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 cycles /) Int execvp (const char * filename, char * const argv []); int fexecve (int fd,char * const argv [], char * const evnp [])

An example:

# include # include # include 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");}

Running 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

/ / the execlp () function is used

# include # include # include int main (int argc, char* argv []) {execlp ("ls", "ls", "- l", "/ home", (char*) 0);}

/ / the use of the execv () function

# include # include # include int main (int argc, char* argv []) {char* argv1 [] = {"ls", "- l", "/ home", 0}; execv ("/ bin/ls", argv1);}

Ecvp () looks for the file name in the directory specified by the environment variable PATH as the first parameter, and the second and subsequent parameters are listed by the parameter list. Note that the last member must be NULL.

# include # include # include int main (int argc, char* argv []) {char* argv1 [] = {"ls", "- l", "/ home", 0}; execvp ("ls", argv1);} Thank you for reading! About "Linux process function fork (), vfork (), what is the use of execX ()" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report