【问题标题】:Can interrupts be handled in standard C?可以在标准 C 中处理中断吗?
【发布时间】:2014-01-24 15:45:20
【问题描述】:

在标准 C (ANSI/ISO) 中是否有处理硬件中断的方法?到目前为止,我看到的所有实现要么使用编译器特定的语言扩展,要么使用预处理器指令。

我刚刚遇到标准 C 库的“信号”功能,但 wikipedia 对它的使用非常轻,我认为它没有达到目的。

【问题讨论】:

  • 中断处理基本上是特定于平台的。 C标准不是。不能用纯标准 C 来完成。

标签: c interrupt


【解决方案1】:

POSIX 信号可以允许用 C 语言编写的用户程序捕获和处理某些类型的中断和/或异常。这是我所知道的最标准的方法。

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
int a,b,*p;
jmp_buf jump_destination;

void exception_handler (int sg)
{
  printf ("Error dereferencing pointer\n");
  p=&b; /* pointer quick fix. */
  longjmp(jump_destination,1); /* long GOTO... */
}

void main (void)
{
  int i;

  signal (SIGSEGV, exception_handler);
  b=0; p=NULL;    

  setjmp(jump_destination); /* ...to this position */
  printf ("Trying to dereference pointer p with value %08.8X... ",p);
  printf ("After dereferencing pointer, its value is: %d\n", *p);
}

对于硬件中断,C 没有明确的语义,主要是因为它不是必需的。例如,Linux 设备驱动程序可以为硬件设备安装自己的中断处理程序。您只需使用负责处理中断的函数的地址调用request_irq() 函数。

例如,这将为 RTC 芯片安装一个中断处理程序(假设它存在并在您的硬件中激活)

...
...
res=request_irq (8,                     /* que IRQ queremos */
                 interrupt_handler,         /* address of handler */
                 IRQF_DISABLED,         /* this is not a shared IRQ */
                 “mydriver",            /* to be shown at /proc/interrupts */
                 NULL);
if (res!=0)
{
  printk ("Can't request IRQ 8\n");
}
...
...

您的处理程序只是一个普通的 C 函数:

static irqreturn_t gestor_interrupcion (int irq, void *dev_id, struct pt_regs *regs)
{
  /* do stuff with your device, like read time or whatever */
   ...
   ...
   ...

  return IRQ_HANDLED; /* notify the kernel we have handled this interrupt */
}

这是可能的(使用常规的 C 函数来处理硬件中断),因为处理程序本身是从另一个内核函数调用的,该函数负责保留当前上下文,因此被中断的进程不会注意到任何事情。如果您在“裸机”计算机中处理中断,并且希望使您的 C 代码不偏离标准,那么您将需要使用一些汇编程序来调用您的函数。

【讨论】:

    【解决方案2】:

    没有。

    信号(来自 POSIX)不用于处理硬件中断,尽管它们可以连接到它们。它们用于处理更高级别的系统事件。

    您必须按照您所见的实现方式,为您想要支持的每个硬件平台使用特定代码。

    【讨论】:

      【解决方案3】:

      在某些时候,您的实现必须与 C 规范之外的某些标准进行交互,无论是 Linux's family of types and function signatures that define interrupt handlers,为另一个操作系统构建的类似 C 代码主体,或者可能是嵌入式平台的硬件规范会allow you to implement your own。如果你能明确你的目标,也许可以给出更具体的答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-26
        相关资源
        最近更新 更多