Multi-threaded, multi-process, multi-core

foreword

The concept of thread and process has actually been entangled for a long time, but fortunately, I have some understanding, but I still feel that it is not so transparent.
It happened that I had an argument with my roommate during dinner. It was very similar to the thread issue. In order to explain it to my roommate in an easy-to-understand manner, it really took a lot of brain cells. Fortunately, he finally said he understood (as to whether it is true or not, it is not clear, but at least I feel a lot of clarity).
In view of holding a heart of open source sharing, I will tidy it up a little and use it for a while!

CPU

In fact, the CPU is a very stupid guy, so stupid that it can only do the simplest things, and it can only do one of the simplest things at a time , and all the things it can do can be counted with fingers. A few. Too much, complicated, sorry, no!

There are only a few things the CPU can do, it's called CPU指令.

Although the CPU guy is stupid, it still has advantages, and this advantage is very popular. What are the advantages? Quick ! Exactly !

time slice

Although the CPU only has a few instructions, it is enough, because those complex things can be done by permuting and combining these instructions . So ah, the CPU is not afraid of trouble at all.

Since the CPU works fast and accurately, many things can be done by him. But the CPU is too small to be stupid. In order to prevent this stupid boy from suffocating his head and focusing on the task at hand, leaving other things behind, there is a guy with a bright mind running to command. CPU, this guy is the operating system (more precisely, the time-sharing operating system) , also called OS .

The OS stipulates that the CPU can only use a short period of time to do one thing (this short period of time is called a time slice ), and when the time is up, it must put down the thing at hand and do the next thing, no matter whether the thing at hand is finished or not. In fact, the OS records the progress of every event, and the next time it is the turn of the event, the CPU will continue to work on the original progress.

Since the CPU is working so fast, it feels like it's doing everything it's supposed to do at the same time.

Process & Thread

Whether it is a process or a thread, it is actually a task. Tasks are also divided into large and small tasks. A large task can contain several small tasks, and a large task must have at least one small task. A large task is a process , and a small task is a thread .

These tasks are handed over to the OS, and the OS will plan according to the situation, allocate a reasonable time slice to each task, and then arrange the CPU to complete it.

Single Thread & Multi Thread

A large task has only one small task, then this large task is called single- threaded , otherwise it is called multi-threaded .

Take a chestnut:

  • Xiao Ming is singing, and singing is doing nothing else. This is single thread .
  • Xiao Ming sang and danced. This is multithreading .
  • Xiao Ming sings and eats. This is also multi-threading , but these two small tasks use the mouth, so it will be a lot of trouble to execute. The solution to the trouble is thread synchronization .

multi-Progress

Lift chestnuts directly:

  • Xiao Ming flew a kite by himself. This is a single process .
  • Xiaoming and Xiaohong are flying their kites. This is a multi-process , except that the two processes are independent.
  • Xiaoming and Xiaohong zoom in on the centipede kite together. This is another multi-process , which requires cooperation between processes, and there are similar 多线程troubles. I don't have a detailed understanding of this issue, so I won't talk much about it.

Multithreading & Multiprocessing

Lift chestnuts again:

  • Xiao Ming sang and danced. This is multi-threading and uses Xiaoming's body (resources).
  • Xiaoming sings and Xiaohong dances. This is multi-process , each with its own body (resource).
  • Xiaoming and Xiaohong sing together. This is also multi-process , each with its own body (resource).

multicore

Single-core and multi-core are for CPU.

Continue to give chestnuts to explain:

  • ordinary people. single core .
  • Nezha. multicore .

If there is something wrong, please correct me!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324424789&siteId=291194637