First assignment: In-depth analysis of Linux system processes

Foreword : There are many kinds of operating systems today. I mainly talk about the Linux operating system. First of all, let's take a look at the Linux system. Linux is a set of Unix-like operating systems that are free to use and spread freely. It is based on POSIX and UNIX. A multi-user, multi-tasking, multi-threaded and multi- CPU operating system. It can run major UNIX tools, applications and network protocols. It supports 32-bit and 64-bit hardware . Linux inherits the network-centric design idea of ​​Unix and is a multi-user network operating system with stable performance. It is mainly used on computers based on Intel x86 series CPUs. This is the explanation on the Baidu website.

1. How does the Linux system organize processes?

First, let's understand what a process is?

1.1 Process is a running activity of a program in a computer on a data set, the basic unit of resource allocation and scheduling in the system, and the basis of the operating system structure. In the early computer structure of process-oriented design, the process is the basic execution entity of the program; in the contemporary computer structure of thread-oriented design, the process is the container of the thread. A program is a description of instructions, data, and their organization, and a process is the entity of a program. From the explanation on the Baidu webpage.

1.2 Linux system creates processes all use the fork() system call to create a child process. The new process created by the fork() system call is called a child process. The function is called once, but returns twice. If the fork() process call is successful, the difference between the two returns is that the return value of the child process is 0, and the return value of the parent process is the process number of the new child process.

1,3 The process control block of Linux is a data structure defined by the structure task_struct . The task_struct is defined in /include/linux/sched.h, which includes various information required for the management process.

 

 task_struct structure description

The included header file include\linux\sched.h.

Each process will be allocated a task_struct structure, which contains all the information of the process,

Information about this structure can be tracked by the operating system at any time.

This structure is the most important data structure of the linux kernel summary, which we will introduce in detail below.

The main information of this structure:

1. Process status, which will record whether the process is waiting, running, or deadlocked

2. Scheduling information, which scheduling function to schedule, how to schedule, etc.

3. The communication status of the process

4. Because to insert the process tree, there must be a pointer to link the father, son and brother, of course, the task_struct type

5. Time information, such as the calculated execution time for cpu allocation

6, label, determine the ownership of the improvement process

7. Can read and write some open file information

8. Process context and kernel context

9. Processor context

10. Memory information

The following pictures are all from the information on the relevant link.

 

 

 1.  Process state (State)

 

As the process executes, it changes state depending on the situation. Process state is the basis for scheduling and swapping. Processes in Linux mainly have the following states

 

 

 

kernel representation

meaning

TASK_RUNNING

runnable

TASK_INTERRUPTIBLE

interruptible wait state

TASK_UNINTERRUPTIBLE

uninterruptible wait state

TASK_ZOMBIE

dead

TASK_STOPPED

pause

TASK_SWAPPING

Swap in/out state of a Linux process

 


 

 

 

 

· Operational status

 

Processes in this state are either running or preparing to run. The running process is the current process (the process pointed to by current), and the process that is ready to run can be put into operation immediately as long as it gets the CPU. The CPU is the only system resource that these processes wait for. There is a run queue (run_queue) in the system, which is used to accommodate all processes in a runnable state. When the scheduler is executed, a process is selected and put into operation. When we discuss process scheduling later, we can see the role of the run queue. The current running process is always in the queue, that is, current always points to an element in the running queue, but it is determined by the scheduler who it points to.

 

·Wait state

 

The process in this state is waiting for some event (event) or some resource, it must be in some waiting queue (wait_queue) in the system. There are two kinds of processes in waiting state in Linux: interruptible waiting state and uninterruptible waiting state. A process in an interruptible waiting state can be awakened by a signal. If a signal is received, the process enters a runnable state from the waiting state, and is added to the run queue, waiting to be scheduled; and a process in an uninterruptible waiting state is due to hardware The environment cannot be satisfied and waits, such as waiting for a specific system resource, which cannot be interrupted under any circumstances, and can only be woken up in a specific way, such as the wake-up function wake_up().

 

· Pause state

 

The process at this point is temporarily stopped to receive some kind of special treatment. Usually a process is in this state when it receives a SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU signal. For example, a process being debugged is in this state.

 

· Zombie state

 

Although the process has been terminated, for some reason, the parent process has not executed the wait() system call, and the information of the terminated process has not been recovered. As the name implies, a process in this state is a dead process, which is actually garbage in the system and must be dealt with to release the resources it occupies.

2. How is the process state transformed?

Screenshot of the image source network 

3. How is the process scheduled?

In modern operating systems, we cannot make a process correspond to a core, which means that managing a process needs to add a management unit that can control it. That is, the process scheduler.

The CPU time allocated by the scheduler cannot be too long, otherwise other programs will be delayed in response, making it difficult to ensure fairness.

The time allocated by the scheduler should not be too short. Each scheduling will result in context switching, which is very expensive.

The structure of the scheduler:

Process scheduling is not always possible. As mentioned earlier, the system will have a periodic scheduler that automatically calls the schedule_tick function according to the frequency. Its main function is to trigger scheduling according to the running time of the process; when the process encounters resources waiting to be blocked, it can also call the scheduler function for scheduling; in addition, when the kernel space returns to the user space, it will judge whether scheduling is currently required, and the process In the corresponding thread_info structure, there is a flag, and the second bit of the flag field (starting from 0) is used as a rescheduling identifier TIF_NEED_RESCHED. When set, it indicates that there is a higher priority process at this time, which needs to be scheduled. In addition, the current kernel supports the kernel preemption function, which can preempt the operation of the kernel at an appropriate time.

 

 

4. Views on the Linux system process model:

Every system has similarities and differences. The core of LINUX is UNIX. There are many other variants of UNIX operating systems .

The state of the system process is constantly switching in various small states, constantly waking up or stopping a certain state. It is also impossible for a process to target a core, so a small assistant is needed to help manage the process scheduler. If you want to know more, you need to study in depth.

Reference link

https://www.cnblogs.com/ck1020/p/6089970.html

https://www.cnblogs.com/hanxiaoyu/p/5549212.html

https://blog.csdn.net/wodeqingtian1234/article/details/54178770

https://blog.csdn.net/myarrow/article/details/7035684

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077591&siteId=291194637