Software interrupts in C?

7.1k Views Asked by At

I have understand the concepts of software interrupt and hardware interrupts theoretically but anyone could give me an example for software interrupt and explain it ?? Please it would be a great help for me.

C program example for windows or Linux operating system

1

There are 1 best solutions below

0
On

Interrupts are handled by the operating system kernel. Applications don't see them (because the kernel processes all interrupts so hides them from applications). On Linux, application processes see signals. See signal(7) and read Advanced Linux Programming.

Notice that the C11 standard (on the C programming language) don't know about interrupts.

Please understand that signals are not interrupts (and Linux applications don't see directly any interrupts, except by measuring them thru proc(5), see file /proc/interrupts). And signal handlers have strong restrictions: only async-signal-safe functions ca be called (directly or indirectly) from a signal handler. Often, setting a volatile sig_atomic_t flag is sensible in your signal handler, and your application should test it elsewhere.

If you have an event loop (e.g. around poll(2) etc...) using the Linux specific signalfd(2) could be very convenient.

So when programming a Linux application (and probably also a Windows one) you don't care about interrupts (but you might handle some signals). BTW, a Linux kernel typically sees hundreds (or perhaps thousands) of interrupts each second, and wake up some driver and/or reschedule some task for most of them. A given Linux application process usually handles much less than one signal per second (but YMMV).