I would like to know if it’s possible to access processor interrupts from a c++ code (I imagine I will need to use assembly code).
Here’s the thing. I am used to working with embedded systems (PIC, ARM7 and Atmel processors) and I can program my interrupts without any problem (I use C). All registers are mapped, and all in all it’s farely easy, depending on the application. Now I’m working with x86 and x64 architecture and I want (emphasis on WANT) to be able to do the same in these processors.
For example: I want to have a thread in c++ running a code that constantly verifies the key being pressed at the moment without pressing the enter key (cin, getchar, etc). I want to program a timer interrupt so I will be able to perform actions at an specific time. I understand that many of the things I want to do can be done via the operating system, but I really want to have the freedom to do so on my own.
Currently I’m using Linux (Ubuntu), but I will certainly be working with Windows soon, if that’s an information needed to answer that question
For user-level processes, interrupts are replaced by signals. You can arrange to have a signal sent to your process by calling
setitimer. But most likely, the best way to do what you’re trying to do is one of two things:Use an event loop. Basically, write your program as a giant loop that periodically checks to see if there’s anything it needs to do. In the loop, check the time, check for keypresses, and so on. Do a little bit of work on whatever you need to do, and loop again.
Use threads. You can have a thread just to watch the time and trigger timer jobs. You can have a thread that blocks on a
readto act like an interrupt when data arrives.Likely it was drilled into your head that you do minimal work in the interrupt handler itself, typically just passing on information to other code that runs in a normal context. Well, the OS already does that part for you. You just have to write the code that waits for the interrupt handler (or whatever is needed) to detect and begin processing the event.
So do that. That requires a thread and it requires changing the terminal’s input mode to one that doesn’t require an enter key. That has nothing to do with interrupts.