简体   繁体   English

C中的阻塞/非阻塞计时器

[英]blocking/non-blocking timer in C

This might be an often repeated question, sorry for bringing it back again. 这可能是一个经常重复的问题,对不起再把它带回来。 I was unable to find a solution :( . I am writing a VM monitoring code in C in Linux. I want to get the read and write count of all the VM's every 10 seconds. Is there any C library that provides this feature(timer alone), blocking/non-blocking timer doesn't matter. Thanks !! 我无法找到解决方案:(。我正在Linux中用C语言编写VM监控代码。我希望每隔10秒获取所有VM的读写次数。是否有任何C库提供此功能(定时器)单独),阻塞/非阻塞计时器无所谓。谢谢!!

Regards, Sethu 此致,Sethu

For a non-blocking timer (on POSIX systems), use alarm : 对于非阻塞计时器(在POSIX系统上),使用alarm

int main(void) {
  signal(SIGALRM, monitor);
  monitor(0);
  /* ... */
}

void monitor(int signal) {
  /* ... */
  alarm(10);
}

But for a blocking timer, use sleep as described by lalli . 但是,对于一个阻挡计时器,使用sleep拉里说明。

sleep(10);

will make the thread sleep for 10 seconds in a unix system. 将使线程在unix系统中休眠10秒。 Use it in a loop with the code for monitoring, and you are good to go. 在带有监控代码的循环中使用它,你很高兴。 If you're using windows as the host for monitoring, then sleep function will accept in milliseconds. 如果您使用Windows作为监视主机,则sleep函数将在几毫秒内接受。

Also, as multithreading/multiprocessing is required, implementations will vary based on os/platform. 此外,由于需要多线程/多处理,实现将根据os / platform而变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM