Threads and the other side of the process

Source: Internet
Author: User
Tags posix terminates

Haven't come to the garden to write an article, today I share an article about the process and threading theory related content, many people are using multi-threaded, multi-process or thread pool, process pool, but you do not know why to use, from what to consider. If you are not particularly clear, it is recommended that you read it.

The process of operating system

First, the concept of process

Process concept is one of the most basic and important concepts in the operating system. A process is an executing program or a procedure that can be described as a program that can be executed concurrently on a  A process is not just a program code, which is sometimes called a text segment (or code snippet). The process also includes the current activity, the value of the program counter, and the contents of the processor register. In addition, processes typically include process stack segments (including temporary data, such as function parameters, return addresses, and local variables) and data segments (including global variables). A process may also include a heap (heap), which is the memory that is allocated dynamically during a process run.

From the point of view of operating system management, the process consists of data structure and the program executed on it, which is the running process of the program on this data set, and also the basic unit of resource allocation and dispatch of the operating system. The process therefore requires some resources, such as CPU time, memory space, files, and I/O devices. These resources can be assigned when the process is created, or they can be dynamically allocated during process runs.

As the concept of describing program execution process, the process is dynamic, independent, concurrency, and structural characteristics.

Dynamic is the most basic feature of a process, it is the process of program execution, it is a certain period of life. It is created by creation, executed by dispatch, temporarily ding due to resource not being made, and killed by undoing.

Independence means that the address space of each process is independent of each other, unless the inter-process communication means are used, otherwise it cannot affect each other.

Concurrency, also known as asynchrony, refers to the macroscopic view that processes are run independently at the same time.

Structure refers to the structure of the process address space, such as code snippets, data segments, and so on.

Processes and procedures are two closely related concepts that differ and relate in the following ways:

The process is dynamic and the program is static. A program is a collection of ordered code, and a process is the execution of a program. Process

It is not usually possible to migrate between computers, and programs usually correspond to files, static, and replication.

The process is temporary and the program is permanent. A process is a process of changing state, and a program can be saved for a long time.

The process differs from the composition of the program: the composition of the process consists of the program, data, and process control blocks (that is, process state information).

Processes are closely related to procedures. A program can correspond to multiple processes through multiple executions, and a process can include multiple programs by invoking a relationship. Processes can create other processes, and programs do not form new programs.

Second, the status and transformation of the process

The most important difference between a process and a program is its dynamics. As the process progresses, it will constantly change state. The process state is defined by the specific operating system based on the current activity of the process. For example, each process can be in the following state: Create, ready to run, wait (block), terminate.

Generally speaking process three basic state refers to the ready state, the running state and the waiting or blocking state

Processes that are not currently executing are placed in a wait queue. Which wait queue you are in depends on what resources or events the process is waiting for. Where the ready queue has its particularity, the process in the ready queue is waiting for CPU resources, and when the process waits for the CPU, it must have got all the other necessary resources.

When the process state changes, causing the process state transition at this time, it is necessary to cause the release of resources or applications. For example, when a process gets a CPU, it changes from a ready state to a running state, and a process changes from a running state to a wait state when it needs a resource but temporarily fails to get the resource.

A process is an abstract concept that cannot be "queued" in the waiting queue in person. In this "0/1" world of computers, processes require a specific representation to participate in management. This is the Process Control block (CONTROLBLOCK,PCB). Each process has a unique PCB corresponding to it. The process transitions between various queues, in fact the process PC B is converted in the corresponding queue; the PCB has pointers, and the PCB in the same queue is connected by these pointers. The PCB can be said to be the most complex data structure in the operating system, which contains all the unique information describing its corresponding process. Control of the process is actually the control of the process PCB.

The operating system needs to be responsible for the following tasks during the management process:

Creation and deletion of processes;

scheduling of processes;

Process synchronization, process communication, process deadlock processing, and so on.

Of course, all through the management of the PCB to achieve. The Process Control block stores information about a specific process, including:

Process state: The state may be new, ready, running, waiting, terminating, and so on.?

Program Counter: The counter indicates the address of the next instruction to be executed by the process.

CPU registers: Based on computer architecture, these registers differ in number and type. Includes accumulator, variable address register, stack pointer, universal register, etc. As with program counters, these state information must be saved when an interrupt occurs, so that later processes continue to execute correctly.

CPU scheduling information: Includes process priority, pointers to the dispatch queue, and other scheduling parameters.

Memory management information: includes base register and threshold register values, page table or Segment table, virtual address space management information, and so on.

Accounting information: Includes CPU time, actual usage time, number of accounts, number of jobs or processes, etc.

I/O status information: Includes a list of I/O devices assigned to the process, a list of open files, and so on. The collection of code and data for an executable file stored on disk is called an executable image (Ex ecut abl eimage). When a program is loaded into the system, it forms a process. A process consists of program code, data, and process control blocks. The process context is actually a static description of the process execution activity.

In particular, the process context includes the values of various registers in the system related to the execution of the process (e.g. general registers, program counter PCs, program status registers PS, etc.), the program's Machine instruction code set (or body segment), the dataset and various stack values and the PC B structure.

Context Switches

When the process scheduler chooses a new process and allocates the CPU for it, to switch the CPU to another process requires that the context of the original process be saved first and then loaded into the context of the new process. Switching the CPU to another process requires saving the state of the current process and restoring the state of another process, a task called context switch. That is, when a context switch occurs, the kernel saves the context of the old process on its PCB and then loads the saved context of the new process that is scheduled to execute. The context switch time is an additional overhead because the system does not perform any user process-required work when switching. The process context switch is implemented by an interrupt, and when an interrupt occurs, the system needs to save the context of the process currently running in the CPU so that it can recover the context after it has been processed, that is, to break the process before continuing. The process context is represented by the process's PCB, which includes the values of the CPU registers, process status, memory management information, and so on. Typically, you save the current state of the CPU by performing a state save (whether it is kernel mode or user mode), and then perform a state restore to start again.

Third, process operations and process communication

One, process operation computer system started without process. The operating system creates a 1th process, which is the ancestor of all processes. The ancestor process creates a new process, which is its child process. The parent process creates a child process that repeats so that it forms a tree-like structure of the process organization.

Processes in the system can execute concurrently, they must be dynamically created and terminated (revoked), and the operating system must provide a mechanism for process creation and termination. Operations that are part of the process control are used primarily for process creation, process undo, and transition control between process states.

(1) Process creation

Processes can be created by creating process system calls during the run process, which is its child processes, and each new process can then create additional processes that form a process tree. Creating a process is primarily creating a Process control block (PCB) for the new process. Create a process system call first request a blank PCB form from the system's PCB, and obtain its internal identification, and then the parameters provided by the calling process, such as the external name, body segment, the first address of the data segment, size, required resources, priority, etc. fill in this blank PCB table. and set the new process state to ready state, and put the PCB into the ready queue, you can enter the system concurrent execution.

When a process creates a new process, there are two possible types of executions:

The ① process executes concurrently with the child process.

② the parent process waits until one or all of the child processes have finished executing.

The address space of the new process also has two possibilities:

The ① process is a replica of the parent process (with the same program and data as the parent process).

The ② child process loads another new program.

To illustrate these different implementations, now look at UNIX class operating systems. In Unix, each process is identified by a unique, integer-type process identifier. A new process can be created through the fork () system call. The new process is made by copying the address space of the original process. This mechanism allows the parent process to communicate easily with the child process. Two processes (both parent and child) continue to execute instructions that lie after the system calls fork (). However, there is a difference: for a new (child) process, the return value of the system call Fork () is 0; For the parent process, the return value is the process identifier of the child process (not 0). Typically, after the system calls fork (), a process uses the system to call EXEC () to replace the process's memory space with a new program. The system calls exec () to load the binary file into memory and start execution. In this way, two processes can communicate with each other and execute according to their respective methods. The parent process can create more child processes, or if there is nothing to do when the child process runs, it takes the system call wait () to move itself out of the ready queue to wait for the child process to terminate.

The C program shown below illustrates the UNIX system invocation described above. There are now two different processes running the same program. The PID value of the child process is 0, and the parent process has a PID value greater than 0. The child Process EXECLP () (EXECLP () is a version of the system calling exec () by system call, and the Unix command/bin/ls (used to list files and directories) to overwrite its address space. The parent process waits for the child process to complete by calling wait () through the system. When the child process finishes (by explicitly or implicitly calling exit ()), the parent process resumes execution from the next instruction called by Wait () and calls the system call exit () to indicate the end.

(2) Process termination

For a tree hierarchy, the process system terminates the system call policy is issued by the parent process, terminates its child process and all descendants of the child process, all the resources of the terminated process (main memory, external memory, PCB table) all released to return the system, and to remove them from all the queue. If the terminated process is running, call the process scheduler to divide the processor into other processes.

(3) Blocking of the process

When the current process cannot execute because it requests an event (such as requesting I/O while waiting for I/O to complete), the process calls the blocking system call to block itself and temporarily abandons the processor. Process blocking is the active behavior of the process itself. The blocking process immediately stops the execution of the original program, changes the current state in the PCB from the operational state to the blocking state, and inserts the PCB into the blocking queue waiting for an event, and finally calls the process scheduler to reallocate the processing machine.

(4) Wake-up of the process

When an event that is expected by a blocked process occurs (such as when I/O is complete), a call to wake up the system called by a process (such as an I/O device handler or a process that frees the resource) wakes up the blocked process, moves the process waiting for the event from the blocking queue, and inserts it into the ready queue. Change the current status of the PCB in the process to ready state.

Inter-process communication

Inter-process communication (inter-process communication, IPC) the problem to be solved is that the information exchange between processes can be large and small. Operating system provides a variety of inter-process communication mechanisms, can be applied to a variety of different occasions, to pay special attention to the three kinds of inter-process communication: Shared storage mechanism, message delivery mechanism, pipeline communication mechanism.

(1) Shared storage System

In this mode of communication, a chunk of memory is drawn up for sharing by multiple processes, and the shared process communicates by reading or writing the data in this shared store. Shared storage is one of the fastest communication mechanisms for process communication and enables large amounts of data transfer.

(2) Message delivery system

In a messaging system, data exchange between processes is based on messages (message), which is called messages in a computer network. The message delivery system can be divided into two kinds: direct communication mode and indirect communication mode because of its different implementation methods. The messaging mechanism needs to provide at least two primitives send and receive,send send a message to a given target, and receive receives a message from a given source. In direct communication, each process that sends or receives a message must indicate to whom or from whom the message was sent, communicate between processes with the send primitive and the receive primitive, which are defined as follows: Send (P, message): Sends the message to the process preceive (Q, Message): Receiving the message process from process Q p and Q automatically establishes a connection by performing both operations, and this connection occurs only between the P and Q processes. Indirect communication is another way to deliver messages. In this case, the message is not sent directly from the sender to the receiver, but rather to the queue of shared data structures that are sent to the staging message, which is called the Mailbox (mail box). So two process communication situations, one process sends a message to a mailbox, and the other process extracts messages from the mailbox. The advantage of using indirect communication is that it increases the flexibility of using messages. The relationship between the sender and the receiver may be one-to-many, one-to-many, or many-to-many. A one-to-one relationship allows a dedicated communication link to be used for two inter-process interactions, and inter-process interactions are not affected by other process errors; a multi-pair relationship is particularly useful for client/server interactions, where a process provides services to multiple other processes (users), in which case the mailbox is often called a port A one-to-many relationship allows a sending process to interact with multiple receive processes, which can be used to broadcast messages to a set of processes.

(3) Pipeline communication

A pipe is a shared file, also known as a pipe file, used to connect a read process and a write process to communicate between them. A send process (that is, a write process) that provides input to a pipeline (a shared file), sends a large amount of data into the pipeline as a character, and the receiving process (that is, the read process) that receives the pipeline output can receive data from the pipeline. Because the sending process and the receiving process are using pipeline communication, it is also called pipeline communication. Pipeline communication is a kind of communication method based on the form of file system. Pipelines are divided into nameless pipes and two types of famous pipes. The nameless pipe is a temporary file that exists only in the file system and does not have a file path name on the structure and does not occupy the file directory entries. A well-known pipeline is a file that has a path name that exists in the file system for a long time, so that other processes can know it exists and can access the file using that path name.

Threads of the operating system

First, Thread basic concepts

A thread is defined as an execution unit within a process or a scheduler entity. In a process concept that does not own threads, a process is a separate unit of resources that allocates virtual address space, main memory, and others, and is a basic unit that can be dispatched and dispatched independently. After a thread is available, the resource-owning unit is called a process (or task), and the unit of dispatch is called a thread, also known as a lightweight process (light Weight PROCESS,LWP). If a process has multiple control threads, it can do multiple tasks at the same time.

A multithreaded process includes several different control flows within the same address space, which are threads under the same process that share resources owned by the process, such as code, data, files, and so on. Threads also monopolize some resources, such as stacks, program counters, and so on. The benefits of multithreaded systems include improved user responsiveness, in-process resource sharing, and the convenience of leveraging multiprocessor architectures.

Second, multithreaded models

From the implementation point of view, the thread is divided into user-level threads and kernel-level threads.

User-level threads are visible to programmers and unknown to the kernel, and the line libraries of user space is typically used to manage user-level threads, and line libraries provides support for thread creation, scheduling, and management. Because the kernel does not know the existence of user-level threads, all thread creation and dispatch work is done in user space, and the entire process is not interfered with by the kernel. Therefore, user-level threading is often created and managed quickly, however, they also have some drawbacks. For example, if the kernel is single-threaded, any user-level thread that executes a system call that causes blocking can cause the entire process to block, even if other threads inside the program run. Kernel-level threads are supported and managed by the operating system, and thread creation, scheduling, and management are implemented in kernel space. Because thread management is done by the operating system, kernel threads are created and managed slower than the user thread. However, because threads are managed by the kernel, if a thread executes a system call that causes blocking, the kernel can execute other threads in the scheduler. Similarly, in multiprocessor environments, the kernel can dispatch threads in different processors. Most modern operating systems support kernel threads.

There are three different models that associate user-level threads with kernel-level threads:

Many-to-one model maps multiple user threads to a kernel thread;

Single-to-one model maps each user thread to a corresponding kernel thread;

Many-to-many models switch multiple user threads between the same (or less) number of kernel threads.

Third, line libraries

Thread libraries provides programmers with the API to create and manage threads. There are two main ways to achieve line libraries. The first approach is to provide a library in user space that does not have kernel support, and all of the code and data structures for this library are present in the user space. Calling a function in the library only results in a local function call in user space, not a system call.

The second approach is to execute a kernel-level library that is directly supported by the operating system. At this point, the library's code and data structure exist in the kernel space. Calling an API function in the library typically results in system calls to the kernel.

The three main line libraries currently in use are: (1) POSIX Pthread, (2) Win32, (3) Java.

Pthread as a POSIX standard extension, you can provide a user-level or kernel-level library. The Win32 line libraries is a kernel-level line libraries for the Windows operating system. The Java Threading API allows threads to be created and managed directly in a Java program. However, because most JVM instances run on top of the host operating system, the Java threading API is typically implemented using line libraries on the host system. This means that on Windows systems, Java threads are typically implemented with the Win32 API, whereas in UNIX and Linux systems pthread

CPU scheduling of the operating system

First, the basic concept of scheduling

The processor scheduler is responsible for dynamically assigning processors to process or kernel-level threads. Processor scheduling is also known as CPU scheduling or process scheduling, which is also called thread scheduling in a wired thread's operating system. To maximize CPU utilization, the goal of the multi-channel program design is to maintain that there is always a process available for execution. In a single processor system, only one process can be run at a time, and any other process must wait until the CPU is idle before it can be re-dispatched.

The idea of multi-channel program design is very simple. A process continues to run until it must wait for certain operations (I/O requests are typical) to complete. In a simple computer system, the CPU is idle when the process waits, which wastes all the waiting time. Using multi-channel programming, we can effectively use this time. Keep multiple processes in memory at the same time. When a process must wait, the operating system withdraws the CPU from the process and allocates the CPU to another process. Then continue running in this way.

We can divide the processor scheduling into different types from different angles such as the object, time, function and so on. Processor scheduling involves not only choosing which ready process to go into the running state, but also when to start the execution of a process. According to the different levels involved in scheduling, we can divide the processor scheduling into three levels of advanced scheduling, intermediate scheduling and low level scheduling.

Advanced scheduling is also known as job scheduling or macro scheduling. From the perspective of a user's workflow, a job submits several processes, each of which is scheduled to execute according to the process. Intermediate scheduling involves the exchange of processes between internal and external storage. From the point of view of memory resource management, swapping part or all of the process out to external memory can provide the required memory space for the execution of the current running process, and swap the required portions of the current process into memory. Instructions and data must be in memory to be accessed directly by the processor. Low-level scheduling is also called Micro-Dispatch. From the point of view of processor resource allocation, the processor needs to often select a ready process or thread to go into the running state.

Second, scheduling time, switching and process

There are four scenarios in which CPU scheduling can occur:

When a process transitions from a running state to a wait state;

When a process transitions from a running state to a ready state;

When a process transitions from a wait state to a ready state;

When a process terminates the runtime

From an operating system perspective, a process or its PCB is always in a variety of waiting queues, or migrating between queues.

Where the ready queue is a special queue, it contains processes that already have almost all of the resources, and only the CPU is missing. The task of CPU scheduling is to select a waiting process from the ready queue and assign the CPU to it;

The best use of the CPU, the user process gets the best service. See, CPU scheduling migrates the selected process from the Ready queue node to the CPU node. The operating system is equipped with a special scheduler, listed in the schedule () function, only the scheduler is called to execute, only one CPU scheduling occurs. The so-called scheduling timing, is when to call this program.

Third, the basic rules of Dispatch

Common metrics include CPU utilization, throughput, response time, turnaround time, wait time, and more

Four, scheduling process scheduling can be used in the following two ways:

(1) Non-preemptive mode

When using this scheduling method, once the processor is assigned to a process, the process is executed until the process is completed or an event is blocked, the processor is allocated to other processes, and a process is not allowed to preempt the already allocated processor.

(2) preemption mode

This scheduling method allows the process scheduler to stop an executing process according to a principle and reassign the processor that has been assigned to the process to another process.

The principles of preemption are:

Time slice principle. Each process runs on a time slice, and when a time slice is exhausted, the execution of the process is stopped and the schedule is restarted. This principle applies to time-sharing systems.

Priority principle. Usually higher priority is given to a number of important and urgent processes. When such a process enters the ready queue, such as from a blocked state to a ready state, or from a still-ready state to an active-ready state, or when a newly created process into the ready state enters the ready queue, if its priority is higher than the process being executed, it stops the process being executed and assigns the processor to a high priority process. Make it perform.

The typical scheduling algorithms for typical scheduling algorithms are:

First come first service scheduling algorithm,

Short-Task (short, short, long-term) priority scheduling algorithm,

Time slice rotation scheduling algorithm,

Priority scheduling algorithm, high-response ratio priority scheduling algorithm,

Multilevel feedback queue scheduling algorithm, and so on.

First come first Come the scheduling algorithm is to select the process according to the sequence of the process into the ready queue, first the process of entering the ready queue is preferred. First come first service scheduling algorithm is the simplest scheduling algorithm, but it will let the short process wait for a very long process. FCFS can produce so-called belady anomalies.

The shortest job/process first (shortest job/process FIRST,SJF/SPF) scheduling algorithm is based on the CPU time required by the process, and always selects the process with the shortest estimated calculation time to run. The shortest process priority scheduling algorithm is the best local method, which satisfies the requirement of the shortest average waiting time. However, it is difficult to implement SJF scheduling because it is difficult to predict the length of the next CPU demand interval for the process. SJF algorithm is a special case of priority scheduling algorithm, priority scheduling and SJF scheduling will produce hunger, aging (Aging) technology can solve the problem of hunger.

The time slice rotation (Round robin,rr) scheduling algorithm is the scheduler each time the CPU is assigned to the ready queue first process using a time slice, for example 100ms, each process in the ready queue rotates to run one such time slice. Time-slice rotation scheduling algorithm is more suitable for timeshare (interactive) system. The main problem of the RR algorithm is to select the time slice, if the time slice is too large, then the RR scheduling becomes the FCFS dispatch, if the time slice is too small, the scheduling overhead caused by context switching is too large.

The priority scheduling algorithm selects the process based on the number of priorities that are determined, and always chooses a high-priority process. The method of prioritizing user processes is varied, and the priority setting of a process can be static or dynamic.

The high response-to-priority (highest Response Ratio next,hrrn) Scheduling algorithm calculates the response of a ready queue process, when scheduling always chooses the response than the highest ready process to get the CPU. Response ratio = (process elapsed time + process requirement run time)/process requirement run time. This scheduling algorithm is advantageous to the short process first, but also takes into account waits the long time the process, this algorithm is the FCFS algorithm and the SJF algorithm compromise.

Multi-Queue scheduling algorithm (multilevel QUEUE,MQ) is based on the nature and type of the process, the ready queue is divided into several sub-queues, all processes according to different circumstances into the corresponding queue, and different ready queue with different scheduling algorithm. The most common is the combination of the foreground interaction queue (using RR scheduling) and the background batch queue (using FCFS scheduling).

The multi-level feedback queue (multilevel Feedback queueing SCHEDULING,MFQ) scheduling algorithm is based on the multistage queue algorithm, allowing the ready process to migrate between queues.

The FCFS algorithm is preemptive, the RR algorithm is preemption, the SJF algorithm and the priority algorithm can be either preemption or non-preemptive.

If the operating system supports threads at the kernel level, then the thread must be dispatched instead of the dispatch process.

Blog Park content Update is slow, if you need to know the crawler (Image recognition, slider verification), machine learning, automation development-related content please add the public number: PythonCoder1024, thank you for your support.

Threads and the other side of the process

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: 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.