Chapter 1: Processes and Threads

Source: Internet
Author: User

1.1 Process

The most central concept in the operating system is the process: this is an abstraction of an ongoing program. Everything else in the operating system is built around the concept of a process.

A process is one of the oldest and most important abstractions that the operating system provides. Without the abstraction of a process, modern computing will cease to exist.

1.1.1 Process Model

A process is an instance of an executing program, including the current values of program counters, registers, and variables. Conceptually, each process has its own virtual CPU.

Because the CPU quickly switches back and forth between processes, the speed at which each process performs its operations is indeterminate. And when the same process runs again, its operation speed is not reproducible. Therefore, you should never make any definite assumptions about the timing when programming the process.

Key idea: A process is a type of activity that has programs, inputs, outputs, and states. A single processor can be shared by several processes, using some scheduling algorithm to decide when to stop the work of a process and to serve another process instead.

1.1.2 Creating a process

The operating system requires a way to create a process. There are 4 types of leading events:

1, System initialization

2, a process called by a running process is executed to create a system call

3, user requests to create a new process

4, initialization of a batch job

Technically, in all of these cases, the new process was created because an existing process performed a system call to create the process. In Unix systems, only one system call can be used to create a new process: fork.

Termination of the 1.1.3 process

There is no eternity. Termination is usually caused by the following conditions:

1, exit normally. Voluntary.

2, error exits. Voluntary.

3, critical error. Involuntary.

4, killed by other processes. Involuntary.

1.1.4 Hierarchy of processes

In Unix, a process and all of its children and descendants together form a process group.

In Windows, there is no concept of process hierarchy. All processes are of the same status.

Status of the 1.1.5 process

Although each process is a separate entity, processes often interact with each other. The output of one process may be entered as another process.

When a process is logically unable to continue running, it is blocked. A process that is conceptually capable of running is forced to stop because the operating system dispatches another process that consumes the CPU. These two situations are completely different. So there are generally three states of the process:

1, operating state. The moment the process actually consumes the CPU

2, ready state. Can run, but is temporarily stopped because other processes are running

3, blocking state. The process cannot run unless an external event occurs

There are four possible conversions for these three states. The process Scheduler is part of the operating system, and the process does not even feel the presence of the scheduler. The main task of the scheduler is to decide which process should run, when it should run, and how long it should run.

Implementation of the 1.1.6 process

To implement the process model, the operating system maintains a table (an array of structures), which is the process table. Each process consumes a process table entry.

All interrupts start with the Save register.

1.1.7 Multi-Channel program design model

The use of multi-channel program design can improve CPU utilization.

1.2 Threads

Use of 1.2.1 Threads

Just after the concept of multithreading, we added a new element: the ability of parallel entities to share the same address space and all available data. For some applications, this capability is required, and this is what the multi-process model cannot express because they do not have the same address space.

The second reason for multithreading: Because threads are more lightweight than processes, they are easier to create and easier to undo than processes. Creating a thread is 10-100 times faster than creating a process.

Third, multithreading can speed up application execution.

Four, in a multi-CPU system, real parallelism has the possibility of implementation.

1.2.2 Classic Threading Model

The process model is based on two separate concepts: the processing and execution of resource groupings. Processes are used to centralize resources together, while threads are entities that are scheduled to execute on the CPU. There is no protection between threads, impossible, nor necessary. The thread concept attempts to implement the ability to execute multiple threads that share a set of resources so that they can work together to complete a task.

Each thread has its own stack. Typically each thread invokes a different procedure, thus having a different execution history.

1.2.3 POSIX threads

1.2.4 Implementing threads in User space

The most obvious advantage is that user-level threading packages can be implemented on operating systems that do not support threading. When you manage threads in user space, each thread needs to have its own dedicated thread table.

There is no need for traps, no context switches, and no need to flush memory caches, which makes thread tuning very fast.

User-level threading allows each process to have its own custom height algorithm.

A problem exists:

1, how to implement blocking system calls?

2, if a thread starts running, other threads in that thread cannot run unless the first thread automatically discards the CPU.

1.2.5 implemented in the kernel

2.2.6 Hybrid Implementation

2.2.7 Scheduler activation mechanism

The goal of dispatcher activation is to emulate the functionality of kernel threads, but to provide better performance and greater flexibility for threading packages that are typically implemented in user space. A goal of the scheduling activation mechanism is to serve as the basis of reliance on upstream calls.

2.2.8 Pop-up threading

The arrival of a message causes the system to create a thread that processes the message, which is called a pop-up thread. The result is that the time between message arrival and processing starts is very short.

2.2.9 multithreading of single-threaded code

Chapter 1: Processes and Threads

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.