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

What is the execution and suspension of the Java process

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "what is the implementation and suspension of the Java process", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the execution and suspension of the Java process?"

1 Overview of progress

Process is an abstraction of logic. We have learned a lot about the process from the books of the operating system, but we may not know much about the implementation of the process. This article attempts to explain the general principle 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. The process is no exception. So the essence of a process is a data structure, which stores a series of data. The operating system is managed in the form of arrays or linked lists and all processes. It can be said that there are two kinds of processes

1 the first process during system initialization

2 except for the first process, they are all created by fork or fork+execute system calls.

Let's first take a look at the information about the structure of the process.

The above is the main information in the structure of the process. Then a structure represents a process. We know that fork takes the parent process as a module, copies a copy of the structure of the parent process, and then modifies some fields. It becomes a new process. If you call execute, you are further modifying the fields in the copied structure (such as page tables, code snippets, data segments). And load the corresponding data from the hard disk into memory. So how did the first process come about? Because a process is just a structure, if we predefine a structure, we can create a process without using fork.

2 execution of the process

When the system creates a process, it sets the value of the cs:ip register, and if it is fork, ip is the ip address of the statement that follows the fork function. If execute, the ip address is specified by the compiler. In any case, when the process starts to execute, cpu parses the cs:ip and gets 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 the tss is also loaded into the corresponding register, such as the cr3,ldt selector. According to the ldt index in the tss information, the first address of the process ldt structure data is first found from the GDT, and then according to the attributes of the current segment, such as the code segment, the selector is obtained from the cs, and the system obtains the first address, limit length, permission and other information of the process linear space from the ldt table. The linear address is obtained by using the first address of the linear address plus the offset in ip, and then the physical address is obtained through the page directory and page table. If the physical address is not assigned, the page fault exception is carried out.

3 suspension and awakening of the process

Suspending, blocking, and multiple processes of a process. We usually hear a lot about these concepts, and now let's take a look at how they are realized. There are two types of process suspensions, or blocking.

1 suspend actively. Suspend the process intermittently through sleep. If the principle of sleep has been analyzed before, it will no longer be analyzed. The general principle

Is to set a timer to wake up the process when it expires.

The modification process is in a suspended state, waiting to 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 the pipe has no data to read, the process is suspended. Insert into the waiting queue of the pipe.

When something is written in the pipe, the process is awakened. The process is suspended (divided into two types that can be awakened by a signal and cannot be awakened by a signal) and the implementation of awakening.

/ / the current process is mounted to the sleep queue p, which points to the address of the queue header pointer

Void sleep_on (struct task_struct * * p)

{

Struct task_struct * tmp

If (! P)

Return

If (current = & (init_task.task))

Panic ("task [0] trying to sleep")

/ *

* p is the address of the first sleep node, that is, tmp points to the first sleep node

The header pointer points to the current process, and this version of the implementation is not in the form of a real linked list

It forms a linked list of temporary variables in the stack of each process, each sleeping process.

There is a variable in the stack that points to the next sleep node, and then points the header pointer of the linked list to the current process.

Then switch to another process to execute, and when awakened by wake_up, wake_up wakes up the first part of the list

Sleep node, because the address of the latter node is saved in the first node, so he wakes up the latter node.

The latter node, and so on, wakes up the nodes of the entire linked list, and the implementation here is similar to nginx's filter

That is, each module saves the address of the next node, and then points the global pointer to itself.

, /

Tmp = * p

* p = current

/ / uninterruptible sleep can only be awakened by wake_up, even if there is a signal.

Current- > state = TASK_UNINTERRUPTIBLE

/ / process scheduling

Schedule ()

/ / Wake up the back node

If (tmp)

Tmp- > state=0

}

/ / Wake up the first node in the queue and clear the linked list, because the first node wakes up the other nodes backwards

Void wake_up (struct task_struct * * p)

{

If (p & * p) {

(* * p) .state=0

* p=NULL

}

}

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

At this point, I believe you have a deeper understanding of "what is the execution and suspension of the Java process". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report