自旋锁如何防止过程中断? [英] How spinlock prevents the process to be interrupted?

查看:707
本文介绍了自旋锁如何防止过程中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此站点上读到一个答案,说自旋锁减少了上下文切换的开销,然后我阅读了与此相关的教科书声明:

I read an answer on this site says the spin-lock reduce the overhead with context switches, and after that I read an textbook statement related to this:

自旋锁使繁忙的等待程序不被中断.

Spin-lock makes a busy waiting program not be interrupted.

我的问题是标题.

由于这本书使用while循环来表示自旋锁的 spin 部分的实现,因此以下是我根据这种考虑尝试说明自己的理由

Since the book uses while-loop to indicate the implementation of the spin part of a spin-lock, the following is my reasoning trying to explain myself with this consideration.

这听起来像是,如果有一个程序有一个忙于等待的while循环,那么所有其他程序(进程)将不会永远执行,但这不会使多程序环境崩溃,因为其他进程无法一定时间间隔过去后,需要更长的执行时间?但是我记得操作系统会阻止任何进程永远控制CPU吗?

This sounds like if there is a program with a busy waiting while loop then all the others program(processes) won't be executed forever, but won't this make a multiprogramming environment broken down, since the others processes can no longer to be executed after certain time interval has past? But I remember that the OS will prevent any process from dominate the CPU forever?

或者这在大多数体系结构中通常都是正确的,因此不建议这样做,因为多重编程的程度会降低,或者人们必须确切知道它将停止多长时间?抱歉,您的问题含糊不清,但我输入的内容与本书所写的完全相同.

Or this is generally true in most architecture so it's not recommended since the degree of multiprogramming decrease, or one has to know precisely how long it will stop? Sorry for vague question but what I typed is exactly the same as the book states.

有关我的困惑的更多详细信息:我认为对于给定的while循环

For more details about my confusion: I think for a given while loop

while (this == true);

仅是扩展版本

if (this == true);
if (this == true);
if (this == true);
...
...    
if (this == true); // (*)
...            
...
if (this == true);
...

那么为什么由于某些原因(例如,该进程的时间间隔结束并且从就绪队列中选择另一个进程)而不会在上面的(*)步骤中中断它?

So why it won't be interrupted at some (*) step above, for some reason like the time interval for the process is ended and another process is chosen from the ready queue?

推荐答案

自旋锁确实可以防止该进程被其他进程中断.这种预防措施的实现是特定于OS的:由于OS本身执行进程调度,因此能够将进程标记为无法重新调度".

A spinlock really prevents the process to be interrupted by other processes. Implementation of this prevention is OS-specific: since OS by itself performs process scheduling, it is able to mark the process as "cannot be rescheduled".

但是您会问其他进程将其标记为不间断是否公平.实际上,有2个(至少)自旋锁"概念:一个用于内核空间线程,一个用于用户空间线程:

But you ask about whether it is fair for other processes to mark the one as uninterruptible. Actually, there are 2 (at least) notions of "spinlock": one for kernel space threads, and one for user-space threads:

    作为操作系统内核的一部分,
  1. 内核进程本身信任:如果一个进程获得了自旋锁,它希望在短时间内将其释放.自旋锁释放后,该过程将不再被视为不间断的,可以从此切换.

  1. Kernel processes, as part of OS kernel, trusts themselves: if one process acquire spinlock, it expects to release it in a short time. Once the spinlock is released, the process is no longer treated as uninterruptible and can be switched from.

大多数描述自旋锁的书都谈论内核(受信任的)过程.

Most books which describes spinlocks talk about kernel (trusted) processes.

用户进程不信任"自己.这就是为什么未为用户进程提供真"自旋锁的原因,该自旋锁在无限时间内使进程不间断.操作系统最多只能提供混合版本的自旋锁和互斥锁:在很短的时间内,试图抓住自旋锁的过程实际上是不间断的.但是,如果在进程获得自旋锁之前时间已到,则该进程将移至正在等待状态,从而允许其他进程在同一内核上运行.因此提供了公平".

User processes, in opposite, do not "trust" themselves. This is why a "true" spinlock, which renders a process as uninterruptible for an infinite time, is not provided for user processes. At most, OS provides hybrid version of a spinlock and a mutex: during the short period of time the process, which tries to grab a spinlock, is actually rendered as uninterruptible. But if the time expires before the process acquires the spinlock, the process is moved to the waiting state, allowing other processes to be run on the same core. So "fairness" is provided.


实际上,"true"自旋锁不仅在等待期间而且在保持自旋锁的同时使该过程不间断.避免死锁是必需的.这适用于内核进程(信任自己).对于用户进程,OS可能会为该进程提供一些(也是很短的)不间断状态的时间,直到它释放自旋锁为止.如果在释放自旋锁之前时间已到,该过程将再次被中断.


Actually, "true" spinlock renders the process as uninterruptible not only while it waits, but also while it holds the spinlock. It is required for avoiding deadlocks. This is works for kernel processes (which trusts themselves). As for user processes, OS may give for the process some (also short) time of uninterruptible state until it releases the spinlock. If the time expires before spinlock is released, the process becomes interruptible again.

这篇关于自旋锁如何防止过程中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆