我需要在C中运行while循环10秒钟。我尝试过:
clock_t start = clock();
while( ( clock() - start ) < ( 10 * CLOCKS_PER_SEC ) ) {
work..
}
但它不起作用。
答案 0 :(得分:0)
请指定什么不起作用。我会猜测:
在Debian 10上,正确的宏为In [198]: np.indices((2,3,4)).sum(axis=0)
Out[198]:
array([[[0, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5]],
[[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]]])
In [199]: idx = np.indices((2,3,4), sparse=True)
In [200]: idx
Out[200]:
(array([[[0]],
[[1]]]), array([[[0],
[1],
[2]]]), array([[[0, 1, 2, 3]]]))
In [201]: sum(idx)
Out[201]:
array([[[0, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5]],
[[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]]])
。但是也许Ubuntu添加了CLOCKS_PER_SEC
作为别名,在这种情况下,这不是您遇到的问题。
实现计时器的方式,循环可能会超过10秒(如果“工作”时间不是10s的除数)。 如果这是您的实际问题,则应检查异步任务和信号。 这样,您可以让一个进程(或线程)无限循环地进行初始工作,而第二个进程在经过10秒后通知第一个进程(例如,发出信号)。但这将需要更复杂的代码!
希望我能给您带来帮助,但是如果我不尝试更精确地回答您的问题,那么
答案 1 :(得分:0)
似乎您真正想要的是一个实际的计时器,以便在经过指定的时间间隔时调用一个函数。在该功能中,您可以处理断开客户端与服务器的连接。
请参阅:https://programming.vip/docs/linux-c-language-timer.html
您应该可以根据需要修改此示例:
#include<stdio.h>
#include<signal.h>
#Include<sys/time.h>//itimerval structure definition
int handle_count=0;
void set_time(void)
{
struct itimerval itv;
itv.it_interval.tv_sec=10;//Load automatically and then respond every 10 seconds
itv.it_interval.tv_usec=0;
itv.it_value.tv_sec=5;//Time of First Timing
itv.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&itv,NULL);
}
void alarm_handle(int sig)
{
handle_count++;
printf("have handle count is %d\n",handle_count);
}
void main(void)
{
struct itimerval itv;
signal(SIGALRM,alarm_handle);
set_time();
while(1){
getitimer(ITIMER_REAL,&itv);
printf("pass second is %d\n",(int)itv.it_value.tv_sec);
sleep(1);
}
return;
}
一些手册页链接:
另一个StackOverflow answer的另一个示例:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
static unsigned int pass_value_by_pointer = 42;
void Timer_has_expired(union sigval timer_data)
{
printf("Timer expiration handler function; %d\n", *(int *) timer_data.sival_ptr);
}
int main(void)
{
struct sigevent timer_signal_event;
timer_t timer;
struct itimerspec timer_period;
printf("Create timer\n");
timer_signal_event.sigev_notify = SIGEV_THREAD;
timer_signal_event.sigev_notify_function = Timer_has_expired; // This function will be called when timer expires
// Note that the following is a union. Assign one or the other (preferably by pointer)
//timer_signal_event.sigev_value.sival_int = 38; // This argument will be passed to the function
timer_signal_event.sigev_value.sival_ptr = (void *) &pass_value_by_pointer; // as will this (both in a structure)
timer_signal_event.sigev_notify_attributes = NULL;
timer_create(CLOCK_MONOTONIC, &timer_signal_event, &timer);
printf("Start timer\n");
timer_period.it_value.tv_sec = 1; // 1 second timer
timer_period.it_value.tv_nsec = 0; // no nano-seconds
timer_period.it_interval.tv_sec = 0; // non-repeating timer
timer_period.it_interval.tv_nsec = 0;
timer_settime(timer, 0, &timer_period, NULL);
sleep(2);
printf("----------------------------\n");
printf("Start timer a second time\n");
timer_settime(timer, 0, &timer_period, NULL);
sleep(2);
printf("----------------------------\n");
printf("Start timer a third time\n");
timer_settime(timer, 0, &timer_period, NULL);
printf("Cancel timer\n");
timer_delete(timer);
sleep(2);
printf("The timer expiration handler function should not have been called\n");
return EXIT_SUCCESS;
}