In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to create sub-processes to execute tasks in Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The Linux operating system relies heavily on process creation to meet the needs of users. For example, whenever the user enters a command, the shell process creates a new process that runs another copy of shell and executes the command entered by the user. New processes are created through fork/vfork system calls in Linux systems. This article shows you how to use fork/vfork system calls to create a new process and use exec family functions to perform tasks in the new process.
Fork system call
The most basic system call to create a process is fork:
# include pid_t fork (void); pid_t vfork (void)
When you call fork, the system creates a new process that is the same as the current process. The original process is usually called the parent process, and the newly created process is called the child process. The child process is a copy of the parent process. The child process gets the same data as the parent process, but the same parent process uses different data segments and stack segments. The child process inherits most of the properties from the parent process, but also modifies some properties. The following table compares the property differences between the parent and child processes:
Inherit attribute difference uid,gid,euid,egid process ID process group ID parent process IDSESSION ID child process run time record open file and file offset parent process lock control terminal to file
Set user ID and set group ID tag bits
Root directory and current directory
The permission mask created by default for the file
Accessible memory segment
Allocation of environmental variables and other resources
Here is a common demo that demonstrates how fork works (the author's environment is Ubuntu 16.04 desktop):
# include # include int main (void) {pid_t pid; char * message; int n; pid = fork (); if (pid < 0) {perror ("fork failed"); exit (1);} if (pid = = 0) {printf ("This is the child process. My PID is:% d. My PPID is:% d.\ n ", getpid (), getppid ();} else {printf (" This is the parent process. My PID is% d.\ n ", getpid ();} return 0;}
Save the above code to the file forkdemo.c file and execute the following command to compile:
$gcc forkdemo.c-o forkdemo
Then run the compiled forkdemo program:
$. / forkdemo
The fork function is characterized by "call once, return twice": once in the parent process, and once in the parent process and once in the child process. The return value when returned in the parent process is the PID of the child process, while the return value when returned in the child process is 0, and the statement after the fork function call is executed after the return. If the fork function call fails, the return value is-1.
When we think about it, we will find that the return value design of the fork function is still very clever. If the fork function returns 0 in the child process, the child process can still call the getpid function to get its own PID, or you can call the getppid function to get the parent process PID. You can get your own PID with the getpid function in the parent process. If you want to get the PID of the child process, the only way is to record the return value of the fork function.
Note: the output of the forkdemo program will change. It may print the information of the parent process or the child process first.
Vfork system call
The functions of vfork system call and fork system call are basically the same. The process created by the vfork system call shares the memory address space of its parent process, but does not completely copy the data segment of the parent process, but shares its data segment with the parent process. To prevent the parent process from overwriting the data needed by the child process, the parent process is blocked by vfork calls until the child process exits or executes a new program. Because the parent process is suspended when the vfork function is called, if we replace the fork function in forkdemo with the vfork function, the order of the output information will not change when the program is executed.
Child processes created with vfork typically execute new programs through the exec family functions. Next, let's take a look at the exec family functions.
Exec family function
After creating a child process using fork/vfork, it executes the same program as the parent process (but it is possible to execute a different branch of code), and the child process often needs to call an exec family function to execute another program. When a process calls exec family functions, the user space code and data of the process are completely replaced by the new program, starting from the beginning of the new program. Calling the exec family function does not create a new process, so the PID of the process does not change before and after calling the exec family function.
There are six functions in the exec family:
# include int execl (const char * path, const char * arg,...); int execlp (const char * file, const char * arg,...); int execle (const char * path, const char * arg,..., char * const envp []); int execv (const char * path, char * const argv []); int execvp (const char * file, char * const argv []); int execve (const char * path, char * const argv [], char * const envp [])
The letter "l" in the function name indicates that the number of parameters is uncertain, and the letter "v" indicates that it uses the string array pointer argv to point to the parameter list.
The letter "p" in the function name automatically searches the path specified by the environment variable PATH for the program to be executed.
Functions with the letter "e" in their names have one more argument envp than other functions. This parameter is a string array pointer that specifies the environment variable. When calling such a function, the environment variable of the child process can be set by the user and stored in the string array pointed to by the parameter envp.
In fact, only execve is the real system call, and the other five functions end up calling execve. The relationship between these functions is shown in the following figure (this figure is from the Internet):
Characteristics of exec family functions: calling exec family functions will load the new program into the current process. After calling the exec family function, the code executed in the process is completely different, so the code after the exec function call will not be executed.
Perform tasks in a child process
Let's execute the ls command in the child process through the vfork and execve functions:
# include # include int main (void) {pid_t pid; if ((pid=vfork ()) < 0) {printf ("vfork error!\ n"); exit (1);} else if (pid==0) {printf ("Child process PID:% d.\ n", getpid ()); char * argv [] = {"ls", "- al", "/ home", NULL}; char * envp [] = {"PATH=/bin", NULL} If (execve ("/ bin/ls", argv, envp) < 0) {printf ("subprocess error"); exit (1);} / / the child process either exits from the ls command or exits from the exit (1) statement above / / so the execution path of the code will never go this far, and the following printf statement will not be executed printf ("You should never see this message.") } else {printf ("Parent process PID:% d.\ n", getpid ()); sleep (1);} return 0;}
Save the above code to the file subprocessdemo.c file and execute the following command to compile:
$gcc subprocessdemo.c-o subprocessdemo
Then run the compiled subprocessdemo program:
$. / subprocessdemo
This is the end of the article on "how to create child processes to execute tasks in Linux". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.