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

Process switching process in Linux system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "process switching process under Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Process switching under Linux kernel

Linux switching does not use the switching method of X86CPU, the essence of Linux switching is cr3 switching (memory space switching, in the switch_mm function) + register switching (including EIP,ESP, all in the switch_to function). Here we talk about the main process of switch_to:

1. Set new_task- > pgd to the cr3 register in the switch_mm function to switch the page table. Since the page table mapping mechanism of 3-4G for each process is exactly the same (copied directly from the kernel page table), although pgd is switched here, it will only change when the task returns to the user space, because the page table mapping of each task in 0-3G is independent.

2. Press esi edi ebp into cur_task stack

3. Save the value in the esp register to cur_task.task_struct.thread.esp, that is, save the stack pointer when cur_task is switched.

4. Set the value in new_task.task_struct.thread.esp to the esp register, where the value in new_task.task_struct.thread.esp is the stack pointer when new_task was last swapped out, and now it has been restored. 2 and 3 combine to realize the stack switch from cur_task to new_task.

5. Set the 1f address to cur_task.task_struct.thread.eip, and the next time cur_task resumes operation, it will start from 1f. This principle is described below.

6. Press new_task.task_struct.thread.eip into the stack of new_task, where the value of new_task.task_struct.thread.eip is 1f, because from 4, the last time new_task was swapped out, it was similar to the current cur_task, and the 1f address was set to new_task.task_struct.thread.eip.

7. Then CPU jumps to the _ _ switch_to function to start execution. Note that jmp is used here, not call,call will pusheip, and jmp will not. Because _ _ switch_to is a function, when CPU executes the function, the last instruction must be iret, and the instruction will popeip. As can be seen from 5, the mirror in the new_task stack is [., esi,edi,ebp,eip (& 1f)] Therefore, popeip sets the value eip (& 1f) to the eip register, so that when iret execution is finished, CPU will continue execution from eip, that is, from 1f.

8. At this time, it is already in the execution environment of new_task. Pop ebp, pop edi, popesi, return to the schedule function. When returning to the user space, the eip,ss,esp of the new_task user space is popped up from the stack of the new_task to the corresponding register, so the new_task can be executed smoothly.

Linux foreground and background process switching

When you start a program with shell, it often works in the foreground. For example, when you often use PUTTY to connect to a remote server to execute a script, if the local network is interrupted, the foreground process ends and must be re-executed. Therefore, it is necessary to switch between the front and background processes.

For example, if you type firefox directly into the terminal, firefox will be opened, but when you close the terminal or ctrl+c forces termination, firefox will also be turned off at random.

You can add a & to the end of the execution, so you can work in the background. Shell supports action control with the following commands:

(1)。 Command & Let the process run in the background

(2)。 Jobs-l view the processes running in the background

(3)。 Fg n lets processes n running in the background come to the foreground

(4)。 Bg n send process n to the background

PS: "n" is the process number seen by jobs.

1. Execute the command & switch to the background

When the Linux terminal runs the command, the & symbol is added to the end of the command to make the program run in the background.

The code is as follows:

Root@Ubuntu$. / tcpserv01&

2. Switch the running program to the background

If the program is running in the foreground, you can use the Ctrl+z option to pause the program, and then use the bg% [number] command to run the program in the background. This step is divided into three steps, as follows:

2.1 suspend the program from running CTRL+Z

Ctrl + z is related to system tasks, ctrl + z can put a command being executed in the foreground in the background and pause it.

The code is as follows:

[Oracle@linuxidc ~] $sh ins.sh

[1] + Stopped ins.sh

2.2 View paused programs

Look at jobs use the jobs or ps command to view the jobs that is being executed.

The code is as follows:

[oracle@linuxidc ~] $jobs-l

[1] + 4524Stopped ins.sh

The result of the execution of the jobs command, + indicates that it is a current job, and the minus table is a job after the current job.

The jobs-l option shows the PID,jobs status of all tasks. It can be running, stopped,Terminated.

2.3 switch programs to background

Bg changes a command that is paused in the background to continue execution. if there are multiple commands in the background, you can call up the selected command with bg% jobnumber.

The code is as follows:

[oracle@linuxidc ~] $bg 1

[oracle@linuxidc ~] $jobs-l

[1] + 4524Running ins.sh

2.4 switch the program to the front desk

You can also use the fg% [number] instruction to drop a program to the foreground to run

The code is as follows:

[oracle@linuxidc ~] $fg 1

. / tcpserv01

2.5 terminating the daemon

You can also directly terminate the program running in the background and use the kill command

The code is as follows:

[oracle@linuxidc ~] $kill 1

But if the task is terminated (kill), shell removes the process identity of the task from the list known to the current shell environment; that is, the jobs command displays information about tasks that are running or suspended in the background in the current shell environment.

This is the end of the content of "process switching process under Linux system". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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