标准C可以处理中断吗?

问题描述 投票:0回答:3

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

我刚刚遇到了标准 C 库的“信号”功能,但维基百科对其使用的了解非常少,我认为它没有达到目的。

c interrupt
3个回答
6
投票

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,                     /* which IRQ? */
                 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 handle_interrupt (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 代码不偏离标准,那么您将需要使用一些汇编程序来调用您的函数。


3
投票

没有。

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

您必须执行类似于您所看到的实现的操作,并为您想要支持的每个硬件平台使用特定的代码。


© www.soinside.com 2019 - 2024. All rights reserved.