-6

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

user2984410
  • 39
  • 2
  • 7
  • 1
    An example is given in the 3rd paragraph here: https://en.wikipedia.org/wiki/Interrupt – Oliver Charlesworth Dec 22 '13 at 12:41
  • @ Oli Charlesworth : I understand the theoretical concept . Could you please explain me with c program example ?? – user2984410 Dec 22 '13 at 12:44
  • 1
    Such interrupts are handled by the OS and drivers. There is not going to be any sensibly-postable example. Linux open-source - go look at some drivers. – Martin James Dec 22 '13 at 12:46
  • @user2984410 - An analogy - You (the computer) is thinking. The interrupt is an idiot has poked you in the ribs with a pencil. You stop what you are doing and shout an expletive. that is an interrupt – Ed Heal Dec 22 '13 at 12:53
  • 1
    There are some compilers for embedded systems that have extensions for `C` to handle interrupts, but, as others have pointed out, these are not applicable for Windows or Linux. – quamrana Dec 22 '13 at 13:11

1 Answers1

3

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).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547