A simple explanation of processes and threads

Source: Internet
Author: User
Tags semaphore

Processes (process) and threads (thread) are the basic concepts of the operating system, but they are abstract and difficult to master.

Recently, I read a piece of material and found that there is a good analogy that can be explained in a clear and understandable way.

1.

The core of the computer is the CPU, which takes on all the computational tasks. It's like a factory that's always running.

2.

Assuming that the plant has limited power, it can only supply one workshop at a time. In other words, when a workshop is started, other workshops must be shut down. The implication behind this is that a single CPU can run only one task at a time.

3.

A process is like a factory workshop, which represents a single task that the CPU can handle. At any one time, the CPU always runs a process and the other processes are not running.

4.

In a workshop, there can be many workers. They work together to complete a task.

5.

A thread is like a worker in a shop. A process can include multiple threads.

6.

The workshop space is shared by the workers, for example, many rooms are accessible to every worker. This symbolizes that the memory space of a process is shared and that each thread can use the shared memory.

7.

However, the size of each room is different, some rooms can accommodate a maximum of one person, such as toilets. When there are people in it, the others can't go in. This means that when a thread uses some shared memory, other threads must wait for it to end before they can use this piece of memory.

8.

A simple way to prevent others from entering is to add a lock to the door. The first person to lock the door, after the people see locked, in the doorway line, wait for the lock to open and then go in. This is called a mutex (Mutual exclusion, abbreviated mutex), which prevents multiple threads from reading and writing to an area of memory at the same time.

9.

There are also rooms that can accommodate n people at the same time, such as kitchens. In other words, if the number of people is greater than N, the extra person can only wait outside. This is like some memory areas that can only be used by a fixed number of threads.

10.

The solution at this point is to hang N keys at the door. The person who goes in takes a key and hangs the key back when he comes out. After the people found that the key was overhead, they knew they had to wait in line at the door. This is called a "semaphore" (Semaphore), which guarantees that multiple threads do not conflict with each other.

It is not difficult to see that a mutex is a special case of Semaphore (N=1). That is, it is entirely possible to replace the former with the latter. However, because the mutex is simpler and more efficient, this design is used in cases where the resource monopoly must be guaranteed.

11.

The operating system is designed so that it boils down to three points:

(1) Multi-process mode, allowing multiple tasks to run simultaneously;

(2) in multi-threaded form, allowing individual tasks to be divided into different parts of the operation;

(3) Provide a coordination mechanism to prevent conflicts between processes and threads, and to allow the sharing of resources between processes and threads on the other.


A simple explanation of 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.