Operating System Learning Notes (i) Process and threading model

Source: Internet
Author: User

A process can be said to be the most central abstraction of the operating system, and threads can be thought of as a lightweight process, or as many mini-processes within a process.

I. Model of the process

process: A process is an instance of a running program. For a single-core processor, only one program can run at a time, but at each time it may run multiple processes, creating the illusion of parallelism.

Conceptually, each process has its own virtual CPU, as if it had exclusive use of the CPU, although the actual CPU is constantly switching. Another major feature of the process is the independent virtual address space .

Creation of processes

There are four types of events that cause process creation:
(1) Initialization of the system. System initialization creates many processes, such as when Windows is powered on.

(2) A system call is executed that is called by the running process. A fork () call is run as a program.

(3) User requests to create a process. Enter./a.out as the command line.

(4) Initialization of a batch job.

Termination of the process

A process is an instance of a running program, and a program runs out, so there is a time for the process to terminate:

(1) Normal withdrawal (voluntary). If the program ends normally.

(2) Error exit (voluntary). If you write a program, call exit (NUM) when the error occurs.

(3) Serious error (involuntary).

(4) Killed by other processes (involuntary). A Kill (PID) is called by another process.

Hierarchical structure of processes

Different operating systems have different concepts. The Windows system does not differentiate between parent and child processes in a Linux system.

Status of the process

There are three main states of the process:

(1) Running state: a process that is running on the CPU.

(2) Ready state: Ready but not yet checked by the scheduler.

(3) Blocking state: Wait for an external event for some reason (such as waiting for I/O completion) to temporarily fail to execute.

Here's a picture of a possible conversion relationship . It should be noted that the blocking process must first enter the ready state and wait for the processor to dispatch.

Implementation of the process

The implementation of the process is relatively deterministic relative to the thread. The operating system kernel maintains a process table, also known as a Process Control block (PCB). The Process table entry is the necessary information for a process to start, including process management (register, PC,PSW, dispatch information, open file status, etc.), storage management (code snippet, data segment, stack segment pointer, etc.), file management (directory, PID, etc.) information.

When the process goes from run state to other states, the PCB holds all the information to start it, and when the process is again selected by the scheduler, it is necessary to recover the information. Save the steps, including: Hardware press into the PC, put the interrupt vector into the new PC, and then save the Register through assembly language and set up a new stack, run the Interrupt service program (usually C), and then select a process through the scheduler, the operation of its information loaded registers and the PC.

Second, the threading model

Threads are used because of the high cost of creating, revoking, and switching processes, and the need to share memory space and data, as well as to increase the speed of operation. One example is a Web server, which, if it takes a process, a page request is responded to, and the process goes into a blocking state, which makes it impossible to provide other services. Instead of threading, you can use a thread to accept the request, then assign the request to the worker thread, implement the simultaneous response, and share the memory.

The threading model is based on two concepts: Resource grouping processing and execution. The purpose of a thread is to share resources and to accomplish a task together.

Threads are very similar to processes, but lighter than processes because the process has a separate virtual address space, while threads in the same process share memory space and resources. Therefore, the thread only needs to save the PC, register, stack , etc., without saving a lot of files, process management information. Threads are also unprotected and equal, because they share the same resources, including open files, sub-processes, and so on.

POSIX (Portable operating System interface of Unix) prescribes a generic threading package pthread for UNIX systems.

Implementation of Threads

Threads are implemented in general in two ways: implementing threads in User space and implementing threads in the kernel.

Implementing Threads in User space

In this case, the kernel does not know that the thread exists, and the dispatch unit is the process. Therefore, in order to reflect the role of multithreading, we must consider the problem of blocking.

Because the user manages threads, each process will have a thread table, similar to the process table, but with less content.

There are many advantages to user-level threading:

(1) can be run on a system that does not support threading.

(2) very fast because it does not need to fall into the kernel, does not require a context switch, and does not require a cache refresh.

(3) Each process can be allowed to customize its own thread scheduling algorithm.

Of course, because the kernel does not know the existence of a thread, it needs to solve the problem of blocking, either by non-blocking system calls , or by checking if the call is blocked (such as select, wrapper).

In addition, because the kernel does not know the existence of a thread, there is no clock interruption inside the process, so the thread must voluntarily yield the CPU, otherwise the other threads cannot preempt it.

Implementing threads in the kernel

The kernel implements a thread, which maintains a thread table in the kernel, and the process is not maintained. Compared with the implementation in user space, the advantages and disadvantages are basically the opposite.

Implementations in the kernel naturally do not require non-blocking I/O, but the cost is that the operation of the thread is expensive.

Operating System Learning Notes (i) Process and threading model

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.