Simple explanation about process and thread
Process and thread are two basic concepts of operating system, but they are a bit abstract which cannot be mastered easily.
There is an analogy which explains these concepts very well.
1. The kernel of the computer is CPU, it handles all the computing tasks, it's like a factory and will run all the time.
2. Assume there is power limitation for the factory, it can only supply to one unit once,i.e, when one unit is working, other units must stop and wait. The meaning behind this is that each CPU can handle only one task at once.
3. Process likes the unit in the factory, it represents the single task the CPU can process. At any time CPU will always handle one task and other tasks will be in inactive status.
4. In one unit, there can be many workers, they collaborate to complete one task.
5. Thread like the worker in the unit, one process can have many threads.
6. The space in the unit is shared by all the workers in that unit, every worker can access any rooms in that unit. It means that each process memory space is shared by all the threads in that process.
7. However, each room has different size, some rooms can only allow one worker such as toilet. When there is some people in the toilet, others cannot get in. This means when one thread uses the shared memory, other threads must wait for it to complete to continue to use this section of memory.
8. One simple way to prohibit other coming in is having one lock on the door. The first person comes to the room will lock it, when others see the door is locked, they will queue outside and get in when the lock is unlocked. This is called mutual exclusion which is to prohibit many threads to read/write the same memory area at the same time.
9. There are also some rooms which can have n people inside such as kitchen. If there are more than n people, the extra people can only wait outside. This is similar to the memory area where only a specified number of threads can access at the same time.
10. The solution now is have n keys on the door, each person gets in will get one key, when they come out, they put the key back. Then if the people come after find no keys left, they will know that they must wait. This is called Semaphore, it guarantees multiple threads will not conflict with each other.
11. The design of operating system can be summarized as below three points:
- With multi-process paradigm, allowing many tasks to execute
- With muti-thread paradigm, allowing single task to be executed with different parts
- Provide collaboration mechanism, on one hand to prohibit processes and threads from conflicting with each other, on the other hand allowing different processes and threads to share resources.
Author 阮一峰: Source : http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html
Previously there is one picture which shows how git merge works, here is another one.