In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to understand web processes and threads", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand web processes and threads.
Processes and threads are important concepts in the operating system, but everything is implemented in code. Seemingly complex process threads are actually in the code of the operating system. It's just some data structures and algorithms. It's just that it may be a little more complicated than ordinary data structures and algorithms. But the learning method is still the same, that is, go deep into the source code and find out.
In the operating system, processes are represented by a task_struct structure. Because most of the operating system is implemented in C language, there is no concept of object. Each process is an object if we understand it in a high-level language. Each time you create a new process, you create a new object. The task_struct structure can be said to be the definition of a class. Let's take a look at a definition of task_struct.
Insert a picture description here
We can see that the task_struct structure actually maintains some information needed by a process, including execution status, execution context tss, open files, root directory, working directory, received signals, signal processing functions, code segments, location of data segments, process id, execution time, exit code, and so on. Let's take a specific look at the role of these data structures.
Management of process
An task_struct array or linked list is maintained in the operating system to record all processes in the current system. Every time a process is created, a task_struct structure is appended to it, and each time a process is destroyed, the parent process of the process deletes the corresponding task_struct.
Scheduling of processes
The operation of the operating system is largely driven by the clock, and there is a hardware in the computer that intermittently produces clock interruptions. The interrupt interval is determined by the operating system when initializing the hardware (the timer is also driven by the hardware, and the timer function we use in the application layer is realized by using the system timer in the final analysis). Every time the clock is interrupted, if the currently executed process time slice has expired, process scheduling will occur. In addition, process scheduling also occurs when the process is blocked. When the process is scheduled, the system will load the tss information in task_struct into cpu. Including the currently executed code location, the values of various registers. Then the switching of the process is completed.
Execution time of the process
Each time the clock is interrupted, the clock interrupt handler accumulates the execution time of the current process, the execution time of the process we usually view, and these data are recorded by these fields. The execution time of a process in kernel mode and user mode is calculated separately.
Signal
The signal-related function is realized in three fields in task_struct, one is signal, which records the signal received by the process and is calculated by bit. Blocked is to record which signals the current process does not receive. Sigaction records the corresponding processing function of each signal, which corresponds to each signal one by one. Every time we call kill, we actually change the value of the signal field. Then, at some point, the system executes the corresponding function in sigaction. These opportunities include system call return, clock interrupt handler return, and other hardware interrupt return, and so on.
Status
Task_struct records the current state of the process with a field state, and exit_code records the exit code when the process exits.
File system related
The root and working directory of the current process. When we usually open a file in the process, if the absolute path is not specified, the system will spell out the absolute path based on the working directory and the relative path we pass. To find the file. In addition, the filp field is the file information opened by the maintenance process. The file descriptor we usually get is the index of the filp field. He will gradually find the underlying corresponding file or socket. Executable is the file information that holds the binaries corresponding to the process. We all know that a program loaded into memory becomes a process. What executable saves is the information of the corresponding file of the program.
Authority
Uid, euid, gid, egid and other fields are used in task_struct to record the permission information of the process.
Process relationship information
The pid field records the id,father of the current process and the id of the parent process. Pgrp,session,leader is group id, session id, and session leader, respectively. Multiple processes form a group, and multiple groups form a session. If a process is the leader of the group or session, its id will become the id of the group or session, such as
The id of group 1 with process an is 1 (group leader, session leader) the id of process b is 2.
The id of group 2 with process c is 3 (group leader), and the id of process d is 4.
If all processes are in one session, the group id and session id for all processes in group 1 are 1. Group 2 the group id for all processes is 3 and the session id is 1.
Execution context
Tss_struct and desc_struct structures record the context of process execution. Every time a process is switched, if it is scheduled for execution, the context is loaded into cpu and the corresponding hardware. If it is suspended, the information of cpu and hardware is saved to the context. Resume the next execution.
These are some of the properties of a process. We find that the process is not so difficult to understand, just as we usually define a person with name, height, and age attributes. For each object, he has some attributes of his own.
Let's take a look at threads again. Threads may be more difficult for many students to understand than processes. In fact, for the operating system, there is no separate implementation of the concept of threads, the operating system abstracts processes and threads into execution context. We can say that they are the same thing. But there's a little difference between them. Let's take the linuxthread library as an example. Find out what a thread is. We know that fork can create a new process. But this process is too heavy, although some properties can be shared. So the operating system reimplements a system call clone. He supports finer-grained attribute sharing. So we call threads lightweight processes. As the name implies, a thread is also a process, but it is lightweight because many of its properties are shared with the parent process (parent thread). Shared properties can be controlled by parameters of the clone function. When we create a new thread, a new process is created in the system. The clone function tells the system where the stack is located and where the code is executed (that is, the function we pass in). The system starts with the functions we define. It's about as follows.
At this point, I believe you have a deeper understanding of "how to understand web processes and threads". 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.
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.