In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to understand the process scheduling of linux". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the process scheduling of linux".
Noun
Before that, we must (and certainly not necessarily, but understanding the principle is conducive to understanding and solving errors) to understand a few nouns.
Process group
A process group is a collection of one or more processes, which facilitates the control of multiple processes. In the case of a large number of processes, it is OK to send signals to the process group.
Its ID is determined by the process ID of its leader process. The leader process creates a process group, but it can not determine the survival time of the process group. As long as there is another process in the process group, the process exists, regardless of whether the leader process has been terminated or not.
Conversation
A session (session) is a collection of one or more process groups, which begins when the user logs in to the terminal and ends when the user logs in. Its meaning, like its name, refers to the whole process of a conversation between the user and the system.
The session includes a control process (the lead process that establishes a connection with the terminal), a foreground process group, and any background process group. A session can have only one control terminal, usually a terminal device or pseudo terminal device logged on to it, and the inputs and signals generated on the control terminal will be sent to all processes in the foreground process group of the session.
Control terminal
Whenever we use the terminal tool to open a local or remote shell, we open a control terminal. Through the ps command, we can see that the command is ttyn is its corresponding process, and it corresponds to a file in the linux / dev/ directory.
Work
The concept of a job is similar to a process group, it is also composed of one or more processes, it is divided into foreground jobs and background jobs, a session will have a foreground job and multiple background jobs, unlike a process group, the child processes generated by a process within the job do not belong to this job.
Analogy
The above concepts can be compared to the whole process of a chat through QQ, the control terminal is QQ software, and turning off this software represents the end of the chat. Every message sent during a chat is a process, and an assignment or process group is something we are talking about, which is made up of many pieces of mutual information. Conversation refers to the whole process from the beginning to the end of our chat, and we may talk about a lot of things.
The related figure between them is as follows:
Background execution
Every time we execute a command in the terminal window, the process always occupies the terminal until the end of the process, during which time our input at the terminal is useless. Moreover, when the terminal window closes or the network connection fails, you will find that the process has been interrupted when you open the terminal again. This is because when the user logs out or the network is disconnected, the SIGHUP signal is sent to the child process to which the session belongs, and the default processing of this SIGHUP is to terminate the process that received the signal. So if the signal is not captured in the program, when the terminal is closed, the process to which the session belongs will exit.
In order to achieve the purpose of background execution, we actually want to achieve the following two goals:
Make the process give up the foreground terminal, so that we can continue to interact with the system through the terminal.
So that the process is no longer affected by the terminal shutdown, that is, the system will no longer send SIGHUP signals to the process after the terminal is closed, or the program will not exit even if the signal is sent.
The following commands revolve around these two goals.
&
The first is the symbol we most often encounter & attaching it to the command allows the process to execute in the background without occupying the foreground interface. It actually starts a background job in the session, and we'll talk about it later.
However, we will find that if the terminal is closed at this time, the process will still exit. This is because the & symbol only has the function of letting the process give up the foreground terminal and cannot keep the process from being affected by the SIGHUP signal.
Nohup
Nohup should be another command we often use, which literally makes the process unaffected by SIGHUP signals. However, after using nohup php test.php, we will find that the process will always occupy the foreground terminal, but even if the terminal is closed or disconnected, the program will still be executed, and we will find that there is an extra file named nohup.out in the current folder.
This is because nohup only protects the process from SIGHUP signals, does not give up the foreground terminal, and it also sets up a nohup.out in the command execution directory to store the process's output. If the process does not need output and does not want nohup to create files, you can redirect standard output and standard error output.
We often use nohup and & together and execute the command as follows: nohup command > / dev/null 2 > & 1 & so we can safely wait for the process to run.
Setsid
Setsid is another command that allows the process to execute in the background. Its purpose is to get the process to open a new session and run the process, using setsid command.
According to the above concept, we know that the process exits after the terminal shuts down because the session head process sends a SIGHUP signal to the process, setsid is powerful, it directly opens a new session to execute the command, then the state of the terminal of the original session will no longer affect the process.
We use pstree to see the use of setsid and nohup... & the process tree state of two commands to run a process.
Nohup php test.php &
I log on to the machine remotely using ssh, so the test.php process is hung under the sshd process. Normally, once the sshd process ends, test.php is not immune.
Setsid php test.php
With setsid, the test.php process is at the same level as the sshd process and is a child of the init process.
However, setsid does not assign an output terminal to the process, so the process is still output to the current terminal.
Setsid's pit
In addition, setsid has a slight pitfall: when you use setsid command to run a process directly in the terminal, the foreground of the terminal will not be affected, and command will run silently in the background. In the shell script, we will find that the process running setsid will block until the end of the command process execution.
This is because setsid will fork () a process when it is the process leader, but it will not wait () its child processes, but will exit immediately, so when using setsid directly in the terminal, setsid as the process leader will not occupy the terminal interface.
In the shell script, setsid is not the process leader, it does not fork () child processes, but bash fork () a child process, while bash wait () child processes, so it behaves like setsid in the wait () child process.
There are two ways to solve this problem:
Use the & symbol described above to force the setsid into the background.
Use. Or source command, the terminal executes setsid.
Other
In addition to the commands described above, there are screen and tmux and other conversation tools, they all have their own set of specifications, but also more complex, mastering the commands of this article is enough for you to control the linux process. Of course, those who want to know new knowledge can query and learn, which should be easier to use than basic commands.
Job command
You may also encounter some minor situations when using the above background to execute commands:
The process we put in the background takes too long to execute, and we forget to use the nohup command, so once the terminal is disconnected, the process needs to be re-executed.
We start a process directly and want it to give up the foreground terminal without interrupting the process.
All of these involve today's second module-homework.
The commands we run in the terminal can be understood as a job, some occupy the foreground terminal, some execute silently in the background, the following command is to schedule these jobs.
Jobs
Jobs is the basic command of the job, which allows you to view the information of the running job. The output is as follows:
Jobs
1-Running php test.php &
2 + Stopped php test.php
The number in front of [] is the job ID, which is also the identification of the job we want to operate later, followed by the job status and commands.
Ctrl+z
Strictly speaking, ctrl+z is not a job command, it only sends a SIGSTOP signal to the current process, prompting the process to enter a stopped state. In this state, the process state will be saved by the system, and the process will be placed in the job queue and let go of the process terminal.
Using it, we can pause the process that is occupying the terminal without stopping it, thus allowing us to use terminal commands to operate the process.
Bg
Bg is an acronym for backgroud, and as the name implies, bg% id puts jobs into background processes to execute.
Combined with the ctrl+z and bg commands, we can solve the problems mentioned above and constantly put the processes that are occupying the terminal in the background.
Fg
Fg, as opposed to bg, can be used to put the job to the foreground to execute.
Disown
Disown is used to remove a job from the job list, even if it is not part of a session, so that when the terminal shuts down, it no longer sends a SIGHUP signal to the job to prevent the terminal from affecting the process.
With disown we can solve the second problem raised above, without re-executing a process that does not use the nohup command will not be affected by terminal shutdown.
Daemon process
All of the above are the handling of some temporary processes, and the ultimate way for a process running in the background is to turn the process into a daemon.
Daemon process
Daemon process (daemon) is a kind of process with a long lifetime, which usually starts when the system starts and stops when the system shuts down. It has no control terminal and will not output. For example, our server, fpm and other processes exist in the form of daemons.
Creation process
To create a daemon, the steps are:
Required option
The fork child process exits the parent process, and the child process is adopted by the init process as an orphan process
Using setsid, open a new session, and the process becomes the session leader and officially leaves the terminal control.
Set signal processing (especially for child processes to exit processing); optional:
Use chdir to change the process working directory, generally to the root directory, to prevent the occupation of unmountable file systems
Reset the file permissions mask with umask and no longer inherit the file permissions settings of the parent process
Close the file descriptor opened by the parent process
Code
The following is the pseudo code for php to create the daemon:
$pid = pcntl_fork (); if ($pid > 0) {exit; / / parent process exits directly} elseif ($pid < 0) {throw_error (); / / process creation failed} posix_setsid (); / / setsid becomes session leader process chdir ($dir); / / changes directory umask (0); / / resets file permissions mask close_fd () / / close the file descriptor pcntl_signal ($signal, $func) of the parent process; / / register the signal processing function while (true) {do_job (); / / process task pcntl_signal_dispatch () / / distribute signal processing} Thank you for your reading. The above is the content of "how to understand the process scheduling of linux". After the study of this article, I believe you have a deeper understanding of how to understand the process scheduling of linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.