Operating system principles (ii), processes, threads

Source: Internet
Author: User
Tags message queue

Technorati Tags: operating system, principle

Modern operating systems, such as linux,windows, are supported by "multitasking" operating systems. Multi-tasking means that the operating system can run multiple tasks at the same time . That is, on the same computer, you can surf the Internet, listen to songs, use Word, in the past, the single core of the CPU has been able to support multi-tasking, the way to achieve is the operating system for each task alternately alternating execution. , such as task 1 execution 0.01 seconds, switch to Task 2, Task 2 executes 0.01 seconds, then switch to Task 3, execute 0.01 seconds, because CPU execution speed is very block, we feel that all tasks are concurrent processing.

In the multi-core CPU era, because the number of tasks is still much more than the core number of CPUs, so the operating system will automatically turn multi-tasking to each core execution.

What is a "process", a task for the operating system is a process, such as the opening of the Thunderbolt, and the process may not only do one thing at the same time, such as thunder can download multiple links at the same time, the browser can open multiple pages and so on. These "subtasks" within a process are referred to as threads (thread).

Because each process has at least one thing to do, a process has at least a single thread, and multithreading is performed the same way as multiple processes, and is quickly switched between multiple threads by the operating system.

So now there are two solutions if you want to perform multiple tasks.

    • Start multiple processes with only one thread per process so that multiple processes can perform multiple tasks in one piece
    • Start a process, and then each process starts multiple threads, and multiple threads can perform the task in one piece.
      Of course, it can be multiple processes, and then open multiple threads, so the model is too complex and rarely used.

To summarize, there are three ways of multitasking

    • Multi-process mode;
    • multithreaded mode;
    • Multi-process + multithreaded mode.

A metaphor for quoting:

1 Process Single-threaded: A person eats vegetables on a table.
2. Single process Multithreading: Many people eat food together on the same table. So many people at the same time eat a dish when it is easy to scramble, that is, the sharing of resources will be conflict scramble
3. Multi-process Single thread: Many people eat food on their own table.

For Windows, adding a table costs a lot of money, so Windows encourages people to eat on a table, so you need to face the problem of thread resource scrambling and syncing.
For Linux, the cost of opening a new table is very small, so you can open as many new tables as possible, but it is inconvenient to talk on different tables, so you need to study the communication between the processes.

So when is it appropriate to use multitasking? time to perform time-consuming operations . For example, to launch a network request, because of the speed of the reason, the server will not immediately respond to us, if not to open multi-tasking, the director will be blocked, thereby affecting the experience.

The CPU of the server now generally has more than one core.

    • When each CPU core runs a process, there is no need to consider the context when switching between CPU cores because each process's resources are independent.
    • When each CPU core runs a thread, because each thread needs to share resources, the resources must be copied from one core of the CPU to the other to continue the operation, which consumes additional overhead.
      In other words, in the case of multi-core CPU, multithreading may not be able to catch up with many processes in performance

Summarize:

The following is a detailed description of processes and threads

1.1 Process

No operating system, the program can only be executed sequentially, when the introduction of the multi-tasking operating system, if there is no dependency between the program, it can be executed concurrently .

We can refer to the execution of a program as a process , since the process is dynamic, so it has a complete life cycle from creation to end, and the CPU and other resources are very limited, not all processes can immediately consume CPU resources, So we need a data structure to record and describe what stage the process is in so that the operating system can be dispatched globally.

In order for the process to execute concurrently, a process control block is introduced for the process, the Process control block (processing control blocks). This data structure records information about the process such as the address of the instruction to be run, the state of the process, the CPU scratchpad, and so on. In other words, the operating system is based on the PCB information to control and manage the concurrency process. If there is no PCB, the system cannot perceive the process, so the PCB is the only sign that the process exists.

So what does the PCB mainly contain?

    • Process identity: Assigns a data ID to a process and is unique, equivalent to having a unique address that the operating system can find.
    • Process state information: that is, what stage the process is in, and how it is prioritized.
    • Process Control information: The address of the process counterpart, the message queue pointer, and so on.
    • Storing staging information in the CPU registers

The entire computer system has multiple PCBs, in order to facilitate the search, will have the same status of the PCB through the form of a linked list of a queue. And because the PCB is often used, so the resident memory

Status of the 1.1.1 process

The process has three basic states

    • Ready: Other than getting the CPU, the others are ready.
    • Activity: Occupy the state of the CPU
    • Blocking: For example, request Io, which has not yet obtained an ACK signal returned by the disk, will block the wait.

    • The ready state of the process is ready, only the allocation of CPU resources, put into the ready queue , once the CPU, the execution state, when the time slice is exhausted then back to the ready state
    • The running state of the program, if you want to perform an IO request, you will block themselves, insert the blocking queue . After the IO is completed, it returns to the ready state, inserting the ready queue and waiting for the schedule to be dispatched again.
1.1.2 Multi-process

The Unix/linux operating system provides a fork () system call, which is very special. A normal function call, called once, is returned once, but the fork () is called once and returned two times because the operating system automatically copies the current process (called the parent process), which is then returned within the parent and child processes, respectively.

The child process always returns 0, and the parent process returns the ID of the child process. The reason for this is that a parent process can fork out many child processes, so the parent process has to note the ID of each child process, and the child process can easily get the ID of the parent process.

With the fork call, a process can copy a child process to handle the new task when it receives a new task, the common Apache Server is the parent process listening port, whenever there is a new http when requested, the child process is forked out to process the new http request.

1.1.3 Process Communication

Process communication refers to the interaction information or data between processes. There is definitely a need for communication between processes, and the operating system provides a number of mechanisms to enable interprocess communication, such as

    • Through message queue: Message Queuing is a linked list of content for messages
    • Pipe: Pipe is a half-duplex communication mode, the data can only flow in one Direction
    • By sharing memory: that is, all processes can access data in a common memory.

1.2 Threads

A process is a running program that can implement concurrency between multiple processes, but if the process is a resource holder, switching between each process means that the resource is being acquired and released, which consumes too much performance. So the introduction of a "lighter process" is also a thread.

That is, the process is only the bearer of the resource, and then several threads are processed in parallel, and the threads in the same process share the resources of the process, and only the thread from one process to the other will cause the process to be dispatched.

In short, the process is the owner of the resource, and the thread becomes the basic unit of the Independent dispatch , the thread itself does not own the resources, only the resources of the process in which it is shared.

The process is concerned about the possession of memory, each process can access the memory is a lap, and the thread as part of the process, the role is to use the CPU to run the code, so focus on the operation of the central processing unit, rather than the management of memory and other resources.

How 1.2.1 threads are implemented

Threads are implemented in two ways

    • Kernel Support threads: thread creation, revocation, and switchover are all dependent on kernel space. The advantage is that SMP (symmetric processors) can dispatch multiple threads in the same process to execute in parallel. The disadvantage is that thread switching is expensive because thread scheduling and management are in the kernel (which can be considered a high-privileged state), but in practice most threads run in the user state (which can be considered a low-privilege state), so the switching thread needs a system call to enter the kernel state, and the overhead is naturally large.
    • User-level threads: refers to thread creation, synchronization, communication, etc. do not need to use the system call to implement, all in the user state completed. The benefits are part of the user program, regardless of the operating system platform. For example, most databases are like this. In this case, however, it is not possible to take advantage of multiple processors, where a process can only be assigned to one CPU, and only one thread in the process is executable.

The implementation of a user-level thread requires a thread to be controlled through an intermediary system such as the runtime system and the kernel.

The so-called runtime system refers to a collection of functions that manage and control threads,

The second approach is to use lightweight processes as intermediate systems, which can be used by system calls to obtain kernel services, a number of lightweight processes that form a thread pool, and when user-level threads run, as long as a lightweight process connection is taken from the pool, the same services as the kernel process can be obtained.

1.2.2 Multi-Threading

Multitasking can be done by multiple processes or by multithreading within a process. Since any process starts a thread by default, we refer to that thread as the primary thread, and the main course can start new threads

The biggest difference between multithreading and multi-process is that in many processes, the same data , each of which has a copy in each process, does not affect each other, and in many threads, all variables are shared by all threads, so any one of the data can be modified by any thread, Therefore, the greatest danger of sharing data between threads is that multiple threads change a variable at the same time, and the content is scrambled. so you can still add a lock to the critical section code, so other threads cannot execute the code at the same time, only wait until the lock is released and the lock can be changed. Since there is only one lock, no matter how many threads, at most one thread at a time holds the lock, so there is no conflict of modification. The thread that gets the lock must release the lock after it is exhausted, or the thread that waits for the lock will wait forever to become a dead thread

The benefit of locking is to ensure that a piece of critical code can only be executed by a single thread from start to finish, and the downside is

    • Prevents multithreading from executing concurrently, and a piece of code that contains a lock can actually be executed in single-threaded mode, and the efficiency is greatly reduced.
    • Because multiple locks can exist, different threads hold different locks and try to acquire locks held by each other, which can cause deadlocks , cause multiple threads to hang all, neither execute nor end, only by the operating system force termination.
1.3 Process vs Thread

Multi-process and multithreading are the two most important ways to multitask. To achieve multitasking, it is common to use the Master-worker mode, where Master is responsible for assigning tasks, and the worker is responsible for performing tasks, so in a multitasking environment, it is usually a master and multiple workers.
Multi-process mode:

    • Pros: high stability, because one child process crashes without affecting the master and other child processes. (Of course, the main process hangs up all the processes, but the master process is only responsible for assigning tasks, the probability of hanging out is low). Apache is the first to use multi-process mode.
    • Cons: The cost of creating a process is large and the process creation overhead of the unix/linux system is small, but creating a process under Windows is costly. Also, the number of processes that the operating system can run concurrently is limited, and if thousands of processes are running at the same time under memory and CPU constraints, the operating system even scheduling will be problematic.

For multithreaded mode, multithreaded mode is usually faster than a multi-process, but it's not going anywhere fast. Moreover, the biggest drawback of multithreaded mode is that any thread that hangs can directly cause the entire process to crash because all threads share the memory of the process. Under Windows, multithreading is more efficient than multiple processes, so Microsoft's IIS server uses multithreaded mode by default. Because of the stability of multithreading, IIS is less stable than Apache.

In fact, whether it is multi-threaded or multi-process, once the number up, the efficiency is not too high . If do homework, first spent 1 hours to do Chinese homework, finished, and then spend 1 hours to do math homework, so, in turn, all done, a total of 5 hours, this method is called a single task model, or batch processing task model.

Then to the multi-tasking model, do 1-minute language, then switch to math, do 1 minutes, then switch to English, and so on, as long as the switch speed is fast enough, this way and the single-core CPU to perform multitasking is the same, it seems to be writing 5 homework.

But notice that switching is a price to pay. from the language cut to the maths, must first tidy up the desk The Chinese book ( saves the scene ), then, opens the mathematics textbook (this is called prepares the new environment ).
The operating system in the same way, to save the current execution of the field environment (CPU register status, memory pages, etc.), and then to prepare the execution environment of the new task (restore the last register state, switch memory pages, etc.) to start execution. If there are thousands of tasks at the same time, the operating system may be mainly busy switching tasks, and then the hard disk, the Point window unresponsive, the system is in suspended animation state.

1.4 Asynchronous IO

Tasks can be divided into compute-intensive and IO-intensive.

    • Compute-intensive: Requires a lot of computation to complete, such as high-definition video decoding, at this time the CPU utilization should be higher the better, should not let the CPU wasted on task switching, so the number of simultaneous computation-intensive tasks should be equal to the CPU Number of cores.
    • IO-intensive: The tasks involved in network and disk IO are IO-intensive tasks, characterized by low CPU consumption, most of the tasks are waiting for IO operations to complete, most of the common tasks are IO-intensive tasks, such as Web applications. For IO-intensive tasks, the most appropriate language is the most efficient (least code) language, the scripting language is preferred, and the C language is the worst.

Because CPU and IO are vastly different speeds, it is best to introduce a multi-process model or multithreaded model to support multitasking concurrency if a task is waiting for IO operations most of the time during execution.
For multiple users, each user is assigned a thread , and if the IO is encountered causing the thread to be suspended, other users ' threads are unaffected.

This solves the concurrency problem, but the number of threads is too many, the CPU time is spent on the thread switch, so the introduction of asynchronous Io, if the full use of asynchronous Io, you can use a single-process single-threaded model to perform multi-tasking , this new model is called the event-driven model, Nginx is a Web server that supports asynchronous IO. On multicore CPUs, you can run multiple processes (the same number as the number of CPU cores) and take advantage of multicore CPUs so that the total number of processes is not large and operating system scheduling is very efficient.

So what is asynchronous IO?

The so-called asynchronous io refers to the need to perform a time-consuming IO operation, it only sends the IO instruction, does not wait for the IO result, then goes into the next round of message processing. After a period of time, when the IO returns the result, the CPU is notified to get the IO operation result directly.

The asynchronous IO model requires a message loop in which the main thread repeats the process of "reading messages-processing messages", which means that the main thread of the asynchronous IO model does not rest at the time of the "IO Request" and the "IO completion", but continues to process other messages in the message loop. Instead of like the synchronous IO model, the main thread can only hang . This way, one thread can process multiple IO requests at the same time , and there is no action to switch threads.

Operating system principles (ii), processes, 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.