When you are asked these questions: what do you think is the difference between concurrent, parallel and serial? What are the differences and relationships between threads and threads? How would you answer that? Are you a little silly and confused or vaguely understanding? Next we will introduce in detail, there is a description of the wrong place can leave a message oh.

Concurrency, parallelism, serialization, threads, and processes are all operating system concepts that represent the way a CPU performs multiple tasks. First, let’s take a quick look at computer composition

The basic hardware components of a computer

First, take a look at the hardware components of a computer and the relationship between them

Multi-cpu and multi-core

The graph below shows the CPU usage of all my Macs

As you can see, my computer is single-CPU, with hyper-threading enabled, 6 cores and 12 threads

Multiple cpus: a simple number of cpus work on the same system. The communication between the cpus is carried out through the bus on the main board

Multi-core: A CPU has multiple core cpus that communicate with each other over an internal CPU bus

So let’s talk about the concepts that we mentioned at the beginning and the differences between them. Let’s talk about them one by one

Processes and threads

Process: the minimum unit of resources (CPU, memory, disk, I/O, and bandwidth) allocated by the operating system (OS). A process is a running instance of a program

When a program is started, the operating system creates a block of memory for the program to hold the code, the running data, and a main thread to perform the task. We call such a running environment a process

For example, opening a browser and a chat window are processes, respectively. A process can have multiple subtasks, such as chat receiving and sending messages, called threads.

Thread: The basic unit of CPU scheduling and allocation. The operating system schedules cpus based on process priority and thread priority.

Thread count: a logical concept, the number of simulated CPU cores.

Processes allocate resources to threads, and threads within a process share the resources of a process. The relationship between them can be seen below:

The relationship between processes and threads is described as follows:

  • A process can simply be thought of as a container with its own independent address space. A process can be composed of execution units of multiple threads. Each thread runs in the context of the same process, sharing the address space of the process as well as resources such as code and global data within it, that is, data in the process is shared between threads

  • Each process has at least one main thread, which is automatically created by the system rather than actively created by users. After the system has created a process, it actually starts the main thread that executes the process. The main thread provides the starting point of the program to the operating system in the form of function addresses, i.e. program entry functions (such as the main function). When the main thread of execution terminates or shuts down, the process terminates and the operating system reclaims the resources occupied by the process

  • The failure of any thread in the process will cause the entire process to crash.

  • The contents of the processes are isolated from each other. Process isolation is A technique to protect each process from interfering with each other in the operating system. Each process can access only the data it owns, preventing process A from writing data to process B. Because data between processes is strictly isolated, a crash or hang of one process does not affect other processes. If there is a need for data communication between processes, then a mechanism for interprocess communication (IPC) is needed.

  • Strictly speaking, the thread should be able to obtain CPU resources, and the process to obtain CPU resources is reflected in the thread. Number of CPU cores, not directly related to process threads. An operating system (OS) can deploy a process on a CPU core, but this depends on the system design.

  • Both processes and threads are scheduled by the operating system. Threads are created by the process, but the process itself is not responsible for scheduling threads. From an operating system’s point of view, threads and processes are essentially the same, except that threads are mini-processes and processes can contain multiple threads

  • For the memory heap, memory and code areas generally belong to a process, but the stack (execution stack) belongs to a thread, and each thread has a separate stack.

To sum up:

Processes and threads are descriptions of a period of time during which the CPU is working

  • Process is the sum of program execution time for context switch = CPU load context +CPU execution +CPU save context

  • Threads are smaller CHUNKS of CPU time that share the context of a process.

The detailed differences between the two can be found in the following figure:

The relationship between program and process and thread

A program: an ordered collection of instructions, a static entity, with no meaning of its own to run.

Process: is it a program in execution of a data set, the process is a single execution of the program, is a dynamic entity, has its own life cycle, due to create, run due to scheduling, for waiting for resources or events in a wait state, was dismissed because the task is complete, reflects a program to run on a certain data set all the dynamic process.

Regardless of how many cpus the system has, each process runs on a single CPU, or if there are multiple processes on a single CPU, multiple processes execute concurrently.

Thread: The smallest unit of program execution. Each program has at least one thread. If a program has only one thread, that is the program itself. A single-threaded process can simply be understood as a process with only one thread, one process doing one thing at a time. A multi-threaded process can be understood as a process doing several things at the same time, each thread can handle different transactions, and one thread blocking does not affect the other thread.

How do you perform the following three operations in a multithreaded versus a single thread

A = 1 + 2
B = 4 - 3
c= 5 * 6
Copy the code

As can be seen from the figure, threads are attached to processes, and multi-threaded parallel processing in processes can improve computing efficiency.

Multithreaded processes can make the most of system CPU resources, but more threads is not always better. The more threads, the less CPU time allocated to each thread.

Process and program relationships:

  • The two are not one-to-one correspondence, a program execution on different data sets will become different processes, process control blocks can be used to uniquely identify each process, this is the program can not do, because the program has no direct relationship with the data, even the program execution of different data.

  • Generally speaking, a process must have a corresponding program, and only one; A program may have no corresponding process because it is not executing, or it may have multiple processes because it is running on different data sets.

Different processes can execute the same program, such as reading data from the same file, but their reading functions have the same code and are run by multiple processes or threads

Through the operating system, computer components, threads, processes, etc., then we will proceed to the concurrent, parallel, serial explanation will be easier to understand, so what is the difference between them and the CPU and its core number?

Concurrent, parallel, serial

Let’s use a picture to describe the differences between the three, which will help us understand the differences between them more intuitivelyIn the single-core CPU CPU is unable to be used in parallel by multiple programs, multiple processes or multiple threads can achieve concurrent (serial on the micro, concurrent on the macro); Multi-core CPU threads can be microscopically parallel.

For example, in development we said that the number of concurrent resource requests reached 10,000. This means 10,000 requests are coming in at the same time. But it’s obviously impossible to really handle all 10,000 requests at once.

If the processor on this machine has four cores, not counting hyperthreading, then we expect four threads to be running at the same time. That is, the number of concurrent accesses is 10,000, while the actual number of underlying parallel processing requests is 4.

If the number of concurrence is smaller than 4, or if your machine has 10,000 cores, then concurrence here has the same effect as parallelism.

That is, concurrency can be virtual or real simultaneous. And parallelism means really simultaneous execution.

Conclusion:

1. Processes in a single CPU can only be concurrent, while processes in a multi-CPU computer can be parallel. 2, single CPU single-core thread can only be concurrent, single CPU multi-core thread can be parallel. 3, whether concurrent or parallel, the user view, see is multi-process, multi-thread.

Synchronous and asynchronous

Synchronous and asynchronous focus on message communication mechanisms

Synchronization: When a process executes a request, if it takes some time for the request to return information, the process will wait until it receives the return information before continuing to execute.

Asynchronous: Asynchronous refers to the process does not need to wait, but continue to perform the following operations, regardless of other process state, when the information return will inform the process for processing, so that you can improve the efficiency of execution, namely the asynchronous is we send a request, the request will be automatically in the background and get the data, and then to deal with data, in this process, We can go ahead and do something else, regardless of how it makes the request, regardless of what it does with the data.

Here’s an example:

You call the bookstore owner and ask if they have “Operating System”. If it’s a synchronous communication mechanism, the bookstore owner will say, “Wait a minute, LET me check”, and start checking and checking, and when you’re done checking (it could be 5 seconds, it could be a day), you’ll get the result. With asynchronous communication, the bookstore owner tells you let me check it out, calls you when it’s done, and hangs up. And when he does, he’ll call you. Here the boss calls back and forth by “calling back.

Blocking and non-blocking

Blocking and non-blocking are concerned with the state of the program while it waits for the result of the call (message, return value).

Blocking call: The current thread is suspended until the result of the call is returned. The calling thread does not return until it gets the result.

Non-blocking call: a call that does not block the current thread until the result is not immediately available.

Again, the above example:

Did you make a phone call to ask the bookstore owner “operating system”, this book if you are blocking call, you will always be “hung” himself, until the book have any results, if is a blocking call, you have no matter the boss told you that your side to play the first, of course you also want to occasionally check back in a few minutes the boss have to return the result.

Blocking and non-blocking have nothing to do with synchronous asynchrony. It doesn’t matter how your boss answers you.

Two ways to improve computer performance

  • Increase hardware level, processing speed or core count.

  • In addition, the number of threads can be set reasonably according to the actual scenario to improve CPU utilization in software.

Reference:

Computer composition principle

I want to understand CPU, core, process, thread, serial, concurrent, parallel