使用c ++和x86和x64架构访问处理器中断 [英] Access processor interrupts with c++ and x86 and x64 architectures

查看:378
本文介绍了使用c ++和x86和x64架构访问处理器中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从c ++代码访问处理器中断(我想我需要使用汇编代码)。

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

这里的东西。我习惯于使用嵌入式系统(PIC,ARM7和Atmel处理器),我可以编程我的中断没有任何问题(我使用C)。所有寄存器都被映射,并且根据应用的不同,所有寄存器都很容易。现在我正在使用x86和x64架构,我想(强调 WANT )能够在这些处理器中做同样的事情。

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.

例如:我想有一个线程在c + +运行代码,不断地验证键被按下,而不按enter键(cin,getchar等)。我想编程一个定时器中断,所以我将能够在特定的时间执行操作。我知道我想做的很多事情都可以通过操作系统来完成,但我真的想拥有自己这样做的自由。

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.

目前我正在使用Linux(Ubuntu),但我一定会很快使用Windows,如果这是一个需要回答这个问题的信息。

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

推荐答案

对于用户级进程,中断由信号替换。您可以通过调用 setitimer 来向您的进程发送信号。但最有可能的是,做你想做的事情的最好方法是两件事之一:

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:


  1. 使用事件循环。基本上,编写你的程序作为一个巨大的循环,定期检查,看看是否有任何需要做的。在循环中,检查时间,检查按键,等等。

  1. 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 read to 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.


I想要有一个线程在c + +运行代码,不断地验证键被按下,而不按enter键(cin,getchar,等)。

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

那样做。这需要一个线程,它需要将终端的输入模式更改为不需要输入密钥的模式。这与中断无关。

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.

这篇关于使用c ++和x86和x64架构访问处理器中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆