如何正确使用nanosleep()以特定频率运行代码? [英] How to correctly use nanosleep() to run code at specific frequency?

查看:80
本文介绍了如何正确使用nanosleep()以特定频率运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为6502 CPU做一个仿真器,我想控制它的运行频率.

I am making an emulator for the 6502 CPU and i want to control the frequency at which it is running.

没有任何延迟,我的最大平均频率约为70-80 MHz.但是,当我尝试使用nanosleep来控制频率时,它只能工作到我达到1 mS延迟(1000000 nS).如果我尝试将延迟设置为更低,例如,设置为.1 mS,则它不会改变平均执行时间.对我来说奇怪的是,如果我将延迟设置为.1 mS,则实际延迟竟然是〜.008 mS,并且对于1-99999 nS范围内的任何值都不会改变.

Without any delay, my max average frequency is about 70-80 MHz. However, when i try to use nanosleep in order to control frequency, it only works until i reach 1 mS delay (1000000 nS). If i try to set the delay any lower, for example to .1 mS, it does not change the average execution timing. What is strange to me is that if i set the delay to .1 mS, the actual delay turns out to be ~.008 mS and it does not change for any value in range 1-99999 nS.

我已经尝试在调试和发布模式下编译代码

I have tried compiling the code both in debug and release mode

我使用nanosleep是否出错?这是我第一次使用nanosleep,我以前只使用过睡眠和usleep,所以我有点困惑.有没有更好的方法来控制代码执行的频率?

Am i using nanosleep wrong? It is my first time using nanosleep, i have only used sleep and usleep before, so i am a bit confused. Is there any better way to control the frequency of code execution?

推荐答案

如何正确使用nanosleep()以特定频率运行代码?

How to correctly use nanosleep() to run code at specific frequency?

我说不可能使用 nanosleep 以特定的频率运行代码(或者,是否有可能达到一定的精度并有些漂移?).来自 man nanosleep 强调我的,但整体是相关的:

I say it's impossible to use nanosleep to run code at specific frequency (or, it's possible, up to a certain accuracy with some drift?). From man nanosleep emphasis mine, but whole is relevant:

nanosleep()函数将导致当前线程被暂停执行,直到指定的时间间隔为止通过rqtp参数已过去或信号已传递到调用线程,其作用是调用信号捕获功能或终止进程.暂停时间可能比请求的时间长,因为参数值已四舍五入设置为睡眠分辨率的整数倍,或者由于系统安排了其他活动.但是,除了在被信号中断的情况下,暂停时间应不小于,该时间由rqtp指定,该时间由系统时钟CLOCK_REALTIME.

The nanosleep() function shall cause the current thread to be suspended from execution until either the time interval specified by the rqtp argument has elapsed or a signal is delivered to the calling thread, and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested because the argument value is rounded up to an integer multiple of the sleep resolution or because of the scheduling of other activity by the system. But, except for the case of being interrupted by a signal, the suspension time shall not be less than the time specified by rqtp, as measured by the system clock CLOCK_REALTIME.

是否有更好的方法来控制代码执行的频率?

Is there any better way to control the frequency of code execution?

使用计时器并尝试依赖内核来适当地安排定期执行,而不是自己执行.使用 timer_create 可以在指定的超时后请求中断,并在该超时后运行代码,您还可以考虑使进程成为实时进程.我想用伪代码:

Use timers and try to depend on the kernel to properly schedule periodic execution, rather then doing it yourself. Use timer_create to request an interrupt after specified timeout and run your code after that timeout, and you may also consider making your process a real-time process. In pseudocode I think I would:

void timer_thread(union sigval s) {
    do_your_code();
}
int main() {
   timer_t t;
   timer_create(CLOCK_MONOTONIC, &(struct sigevent){
        .sigev_notify = SIGEV_THREAD,
        .sigev_notify_function = timer_thread
   }, &t);
   timer_settime(t, &(struct itimerspec){{.tv_sec = 1},{.tv_sec = 1}}, 0);
   while (1) {
      // just do nothing - code will be triggered in separate thread by timer
      pause();
   }
}

这篇关于如何正确使用nanosleep()以特定频率运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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