The most vivid explanation of processes and threads is the explanation of process threads.

Source: Internet
Author: User
Tags semaphore

The most vivid explanation of processes and threads is the explanation of process threads.

Process and thread are the basic concepts of the operating system, but they are abstract and difficult to grasp. I found a good analogy that can be clearly explained and understood.

The core of a computer is the CPU, which undertakes all the computing tasks. It is like a factory that is always running.

If the power supply of the plant is limited, it can only be used in one workshop at a time. That is to say, when a workshop starts, all other workshops must be shut down. The meaning is that a single CPU can only run one task at a time.

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

There can be many workers in a workshop. They work together to complete a task.

A thread is like a worker in a workshop. A process can contain multiple threads.

The workshop space is shared by workers. For example, many rooms are accessible to every worker. This indicates that the memory space of a process is shared and can be used by every thread.

However, the size of each room is different. Some rooms can accommodate only one person at most, such as a toilet. When there is someone in it, other people cannot go in. This means that when a thread uses some shared memory, other threads must wait until it ends to use this memory.

A simple way to prevent others from entering is to lock the door. The first person locks the door, and then the person who sees the lock, queues at the door, wait until the lock is opened and then go in. This is called Mutex, which prevents multiple threads from simultaneously reading and writing a memory area.

There are also some rooms that can accommodate n people at the same time, such as the kitchen. That is to say, if the number of people is greater than n, more people can only wait outside. This is like some memory areas, which can only be used by a fixed number of threads.

The solution at this time is to mount n keys at the door. The person who goes in gets a key and then hangs the key back to the original place. The next person found that the key had been overhead and he knew that he had to wait in queue at the door. This method is called Semaphore to ensure that multiple threads do not conflict with each other. Mutex is a special case of semaphore (n = 1 ). That is to say, the latter can be used to replace the former. However, because mutex is relatively simple and efficient, this design is still used when resources must be exclusive.

The operating system design can be attributed to three points:

(1) Allow multiple tasks to run simultaneously in the form of multi-process;

(2) In the form of multithreading, a single task can be divided into different parts for running;

(3) provide a coordination mechanism to prevent conflicts between processes and threads, and allow resources to be shared between processes and threads.

The most vivid understanding of the processes and threads frequently asked during the interview

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.