双链列表创建过程中的上下文切换 [英] context switch during doubly linked list creation

查看:77
本文介绍了双链列表创建过程中的上下文切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

莫里斯·巴赫(Maurice Bach)的"Unix操作系统设计"中有一个示例 提到了双重链接列表可能由于以下原因而被销毁的原因: 创建期间的上下文切换. (他继续说,通过在这样的关键代码区域内提高处理器级别可以防止这种情况的发生,但是我很难理解他试图首先显示问题的原因)他包括的示例代码如下: 如下:

There's an example in Maurice Bach's The Design of the Unix Operating System that mentions how it's possible for a doubly linked list to be destroyed due to a context switch during its creation. (He goes on to say that this is prevented by raising the processor level during such critical regions of code, but I'm having trouble understanding his reasoning that tries to show the problem in the first place) The sample code that he includes is as follows:

struct queue {

} *bp, *bp1;
bp1 -> forp = bp -> forp;
bp1 -> backp = bp;
bp -> forp = bp1;
/* consider possible context switch here */
bp1 -> forp -> backp = bp1;

他写的图最初显示:

              |     |
              | bp1 |

->    |    |    ->         |    |
<-    | bp |    <-         |    |

然后,显示最终状态:

->  |    |  ->  |     |   -> |    |
<-  | bp |  <-  | bp1 |      |    |
       ^
        \                       /
         ----------------------- 

我正在尝试讲解逻辑,但是我不能说出为什么代码会导致 到一个破碎的双向链表,如图所示.有人可以解释发生了什么 在上下文切换期间导致此问题?

I'm trying to walk through the logic, but i can't tell why the code would lead to a broken doubly linked list as shown. Can someone explain what's occuring during the context switch to cause this problem?

(p.s.将被标记为双向链接列表,但没有标记创建权限)

(p.s. would have tagged as doubly-linked-list, but no tag creation permissions)

推荐答案

我的错误阅读不够仔细-Maurice在图表前的页面中说,如果另一个进程操纵指针,上下文切换将如何破坏代码"在链表上重新运行原始程序之前."我很困惑,因为我试图仅从图和代码中获取足够的信息,但都没有提到切换到该进程将处理内存中相同的数据结构这一事实(尽管存在上下文切换).仍然不是100%明确/有动机的示例恕我直言).无论哪种方式,当我使上下文从读取一页切换到下一页时,显然我自己的内核有一些数据损坏.

My mistake in not reading carefully enough - Maurice says in the page before the diagrams how the context switch would break the code IF another process "were to manipulate the pointers on the linked list before the original process ran again." I was confused because I was trying to get enough information from just the diagram and the code, neither of which mentioned the fact that the process being switched to would be handling the same data structure in memory (despite there having been a context switch.. still not a 100% clear/motivated example imho). Either way, apparently my own kernel had a bit of data corruption when i made the context switch from reading one page to the next..

这篇关于双链列表创建过程中的上下文切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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