How do signals interact with sequence points?

这一生的挚爱 提交于 2021-02-07 14:35:05

问题


The C89 standard states:

At sequence points volatile objects are stable in the sense that previous
evaluations are complete and subsequent evaluations have not yet occurred.

The C89 standard also states:

When the processing of the abstract machine is interrupted by receipt of a
signal, only the values of objects as of the previous sequence point may be
relied on.

These requirements leave me confused, because I can't imagine how they would actually be implemented. I only have a rudimentary understanding of x86-64 assembler, but here's what is running through my head. Let's say that we have a volatile struct, and it's many kilobytes large. If you copy an existing struct into this volatile struct in C, then the resulting machine code might be very large. Now, let's say that this large amount of machine code is executing, and some shell on the system executes the "kill" command on the process, causing the raising of some signal. My understanding (or rather my guess) is that the process would be interrupted in mid-copy, causing the requirement on volatile to be broken, and causing the struct be malformed from the perspective of any signal handler.

Obviously things don't actually work like this, but then how do they actually work? For the sake of argument feel free to assume that the platform is x86-64 or x86 Linux, Windows, OS X, or some other common UNIX.


回答1:


At sequence points volatile objects are stable in the sense that previous evaluations are complete and subsequent evaluations have not yet occurred.

This wouldn't be violated, since the interruption is not a sequence point. This rule only describes what happens "at sequence points". If we are interrupted after sequence point A and before sequence point B completes, we aren't "at" sequence point A or sequence point B.

When the processing of the abstract machine is interrupted by receipt of a signal, only the values of objects as of the previous sequence point may be relied on.

This wouldn't be violated. If we are interrupted between sequence points A and B, say in mid copy, all the changes made at sequence point A can be relied on. They're done.

Being able to relying on a previous modification doesn't mean that you won't see any effects of a subsequent modification. Values are stable until some future bit of code can modify them.

So the obvious implementation doesn't violate either of the requirements.



来源:https://stackoverflow.com/questions/24975051/how-do-signals-interact-with-sequence-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!