Home  >  Article  >  Java  >  mean? Please provide more context.

mean? Please provide more context.

王林
王林forward
2023-05-08 22:16:291360browse

1 Process Overview

The process is an abstraction of logic. We have a lot of understanding of the process from operating system books, but we may not know much about the implementation of the process. This article Try to explain the general principles of process implementation.
The implementation of the process is actually the same as when we usually write code. For example, if we want to represent something, we will define a data structure. Processes are no exception. So the essence of a process is a data structure, which stores a series of data. The operating system manages all processes in the form of arrays or linked lists. Processes can be said to be divided into two types
1 The first process when the system is initialized,
2 Except for the first process, other processes are created by the fork or fork execute system calls.
Let’s first take a look at the information in the process structure.

mean? Please provide more context.

The above is the main information in the structure representing the process. Then a structure represents a process. We know that fork uses the parent process as a module, copies the structure of the parent process, and then modifies certain fields. It becomes a new process. If execute is called, the fields in the copied structure (such as page tables, code segments, and data segments) are further modified. And load the corresponding data from the hard disk to the memory. So how is the first process generated? Because a process is just a structure, if we predefine a structure, we can create a process without forking.


2 Process execution

When the system creates a process, it will set the value of the cs:ip register. If it is a fork, the ip is the statement after the fork function. ip address. If it is execute, the ip address is specified by the compiler. No matter what, when the process starts executing, the CPU will parse cs:ip and get an instruction to execute. So how is cs:ip parsed?
When the process is executed, the tss selector (GDT index) is loaded into the tss register, and then the context in tss is also loaded into the corresponding register, such as cr3, ldt selector. According to the ldt index in the tss information, first find the first address of the process ldt structure data from GDT, and then obtain the selector from cs according to the attributes of the current segment, such as the code segment, and the system obtains the first address of the process linear space from the ldt table. Address, length limit, permissions and other information. Use the first address of the linear address plus the offset in the IP to obtain the linear address, and then obtain the physical address through the page directory and page table. If the physical address has not been allocated, page fault exceptions and other processing will be performed.

3 Process suspension and wake-up

Process suspension, blocking, and multiple processes. We usually hear these concepts quite often, now let’s see how they are implemented. There are two types of process suspension or blocking.
1 Actively suspend. Let the process hang intermittently through sleep. The principle of sleep has been analyzed before, so I will not analyze it again. The general principle is to set a timer and wake up the process after expiration.

  • Modify the process to a suspended state and wait for wake-up.

  • 2 Passive suspension.

    There are many scenarios of passive suspension, mainly when the process applies for a resource, but the resource does not meet the conditions, then the process is suspended by the operating system. For example, when we read a pipe. If there is no data to read from the pipe, the process is suspended. Insert into the pipe's waiting queue.



When the pipe has content written, the process is awakened. The process is suspended (divided into two types: those that can be awakened by signals and those that cannot be awakened by signals) and the implementation of wake-up.
mean? Please provide more context.
<code>// 当前进程挂载到睡眠队列p中,p指向队列头指针的地址<br>void sleep_on(struct task_struct **p)<br>{<br>    struct task_struct *tmp;<br><br>    if (!p)<br>        return;<br>    if (current == &(init_task.task))<br>        panic("task[0] trying to sleep");<br>    /*<br>        *p为第一个睡眠节点的地址,即tmp指向第一个睡眠节点<br>        头指针指向当前进程,这个版本的实现没有采用真正链表的形式,<br>        他通过每个进程在栈中的临时变量形成一个链表,每个睡眠的进程,<br>        在栈里有一个变量指向后面一个睡眠节点,然后把链表的头指针指向当前进程,<br>        然后切换到其他进程执行,当被wake_up唤醒的时候,wake_up会唤醒链表的第一个<br>        睡眠节点,因为第一个节点里保存了后面一个节点的地址,所以他唤醒后面一个节点,<br>        后面一个节点以此类推,从而把整个链表的节点唤醒,这里的实现类似nginx的filter,<br>        即每个模块保存后面一个节点的地址,然后把全局指针指向自己。<br>    */<br>    tmp = *p;<br>    *p = current;<br>    // 不可中断睡眠只能通过wake_up唤醒,即使有信号也无法唤醒<br>    current->state = TASK_UNINTERRUPTIBLE;<br>    // 进程调度<br>    schedule();<br>    // 唤醒后面一个节点<br>    if (tmp)<br>        tmp->state=0;<br>}<br><br>// 唤醒队列中的第一个节点,并清空链表,因为第一个节点会向后唤醒其他节点<br>void wake_up(struct task_struct **p)<br>{<br>    if (p && *p) {<br>        (**p).state=0;<br>        *p=NULL;<br>    }<br>}</code>

We found that the implementation of the process is similar to how we usually write code, which is to define the data structure and then implement the algorithm to operate the data structure. Of course, because it involves the underlying hardware, the implementation of the operating system is much more complicated than our code.


The above is the detailed content of mean? Please provide more context.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete