Linux kernel design and implementation summary notes (Chapter IX) kernel synchronization,

In applications that use shared memory, one must pay special attention to the protection of shared resources, prevent concurrent access to shared resources.

A critical area and competitive conditions

1.1 Critical Area and competitive conditions

The so-called critical area is access and manipulate data shared code segment. Multiple execution threads concurrently access the same resources are usually insecure, in order to avoid concurrent access to a critical section, the programmer must ensure that the code is executed atomically.

If two threads of execution may be in the same time and perform a critical section, if this situation does occur, we say it is a race condition. Because the error is very difficult to reproduce due to competition, so this error will be very difficult to debug.

Avoid concurrent and prevent race condition known as synchronization.

 

Why protect?

Some transactions must take place completely, or simply does not occur, but must not be interrupted.

Second, lock

This mechanism is provided lock: It is like a door, the room behind the door can think of to give a critical section. Within a specified time, the room there is only one thread of execution, when a thread enters a room, it will lock the door behind him. When it ended operations on shared data, it will be out of the room, open the door. If another thread came in the door locked, then it must wait for thread concurrency in the room came out to open the door to enter the room.

Thread holds the lock, the lock to protect data.

2.1 causes of concurrent execution

The kernel may cause similar reasons concurrent execution, they are:

  • ---- almost interrupt interrupt can occur at any time asynchronous, it could break at any time of the currently executing code
  • Soft interrupt can wake up and tasklet ---- kernel or soft interrupt and scheduling tasklet at any time, interrupting the currently executing code
  • ---- because the kernel has kernel preemption preemptive, so the kernel task may be preempted by another task
  • ---- sleep and synchronization and user space could sleep in the kernel execution process, which will wake up the scheduler, resulting in scheduling a new user process execution
  • Symmetric Multiprocessing ---- two or more processors can execute code simultaneously

The real challenge is to find a possibility that the potential for concurrent execution, and consciously take certain measures to prevent concurrent execution.

In fact, the real use locks to protect shared resources is not difficult, especially in the early design of the code did just that, something even simpler.

Identify the real need to share critical data and response area, it is the real challenging place.

 

2.2 To understand what protection

Find out what data needs to be protected is the key.

Thread local data is only accessed it, obviously you do not need protection.

 

Third, the deadlock

 

Fourth, the contention and scalability

Guess you like

Origin www.cnblogs.com/ch122633/p/11013358.html