Tag: 定时器

软件用于Windows操作系统的C中断服务程序

#include #include #include void Task() { printf(“Hi”); } int main ( ) { time_t t; clock_t start, end; long i; long count; double x = 0.0; count = 2; start = clock(); time(&t); printf(ctime(&t)); printf( “Counting to %ld\n”, count ); if(count) { Task(); } end = clock(); printf( “That took %f seconds and I counted […]

如何解决这个Linux Timer

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define million 1000000L timer_t firstTimerID, secondTimerID, thirdTimerID; double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster; struct sockaddr_in addr, client; int acceptSocket; char buf[256]; long rc, sentbytes; int port = 18037; void TASK1(Task2ms_Raster) { struct timespec start, stop; double startTime, stopTime; if( (startTime = […]

POSIX定时器:定时器的信号处理程序冻结

这篇文章与以下内容有关: POSIX TIMER-有多个计时器 我想在SignalHandler中调用一个函数。 此函数是TCP套接字客户端(getSpeed)。 每次计时器滴答一秒钟时它从服务器获取数据并发送一个信号,然后调用相应的处理程序。 首先,我不确定从信号处理程序调用函数是否是一个好习惯。 现在问题是,每当我执行与服务器通信的程序时,我的代码就会随机冻结。 如果这种使用信号定义计时器的方式很糟糕(并且可能导致一些隐藏的竞争条件)是否有更好的方法来编码上述场景? #include #include #include #include #include #include #include #include static timer_t tid; static timer_t tid2; void SignalHandler(int, siginfo_t*, void* ); timer_t SetTimer(int, int, int); int getSpeed(void) { int sockFD = 0; struct sockaddr_in addr; int numbytes; unsigned char rxMsg[128]; /* open socket */ if((sockFD = socket(AF_INET, SOCK_STREAM, […]

全球变量的Linux计时器

我在互联网上找到了下面的代码,我试图理解Linux计时器是如何工作的,无论如何,正如你在下面看到的那样,counter1是全局var,如果while正在处理它并且计时器关闭并改变会发生什么counter1的值,我需要锁吗? // timertst1.c: Simple timer demo program. Use cross-gcc // Vers. 1.00 – 21.Oct. 2002 // kdwalter@t-online.de #include #include #include #include // This is a timer handler. int counter1 = 0; void timerHandler (int signum) { printf (“timerHandler: counter= %d\n”, counter1++); fflush (stdout); } // This is the one and only main function. int main […]

等待信号,然后继续执行

我正在尝试制作一个暂停执行的程序, 直到信号到达 。 然后,在信号到达后,我只想让我的代码从原来的位置继续执行 。 我不希望它执行函数处理程序或任何其他。 有一个简单的方法吗? 我已经挣扎了一个星期左右,在这里和那里阅读,并没有设法得到一个完整的操作代码。 特别是,我希望主程序创建一个等待某个特定事件发生的线程 (例如,用户已经向stdin输入了一些数据)。 与此同时,主程序正在做一些事情,但在某些时候它会暂停执行,直到收到信号。 信号可能来自线程,因为它已检测到事件,或者可能是由于超时,因为我不希望它等待。 我已经制作了一些代码,但它没有按预期工作…… /* * This code SHOULD start a thread that gets messages from stdin. * If the message is a “quit”, the thread exits. Otherwise it raises * a signal that should be caught by the main program. * The main program simply […]

为什么timer_create在solaris 10中抛出SIGEV_THREAD错误?

我通过使用timer_create写了一块用于设置定时器来调用一个线程,其中我将sigev_notify设置为SIGEV_THREAD,它给我错误EINVAL(无效参数)但是当我设置sigev_notify时SIGEV_SIGNAL代码工作正常。 我的这段代码即使在solaris 11中也适用于所有操作系统,但对于solaris 10,我给出了错误。 代码如下: { int status =0; struct itimerspec ts; struct sigevent se; se.sigev_notify = SIGEV_THREAD; se.sigev_value.sival_int = val; se.sigev_notify_function = func; se.sigev_notify_attributes = NULL; status = timer_create(CLOCK_REALTIME, &se, timer_id); ts.it_value.tv_sec = abs(delay); ts.it_value.tv_nsec = (delay-abs(delay)) * 1e09; ts.it_interval.tv_sec = abs(interval); ts.it_interval.tv_nsec = (interval-abs(interval)) * 1e09; status = timer_settime(*timer_id, 0, &ts, 0); } […]

如何在C中以毫秒为单位获取程序的执行时间?

目前我通过调用以下命令获得程序的执行挂起时间: time_t startTime = time(NULL); //section of code time_t endTime = time(NULL); double duration = difftime(endTime, startTime); 是否有可能以毫秒为单位获得墙上时间? 如果是这样的话?

需要程序来说明在GNU C中使用settimer和alarm函数

任何人都可以在gnu C中说明settimer或alarmfunction的使用,请参考一些程序示例吗? 我有一个连续处理一些数据的程序,我需要设置一个每t秒关闭一次的计时器/警报,响应于此,我需要将处理过的数据存储到一个文件中。 这个文件写入必须是异步的。 我浏览了GNU C Library页面,但我理解不了多少.. [编辑] 我得到了这个程序: #include #include #include #define INTERVAL 1 int howmany = 0; void alarm_wakeup (int i) { struct itimerval tout_val; signal(SIGALRM,alarm_wakeup); howmany += INTERVAL; printf(“\n%d sec up partner, Wakeup!!!\n”,howmany); tout_val.it_interval.tv_sec = 0; tout_val.it_interval.tv_usec = 0; tout_val.it_value.tv_sec = INTERVAL; /* 10 seconds timer */ tout_val.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &tout_val,0); […]