Concurrency and competition (1) concept

What is concurrency and parallelism

Three concepts:

  • parallel
  • concurrency
  • concurrency plus parallelism

concurrency

The following picture describes the concept of "concurrency" of a single-core CPU:
insert image description here

  • Task 1 and task 2 are running on this single-core CPU, and we feel that task 1 and task 2 are being carried out at the same time, and a single-core CPU can only execute one task in a time slice round. Since the switching time of the CPU is very fast, it will feel as if task 1 and task 2 are being executed at the same time.

parallel

insert image description here

  • This chip is a dual-core CPU, one CPU runs one task, and at the same time two tasks can be run on this chip at the same time, which is parallelism.
  • Parallelism can be regarded as the ideal state of doing things concurrently.

parallel plus concurrency

insert image description here

  • There are two CPUs on this chip, and each CPU often does not only perform one task, but generally it performs multiple tasks;

What is concurrency and competition

Concurrency will cause multiple programs to access a shared resource at the same time. At this time, the problem caused by concurrent access to a shared resource at the same time is competition.

Linux is a multitasking operating system, and concurrency and competition are very common in Linux. Therefore, concurrency and competition must be considered in the process of writing Linux drivers. Otherwise, problems are likely to occur when accessing shared resources, and these problems are often not easy to troubleshoot and locate.

What happens if you don't handle concurrency?

What is discussed here is the concurrency in the kernel space, and the concurrency in the user space is not discussed. Now there are two identical drivers A and B. These two drivers are executed concurrently, and they both modify the variable c.

  • Situation 1: Program A runs first, and then program B runs after program A is finished. This is an ideal situation. Program A and Program B run perfectly with no errors.
  • Situation 2: Program A runs halfway, and the kernel schedules program B to execute. After program B finishes executing, it returns to execute program A. But after program A is executed, is program B equivalent to doing nothing? The value of variable c is also the value of program A.

Situation 1 is an ideal situation, but we can't predict how program A and program B will run. If the shared resources are not protected, the program will run once in vain, or the system will crash.

Under what circumstances does Linux cause concurrency?

There are mainly the following situations:

  1. Interrupt program concurrent access . Interruption can be generated at any time. Once an interruption occurs, the work at hand will be put down to execute the interrupted task. If the shared resource is modified during the execution of the interrupted task, the problem just mentioned will occur.
  2. Preemptive concurrent access . After Linux kernel 2.6, the Linux kernel supports preemption. When preemption is supported, the executing process may be preempted at any time.
  3. Multiprocessor (SMP) concurrent access . Inter-core concurrent access exists between multi-core processors.

What to protect against concurrency?

It is not safe for processes (a running program is a process) to access shared resources concurrently. If two processes access space resources at the same time, there will be competition. So we have to protect shared resources.

From a code point of view, a shared resource is an integer global variable, or a device structure in a driver. etc.

Handling Concurrency and Competition

When writing a driver, we should try to avoid concurrency and competition in the driver. The Linux kernel provides several concurrency and competition methods, namely: atomic operations, spin locks, semaphores, and mutexes.

Guess you like

Origin blog.csdn.net/qq_28877125/article/details/128209995