Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 9 An Introduction to Kernel Synchronization #73

Open
jason--liu opened this issue Jul 10, 2021 · 0 comments
Open

Chapter 9 An Introduction to Kernel Synchronization #73

jason--liu opened this issue Jul 10, 2021 · 0 comments

Comments

@jason--liu
Copy link
Owner

Critical Regions and Race Conditions

Code paths that access and manipulate shared data are called critical regions (also called
critical sections).

并发原因

  • Interrupts— An interrupt can occur asynchronously at almost any time, interrupting the currently executing code.
  • Softirqs and tasklets— The kernel can raise or schedule a softirq or tasklet at almost any time, interrupting the currently executing code.
  • Kernel preemption— Because the kernel is preemptive, one task in the kernel can preempt another.
  • Sleeping and synchronization with user-space— A task in the kernel can sleep and thus invoke the scheduler, resulting in the running of a new process.
  • Symmetrical multiprocessing— Two or more processors can execute kernel code at exactly the same time.

写内核代码前需要问自己几个问题

  • Is the data global? Can a thread of execution other than the current one access it?
  • Is the data shared between process context and interrupt context? Is it shared between two different interrupt handlers?
  • If a process is preempted while accessing this data, can the newly scheduled process access the same data?
  • Can the current process sleep (block) on anything? If it does, in what state does that leave any shared data?
  • What prevents the data from being freed out from under me?
  • What happens if this function is called again on another processor?
  • Given the proceeding points, how am I going to ensure that my code is safe from concurrency?

死锁

A deadlock is a condition involving one or more threads of execution and one or more
resources, such that each thread waits for one of the resources, but all the resources are
already held.

死锁类型

ABBA
image
密码死锁应该遵循的规则

  • Implement lock ordering.
  • Prevent starvation.Ask yourself, does this code always finish? If foo does not occur, will bar wait forever?
  • Do not double acquire the same lock.
  • Design for simplicity.

在锁代码上方添加获取锁顺序的注释是很好的实践。解锁的顺序与死锁无关,尽管通常的做法是按照与获取锁的顺序相反的顺序释放锁

Contention and Scalability

The term lock contention, or simply contention, describes a lock currently in use but that
another thread is trying to acquire.
锁的作用是让系统顺序访问临界资源,因此锁很明显会拖慢系统。
Scalability is a measurement of how well a system can be expanded.
// TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant