Simple explanation (description) of processes and threads

Simple explanation (description) of processes and threads

Process and thread are the basic concepts of operating systems, but they are relatively abstract and not easy to grasp.

Popular description of processes and threads

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

2. Assuming that the power of the factory is limited, it can only be used by one workshop at a time. In other words, when one workshop starts, all other workshops must stop. The implication is that a single CPU can only run one task at a time.

3. The process is like the workshop of a factory, it represents a single task that the CPU can handle. At any time, the CPU always runs a process, and other processes are in a non-running state.

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

5. Threads are like workers in the workshop. A process can include multiple threads.

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

7. However, the size of each room is different, and some rooms can only accommodate one person at most, such as a toilet. When there are people inside, no one else can enter. This means that when a thread uses some shared memory, other threads must wait for it to end before using this memory.

8. A simple way to prevent others from entering is to add a lock at the door. Those who arrived first locked the door, and those who arrived later saw the lock and lined up at the door, waiting for the lock to open before entering. This is called "mutual exclusion" (Mutual exclusion, abbreviated as Mutex), which prevents multiple threads from reading and writing a certain memory area at the same time.

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

10. The solution at this time is to hang n keys at the door. Those who enter take a key and hang the key back when they come out. Those who arrived later found that the key was empty, and knew that they had to wait in line at the door. This approach is called "semaphore" (Semaphore), used to ensure that multiple threads will not conflict with each other.

It is not difficult to see that mutex is a special case of semaphore (when n=1). In other words, the latter can be used instead of the former. However, because mutex is relatively simple and efficient, this design is still used when resource monopoly must be guaranteed.

to sum up

1. In the form of multiple processes, multiple tasks are allowed to run simultaneously.
2. In the form of multi-threading, allowing a single task to be divided into different parts to run.
3. Provide a coordination mechanism to prevent conflicts between processes and threads on the one hand, and allow resources to be shared between processes and threads on the other hand.

Guess you like

Origin blog.csdn.net/qq_37192571/article/details/109050421