gpt4 book ai didi

c - 根据 C 规范,中断是信号吗?

转载 作者:行者123 更新时间:2023-12-02 00:47:20 26 4
gpt4 key购买 nike

C11 5.1.2.3/5:

When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects that are neither lock-free atomic objects nor of type volatile sig_atomic_t are unspecified, as is the state of the floating-point environment. The value of any object modified by the handler that is neither a lock-free atomic object nor of type volatile sig_atomic_t becomes indeterminate when the handler exits, as does the state of the floating-point environment if it is modified by the handler and not restored to its original state.



这个问题专门针对嵌入式系统,在没有类似操作系统的设置的情况下。

在嵌入式系统上,经典信号(POSIX 风格)不存在。假设我们有一个调用多个函数的中断,所有函数都在同一个((半)全局)变量上工作,但是这个变量不是在中断外上下文中使用的。就像是
static enum State state;

static void setStateTo1(void)
{
state = stateOne;
}

static void setStateTo2(void)
{
state = stateTwo;
}

void ISR(void)
{
if (state == stateOne)
setStateTo2();
else
setStateTo1();
}

两个问题:
  • 中断是信号吗?
  • 上面的代码是未定义行为的一个例子,因为 state不是 volatile ?
  • 最佳答案

    术语“信号”是指其行为由标准(C11 7.14“信号处理”)定义的特定事物;该定义包括信号有一个编号,并且可以通过跳转到由 signal 安装的处理程序来中断执行。功能等等。

    除了信号和线程之外,该标准不涵盖任何其他类型的异步代码执行。

    如果您的实现提供了不符合信号规范的任何其他类型的中断,并且中断处理程序更改了抽象机的行为,那么我们可以说该实现不符合要求,或者说要安装的代码信号处理程序导致未定义的行为。

    为了定义良好,您可以让处理程序除了编写 volatile 原子变量之外什么都不做。

    所以,你的第二个问题超出了标准的范围。在标准 C 中,如果程序从未调用过这些函数,优化器可以将所有这些函数删除为未使用。

    实际上,提供非标准中断的实现将在自己的权限下定义自己的行为,您可以将其视为具有附加功能的 C 语言的扩展方言。

    关于c - 根据 C 规范,中断是信号吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60864187/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com