"Operating System" process management (II)

Source: Internet
Author: User
Tags mutex semaphore switches terminates ptad

First, preface

The various modules of the operating system have been introduced before, and now we will learn more about process management in the operating system.

Ii. Basic concepts of the process

In systems that do not have an OS configured, the program executes sequentially, that is, one program must be executed before another program is allowed to execute, and in a multi-channel program environment, multiple programs are allowed to execute concurrently. It is also the concurrent execution of the program that leads to the introduction process.

2.1 Sequential execution of programs

It is usually possible to divide an application into several segments, which must be executed in a sequential order between the sections of the program, and only after the current operation (the program segment) has been executed to perform subsequent operations. The print task, such as the typical input-and-compute, is sequential execution.

The following features are performed when the program is executed sequentially

① sequence, the operation of the processor is performed strictly in the order specified by the program lock, i.e. each operation must begin after the last operation.

② Sealing, the program is executed in a closed environment, that is, the program runs exclusive full machine resources, the State of the resources (except the initial state) only this program can change it, once the program starts execution, its execution results are not affected by external factors.

③ reproducibility, as long as the program executes the same environment and initial conditions, when the program is repeated execution, whether it is executed from start to finish, or a walk-stop execution, will achieve the same result.

The characteristics of the program execution are very convenient for the programmer to detect and correct the errors of the program.

2.2 Concurrent execution of programs

Multiple programs can be executed concurrently, and concurrent execution can improve CPU efficiency and system throughput. Its characteristics are as follows

① discontinuity, when the program is executing concurrently, because they share system resources and cooperate with each other to accomplish the same task, the mutual constraint relationship is formed between these concurrent executing programs. If the calculation operation must be after the input operation.

② loss of closure, the program in the concurrent execution, is a number of programs to share the various resources in the system, so that the state of these resources will be changed by a number of programs, but the operation of the program is not closed, so that a program at run time, will inevitably receive other program impact. If a program consumes processor resources, another program must wait.

③ is non-reproducible, and when the program is executed concurrently, it loses its reproducibility due to the loss of its closeness. may produce different results due to different sequence of operations.

2.3 Characteristics of the process

Because the program executes concurrently, they lose the closed, intermittent and non-reproducibility, which determines that the general program can not participate in concurrent execution, because the results of the program execution is not reproducible. In this way, the operation of the program is meaningless, in order to be able to be the correct concurrency of the program, the introduction of the concept of the process. The process has the following characteristics.

① structure characteristics, in order to enable the program to run independently, you should configure a Process control block, that is, the PCB (Process Control block). and the procedure section, the Related Data section and the PCB three parts constitute the process entity. The so-called creation process essentially creates the PCB in the process entity, and the undo process is also the PCB in the undo process.

② dynamic, the essence of the process is an execution process entity, therefore, the dynamic is the most basic characteristics of the process, the process entity has a life cycle, and the program is just a set of ordered instructions, and stored in a certain medium (such as hard disk), which itself does not have the meaning of motion, Thus is static.

③ concurrency, multiple process entities are stored in memory and can be run concurrently over a period of time. Concurrency is an important feature of the process and also an important feature of the OS.

④ independence, the process entity is a basic unit which can run independently, allocate resources independently and accept the dispatch independently. Any non-PCB program can not be used as a separate unit to participate in the operation.

⑤ Async, processes move forward at their own, unpredictable speed, or process entities run asynchronously.

A process is a process entity that runs the process and is an independent unit of the system's resource allocation and scheduling (before a thread does not appear).

2.4 Status of the process

The discontinuity of the process execution determines that the process may have multiple states, the most basic three states are as follows

① ready state, when the process has been assigned to all the necessary resources except the CPU, as long as the CPU is obtained, it can run immediately, the status of the process is called the ready state. In a system, multiple processes may be in a ready state, usually lined up in a queue, known as a ready queue.

② execution State, the process has obtained the CPU and its program is executing. In a single processor system, only one process is in the execution state, and in multiprocessor system, multiple processes are in the execution state.

③ blocking state, a thread that is in execution cannot continue execution because of an event, discards the processor and is paused, at which point the state of the process is called the blocking state, or the waiting state or blocking state. such as IO requests, request cache space, and so on, the blocked process will also be queued and may be queued for different blocking reasons.

Description: Represents the conversion between three basic states.

In addition to the above three basic states, in some systems, a new suspend state, the reason for the introduction of the suspended state is as follows

① the end user's request, when the end user discovers suspicious problems during the operation of his own program, wants to temporarily quiesce his own program, even if the executing process suspends execution, and if the user process is in a ready state and is not executed, the process will not accept the schedule. So that users can study their execution or modify the program, this state is called a pending state.

② the parent process request, sometimes the parent process wants to suspend one of its child processes in order to examine and modify the child process, or to reconcile the activities between the child processes.

③ load regulation needs, when the real-time system workload is heavier, it may affect the real-time task control, the system can be a few unimportant processes hang, to ensure that the system can function properly.

④ the operating system needs, the operating system sometimes wants to suspend certain processes in order to check for resource usage or bookkeeping in the run.

After the introduction of the suspended state, the conversion of the following States is added

① active ready, still ready, is called the active-ready state when the process is in a ready state that is not pending, and when it is suspended, it becomes still-ready and the process in the still-ready state does not accept the schedule.

② active blocking, static blocking, when the process is in a blocked state that is not suspended, is called an active blocking state, and when it is suspended, it becomes a standstill blocking state, and the process in that state will change from a standstill block to a standstill after the event it expects to occur.

③ is still ready and active, activated using activation primitives.

④ static blocking-activity blocking, activated using activation primitives.

There are two common states for process management, namely the creation state and the terminating state.

① Create a state, creating a process typically takes two steps. First, a common PCB for a new process, and fill in the necessary management information, second, the process into a ready state and inserted into the ready queue. When a new process is created, the system assigns it a PCB, fills in the process flags and other information, but because the process is necessary for the resources or other information (such as memory) has not been allocated, at this time, the process has a PCB, but it does not enter the memory itself, that is, the creation of the work has not been completed, the process can Its state is the creation state.

② terminates the state, and the process terminates in two steps. First, wait for the operating system to do the aftercare, second, its PCB cleared, and the PCB space back to the operating system. The process that enters the terminating state cannot be executed anymore, but it retains a record in the operating system, where the status code and some timing statistics are saved for other processes to collect, and the operating system will delete the process once other processes have finished extracting the information from the terminating state process.

Description: The above is a conversion between seven states.

2.5 Process Control block

The role of the 2.5.1 Process control block

In order to describe and control the operation of the process, the system defines a data structure for each process, a Process control block PCB, which is part of the process entity, is the most important record data structure in the operating system, the PCB records the operating system required to describe the current situation of the process and control the process of the operation of all the information.

The role of the process control block is to enable a program that can run independently in a multi-channel program environment, becoming a unit that can run independently, and a process that can execute concurrently with other processes. In other words, the OS is based on the PCB to control and manage the concurrent execution of the process. The PCB is the unique identification of the process presence.

When creating a process, create a PCB for it, the process at the end of the recycling of its PCB, the process then dies, the PCB can be read or modified by multiple modules in the operating system, such as the Scheduler, resource allocation program, interrupt processing program and monitoring analysis program, such as read or modified, because the PCB is often accessed by the system, In particular, the high frequency of the process and the dispatch program Access, the PCB should be resident memory. The system organizes all the PCBs into several lists (or queues), which are stored in a specially opened PCB area within the operating system.

2.5.1 information for Process Control blocks

The process Control block PCB mainly contains the following information.

The ① process identifier, which uniquely identifies a process, has an internal identifier (a unique number given by the system, usually a sequence number for a process, for ease of use by the system), and an external identifier (provided by the creator to describe the family relationship of the process).

② processor state, when the processor is interrupted, its register information must be stored in the process of the PCB, so that the process re-execution, can continue to execute from the breakpoint.

③ process scheduling information, including the status of the process (indicating the current state of the process, as a basis for process scheduling and swapping), process prioritization (used to describe the priority level of the process using the processor, high priority processes should take precedence over the processor), and other information required for process scheduling (in relation to the process scheduling algorithm, such as the sum of the time the process has been waiting for the CPU, the sum of the time the process has been executing, and so on), the event (the event that the process is transitioning from execution state to the blocking state waiting for the cause to occur).

④ Process Control information, including the address of the program and data (the memory or external memory of the process's program and data, so that it can find its program and data from the PCB when the process is dispatched), process synchronization and communication mechanisms (mechanisms necessary to implement process synchronization and process communication, such as Message Queuing pointers, semaphores, etc.), A list of resources (all the resources required for a process other than the CPU and a list of resources that have been assigned to the process), the link address (the first address of the PCB for the next process in the queue where this process is located).

2.5.3 how process Control blocks are organized

In order to effectively manage the PCB, these PCBs should be organized in the appropriate way, commonly used in the organization of the following

① Link, the same state of the PCB, linked to a queue, so that the formation of a number of ready queue, blocking queue and blank queue, and so on, the priority of the high-level process of the PCB line in front.

② index, the system establishes several index tables based on the state of all processes, such as ready index tables, blocking index tables, etc., and records each index table at the first address of memory in some dedicated units of memory, and in each Index table table, records the address of a PCB with corresponding status in the PCB table.

Third, Process Control

3.1 Creation of the process

A process can create a child process that inherits the resources owned by the parent process, such as inheriting the file that the parent process opened, the buffer allocated to it, and so on, when the child process is revoked, the resources it obtains from the parent process are returned to the parent process, and all its child processes must be revoked at the same time when the parent process is revoked.

The events that caused the creation process are as follows

① User Login. ② job scheduling. ③ provides services. ④ application Request.

  process Creation steps are as follows

① apply for a blank PCB, request a unique digital identifier for the new process, and request a blank PCB from the PCB collection.

② allocates resources for the new process, allocating the necessary memory space to the program and data of the new process and to the user stack.

③ initialization Process Control block, the initialization of the PCB includes: Initialize the identity information, the system assigned identifier and the parent process identifier into the new PCB; Initialize the processor state information, so that the program counter points to the entry address of the program, so that the stack pointer to the top of the stack; Initialize the processing machine control information, Sets the state of the process to either a ready state or a still-ready state.

④ inserts a new process into the ready queue and inserts the new process into the ready queue if the process-ready queue is able to accommodate the new process.

3.2 Termination of the process

The event that caused the process to terminate is as follows

① The normal end, in any computer system, there should be an indication that the process has completed running.

The ② ends abnormally, forcing the process to terminate during a process run due to certain errors and failures. such as an out-of-bounds error (the area of the process to which the program is accessing), a protection error (the process attempts to access a resource or file that is not allowed to access, or is accessed in an inappropriate manner, such as writing a read-only file), an illegal instruction (the process attempts to execute a nonexistent instruction), The privileged instruction is wrong (the process is attempting to execute a command that only allows the OS to execute), waiting for a timeout (the process is waiting for the event to exceed the specified maximum), the arithmetic operation is wrong (the process is attempting to perform a forbidden operation, such as being removed by 0), I/O failure (Error occurred during I/O).

③ outside intervention, the process terminates the operation at the request of the outside, such as an operator or operating system intervention, the parent process request (the parent process has the ability to terminate any descendant processes), the parent process terminates (the parent process terminates, and all descendant processes are terminated).

  The steps to terminate the process are as follows

① retrieves the PCB of the process from the PCB collection summary, based on the identifier of the terminated process, and reads out the status of the process.

② If the terminated process is in the execution state, the execution of the process should be terminated immediately, and the dispatch flag bit true, indicating that the process should be dispatched after it has been terminated.

③ If the process also has descendants, the process of its descendants should be terminated to prevent them from becoming an uncontrolled process.

④ all resources owned by the process to be terminated, either returned to its parent process or returned to the operating system.

⑤ the terminated process PCB from the queue or linked list, waiting for other programs to collect information.

3.3 Blocking and awakening of processes

Events that cause the process to block and wake up are as follows

① requests the system service, when the executing process requests the operating system to provide services, and for some reason the operating system does not immediately meet the requirements of the process, and the process can only be turned into a blocking state to wait.

② initiates an action that, when the process initiates an action that must not continue until after the operation has completed, the process must be blocked to wait for the operation to complete.

③ new data has not yet arrived, and for the process of mutual cooperation, if one process needs to obtain the data provided by another cooperation process before processing the data, the process only (waiting) is blocked as long as its required data has not arrived.

④ There is no new work to do, the system often sets up some system processes with certain features, and whenever the process completes its task, it blocks itself to wait for new tasks to arrive.

  The process blocking steps are as follows

When the executing process finds one of these things, because it cannot continue running, the process blocks itself by calling block primitives, so that the process is blocking itself as an active behavior of the process itself, after which the process stops executing and changes the state of the process control block from execution to blocking and inserts the PCB into the blocking queue , if multiple blocking queues that are blocked by different events are set up in the system, the process should be plugged into a blocking queue with the same events, and finally, the scheduler will reschedule, assigning the processor to another ready process and switching. That is, the processor state of the blocked process is retained in the PCB, and then the CPU environment is set in the processor state of the new process's PCB.

  process Wake-up steps are as follows

When the time that is expected for the blocked process to occur, such as I/O completion receives the data it expects to arrive, the wake-up primitive wakeup is invoked by the process concerned (such as a process that runs out and releases the I/O device), wakes the process waiting for the event, and first moves the blocked process out of the blocking queue awaiting the event. Change the current state of its PCB from blocked to ready, and then insert the PCB into the ready queue. It should be noted that theblock primitives and wakeup reasons should be executed in different processes .

3.4 Suspend and activate of a process

  process suspend steps are as follows

When an event that causes a process hang occurs, such as a user process request to suspend itself or a parent process request to suspend a child process of its own, the system uses the suspend primitive suspend to suspend the specified process or the process in the blocking state. Check the status of the suspended process, if it is in an active state, change it to still ready, for the active blocking state of the process, it will change to a static block, if the suspended state is executing, then the scheduler to reschedule.

  The process activation steps are as follows

When an event occurs for an activation process, for example, a parent process or a user process request to activate the specified process, if the process is resident and there is sufficient space in memory, the process that is in a external memory state is swapped into memory, and the activation primitive active will be used to activate the specified process. Activate the primitive language first the process from the external memory into memory, check the current state of the process, from the corresponding static state to active state, if the use of preemptive scheduling policy, there is a new process such as ready queue, should check whether to reschedule, that is, the scheduler will be activated by the process and the current process to prioritize the comparison, If the priority of the activated process is lower, there is no need to reschedule, otherwise, immediately deprive the current process of running, the processor is allocated to the newly activated process.

Iv. Synchronization of processes

Process synchronization is mainly the coordination of multiple related processes in the order of execution, so that the concurrent execution of the processes can effectively share resources and mutual cooperation, and from the execution of the program can be reproducible. In the multi-channel program environment, when the program executes concurrently, due to the resource sharing and process cooperation, the following two forms of restrictive relationships may exist between the processes in a system.

① indirect mutual restriction relationship, the process in a system, usually share some system resources, such as shared CPU, I/O devices, indirect mutual restriction is originated from this kind of resource sharing.

② directly to each other, this constraint mainly stems from inter-process cooperation, such as a process through the buffer to the B process to provide data, when the buffer is empty, B is blocked, after a input data, B is awakened, buffer full, a block, after the data is taken out, a is awakened.

4.1 Critical Area

Many hardware resources, such as printers, tape drives, and so on, are critical resources, and the processes should be mutually exclusive ways to achieve the sharing of such resources. The code that accesses the critical resources in each process becomes the critical section, and it is obvious that the mutually exclusive access to the critical resources can be realized if the process is guaranteed to enter its own critical section.

4.2 Principles followed by the synchronization mechanism

① idle let in, when no process is in the critical section, indicating that the critical resource is idle, should allow a request to enter the critical section of the process immediately into its own critical area, in order to effectively utilize critical resources.

② busy is waiting, when the existing process into the critical section, indicating that critical resources are being accessed, so that other views into the critical section of the process must wait to ensure mutually exclusive access to critical resources.

③ Limited wait, to require access to critical resources of the process, should be guaranteed to enter their own critical area within a limited time limit to avoid falling into the death state.

④ the right to wait, when the process can not enter its own critical section, should immediately release the processor, so as to avoid the process into a busy state.

4.3 Process synchronization process issues

1. Producer/Consumer Issues

2. The question of the Philosopher's meal

3. Read and Write issues

V. Process Communication

Process communication refers to the exchange of information between processes, mutual exclusion and synchronization of processes, because only a small amount of information can be exchanged for low-level communication, the current advanced communication mechanism can be attributed to three major categories

① Shared Memory System

The process of communicating with each other can share some data structures or shared storage areas, the process can communicate through these spaces, and based on this, there are two types: the communication mode based on shared data structure, in which the processes are required to share some data structures, so as to realize the exchange of information between processes. Based on shared storage, in order to transfer large amounts of data, a shared storage area is drawn out in memory, and the process can communicate by reading or writing the data in the shared storage area.

② Message delivery system

Inter-process data exchange is a formatted message unit, the programmer directly using the operating system provides a set of communication commands (primitive), not only can achieve a large number of data transfer, but also hides the implementation details of the communication, so that the communication process is transparent to the user, thereby greatly reducing the complexity of communication programming.

③ Pipeline Communication

Connect a read process and a write process to achieve the communication between them a shared file, also known as pipe file, to the pipeline (shared file) to provide input to the sending process, a character stream in the form of a large amount of data into the pipeline, and accept the pipeline output of the acceptance process from the pipeline to accept data, Because the sending and receiving processes communicate using pipelines, they are called pipeline communications. Pipeline communication requires three aspects of coordination: mutual exclusion (when a process is reading/writing to the pipe, other processes must wait), synchronization (when the write process writes a certain amount of data to the pipe, it goes to sleep wait, after the reading process takes the data, then wakes it up, when the reading process reads an empty pipe, You should also sleep until the data is written to the pipeline to wake it up, to determine if the other party exists, and to communicate only if it is determined that the other person already exists.

Six, Thread

  6.1 Thread-to-process comparison

A thread is called a lightweight process or process element, and in an operating system that introduces a thread, a process often has multiple threads, at least one thread, and the following compares threads to processes from different aspects (provided the operating system introduces threads).

① Scheduling

Threads act as the basic unit of dispatch and dispatch, while processes act as the basic unit of resource ownership. In the same process, the switch of the thread does not cause the process to switch, but when a thread in one process switches to a thread in another process, it causes the process to switch.

Concurrency of ②

Not only can the process be executed concurrently, but also concurrently between multiple threads in the same process, the operating system has better concurrency, which can improve system resource utilization and system throughput more effectively.

③ has resources

A thread generally does not have system resources (except for a small amount of essential resources), but it can access the resources of its subordinate processes, that is, the code snippet for a process, the data segment, and the system resources it has, such as open files, I/O devices, and so on.

④ system Overhead

When creating and revoking processes, the system creates and reclaims process control blocks, allocates or reclaims resources, and the operating system pays significantly more than the cost of thread creation or revocation, and when the process switches, it involves the saving of the current process CPU environment and the setting of the CPU environment of the newly scheduled running process. While the switch of the thread only need to save and set a small amount of register content, do not involve the operation of memory management, so the switching cost of the thread is much smaller than the process switching cost, besides, because of the same address space in one process, it is easier to implement synchronization and communication, in some operating systems, thread switching , both synchronization and communication require no intervention from the operating system kernel.

6.2 Properties of a thread

① lightweight entities, each of which has a thread control block TCB for controlling thread runs, a program counter that indicates the sequence of instructions being executed, a local variable is reserved, a small number of state parameters are returned to the address, and so on.

② is the basic unit of independent Dispatch and dispatch, and the thread is the basic unit capable of running independently, switching very quickly and with little overhead.

③ can be executed concurrently, and threads of a process and different processes can execute concurrently.

④ shared process resources, the threads in the same process share the resources owned by the process, and all threads have the same address space (the process's address space), which means that the thread can access every virtual address in that address space, and also access the open files, timers, semaphores, etc. owned by the process.

6.3 Status of the thread

① state parameters, each of which can be described with a thread identifier and a set of state parameters. The state parameters include the register state (including the contents of the program counter and the stack pointer), the stack (the saved local variable and return address), the thread run state, the priority, the thread-specific memory (the save thread has its own copy of the variable), and the signal mask (masking some information).

② the running state of a thread, which includes the execution state, the ready state, and the blocking state.

6.4 Creation and termination of threads

When you create a new thread, you need to create a function (or system call) with a thread, and according to the corresponding parameters, such as the entry pointer to the thread's main program, the size of the stack, the priority of the schedule, and so on. After the thread creation function finishes executing, a threading identifier is returned for later use.

There are two ways to terminate a thread, either voluntarily when the thread finishes working, or when the thread is running in error or is forcibly terminated by another thread for some reason. In most OSes, a thread is terminated without immediately releasing the resources it occupies, and only when other threads in the process have performed the detach function, the terminated line friend is separated from the resource, at which point the resource can be exploited by other threads. Although a thread that has been terminated but has not yet freed the resource, it can still be called by the thread that needs it so that the terminated thread resumes running.

6.5 Processes in the multi-threaded OS

① as a unit of system resource allocation, the resources in either process include the user address space that is protected separately, the mechanism used to implement inter-process and inter-thread synchronization and communication, the open file and the requested I/O device, and an address mapping table maintained by the core process. This table is used to implement the mapping of the user program's logical address to the memory address.

A ② can include multiple threads, a process that has at least one thread, and a process that provides resources and a running environment for the threads that can be executed concurrently. All threads can only be subordinate to a particular process.

The ③ process is not an executable entity, and the process still has an execution-related state, such as execution state, which indicates that a thread in the process is executing, the process-state-related actions imposed on the process, and the role of its thread, for example, when a process is suspended, all threads in the process are also suspended. When a process is activated, all threads that belong to the process are also activated.

6.6 Synchronization and communication between threads

① mutex, mutex is a relatively simple mechanism to implement mutually exclusive access to resources between threads. Because the time and space overhead of operating mutexes is low, it is more suitable for critical shared data and program segments used by high frequencies.

The ② condition variable, when creating a mutex, is associated with a condition variable, a simple mutex is used for short-term locking, which is mainly used to guarantee mutual exclusion of the critical section, and the condition variable is used for the long wait of the thread until the resource being waited for becomes available.

③ semaphore mechanisms, including private semaphores (when different threads of the same process need to synchronize, the command that creates semaphores can be called to create a private semaphore, whose data structure resides in the application's address space, where the private semaphore belongs to a particular process, and the OS does not know the existence of a private semaphore, When the occupier of a private semaphore is abnormal or ends normally, but does not release the space occupied by the private semaphore, the system will not be able to restore it to 0, nor will it be able to transfer it to the next thread requesting it, and the public semaphore (which is set to synchronize between threads in different processes or processes). Used by all processes, its data structure is placed in the hand-protected system store, allocated by the OS for it and managed, also known as the system semaphore, the OS will automatically reclaim its space, it is a more secure synchronization mechanism)

6.7 How threads are implemented

① Kernel support thread (Kernel supported Threads)

Regardless of the thread in the user process or the threads in the system process, their creation, revocation, and switchover are also dependent on the kernel for implementation in kernel space. Kernel space sets up a thread control block for each kernel support thread, and the kernel senses the existence of a thread based on that control block and controls it. The advantages are as follows

1. In multiprocessor system, memory can simultaneously schedule multiple threads in the same process to execute concurrently.

2. One thread in the process is blocked, the kernel can dispatch other threads in the process, or it can run threads from other processes.

3. Kernel support threads have very small data structures and stacks, with fast thread switching and low overhead.

4. The kernel itself uses multithreading technology to improve the execution speed and efficiency of the system.

The disadvantage is that the switching overhead is significant for the user's thread switching because of the need to switch modes.

② User Level Thread

User-level threads exist only in user space. For thread creation, revocation, synchronization, and communication, there is no need to make use of system calls, and switching to user-level threads typically occurs between many threads of an application process. The task control block of a thread is set in user space, and the operation performed by the thread does not require the help of the kernel, so the kernel does not know the existence of the user-level thread at all. A system that has a user-level thread set can still be scheduled in process, and the kernel support thread is set to be dispatched on a thread-by-unit scale. The advantages are as follows

 1. Thread switching does not require conversion to kernel space, saving switching overhead.

2. The scheduling algorithm can be process-specific, the process can choose different scheduling algorithms to manage and dispatch their own threads, and the operating system of the low-level scheduling algorithm independent.

3. The implementation of user-level threads is independent of the operating system platform and can be shared across all applications.

The disadvantages are as follows

1. The blocking problem with system calls, when a thread executes a system call, not only is the thread blocked, but all threads within the process are blocked, and in the kernel support thread mode, other threads in the process can still execute.

2. In a simple user-level threading implementation, multi-threaded applications cannot take advantage of multiprocessing for multiprocessing, and the kernel allocates only one CPU per process each time, so only one thread in the process can execute, and other threads can wait until the thread discards the CPU.

③ Combination Mode

The kernel supports the establishment, scheduling, and management of multi-KST threads, while also allowing user application resumes, scheduling, and managing user-level threads.

6.8 Implementation of Threads

① Kernel support thread implementation

When the system creates a new process, it assigns a task data area, Ptad (Per Task), which includes several thread control block TCB spaces, which are stored in kernel space. When a process is creating a new thread, it assigns a TCB, fills in the relevant information, allocates the necessary resources, and the newly created thread has the opportunity to execute. When all TCB in Ptad is allocated, the process will create a new thread, and the system can allocate a new TCB space as long as the number of threads it creates does not exceed the allowable value of the system. When a thread is revoked, all the resources and TCB of that thread are also reclaimed, and some systems, in order to reduce the overhead of creating and revoking a thread, do not immediately reclaim the resource and TCB of that thread when a thread is revoked, and when a new thread is created later, Can take advantage of a thread that has been revoked but still maintains resources and TCB as a new thread.

② implementation of user-level threading

User-level threads are implemented in user space, all user-level threads have the same structure, they all run on an intermediate system, and there are currently two ways to implement the intermediate system, the runtime system and the kernel control thread.

The runtime system is a collection of functions (procedures) for managing and controlling threads, including functions for creating and revoking threads, functions for thread synchronization and communication, and functions for implementing thread scheduling, because these functions make user-level threads independent of the kernel, and all functions in the runtime system reside in user space. And as the interface between the user-level thread and the kernel. User-level threads do not need to switch to a kernel mentality, but instead are switched by the thread switching process in the runtime system.

The kernel control thread, also known as the lightweight process LWP (light Weight process), can have more than one LWP per process, and as with user-level threads, each LWP has its own data structure, such as the TCB, which includes thread identifiers, priorities, states, stacks, local stores, and so on. They can also enjoy the resources owned by the process, which can be used by the system call to get the service provided by the kernel, and when a user-level thread runs, it has all the attributes of the kernel to provide the support thread, as long as it is connected to an LWP. The LWP is made into a buffer pool where user-level threads can be connected to any LWP, so that each user-level thread can use the LWP to communicate with the kernel, allowing multiple user-level threads to multiplex an LWP, but only the threads currently connected to the LWP can communicate with the kernel, the remaining processes or blocks, Or wait for the LWP, each LWP also need to connect to a kernel-level thread, so that through the LWP can connect the user-level thread with the kernel thread, the user-level thread can access the kernel through LWP, the kernel does not see the user-level thread, LWP implements the kernel and user-level thread isolation.

③ connection of user-level threads to kernel-controlled threads

1. One-to-one model

A kernel control thread is set up for each user thread to connect, and when one thread is blocked, another thread is allowed to run, and in multiprocessor system, multiple threads execute in parallel.

2. Multiple-to-one model

To map multiple user threads to a kernel control thread, for administrative convenience, these user threads typically belong to a process that runs in the user space of the process, and the scheduling and management of those threads is also done in the user space of the process, which is mapped to a kernel control thread when the user thread needs to access the kernel. But only one thread is allowed to map at a time. Thread management overhead is small and efficient, but when a thread accesses the kernel, the entire process is blocked, and in multiprocessor system, multiple threads of a process cannot implement parallelism.

3. Many-to-many models

Mapping multiple user threads to multiple kernel control threads, the number of kernel-controlled threads can vary depending on the application process and system.

Vii. Summary

Introduction to the process and the thread is here, the process and the thread is the operating system inside the very core concept, understand these concepts for the later learning will be very good, thank you for the view of the Garden friends ~

"Operating System" process management (II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.