Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助在Linux中应用C中的计时器吗_C_Linux - Fatal编程技术网

需要帮助在Linux中应用C中的计时器吗

需要帮助在Linux中应用C中的计时器吗,c,linux,C,Linux,我想在我们的C程序中创建一个计时器,这样它就可以每1秒打印一次变量 有谁能帮我做这件事吗?如果你只需要第二精度。使用time(0),如果包含time.h,则返回当前时间 更新: 添加在20秒内每秒打印10的简单示例: #include <time.h> #include <stdio.h> int main() { int a = 10; int num = 20; int c = time(0); while(n--) {

我想在我们的C程序中创建一个计时器,这样它就可以每1秒打印一次变量


有谁能帮我做这件事吗?

如果你只需要第二精度。使用
time(0)
,如果包含
time.h
,则返回当前时间

更新: 添加在
20
秒内每秒打印
10
的简单示例:

#include <time.h>
#include <stdio.h>

int main()
{
    int a = 10;
    int num = 20;
    int c = time(0);
    while(n--)
    {
        printf("%d\n", a);
        while(!(time(0) - c));
        c = time(0);
    }
    return 0;
}
#包括
#包括
int main()
{
INTA=10;
int num=20;
int c=时间(0);
而(n--)
{
printf(“%d\n”,a);
而(!(时间(0)-c));
c=时间(0);
}
返回0;
}

如果您只需要第二精度。使用
time(0)
,如果包含
time.h
,则返回当前时间

更新: 添加在
20
秒内每秒打印
10
的简单示例:

#include <time.h>
#include <stdio.h>

int main()
{
    int a = 10;
    int num = 20;
    int c = time(0);
    while(n--)
    {
        printf("%d\n", a);
        while(!(time(0) - c));
        c = time(0);
    }
    return 0;
}
#包括
#包括
int main()
{
INTA=10;
int num=20;
int c=时间(0);
而(n--)
{
printf(“%d\n”,a);
而(!(时间(0)-c));
c=时间(0);
}
返回0;
}
使用
时间(0)
参见此示例

/* timer.c */ 
#include <stdio.h> 
#include <time.h> 

void delay_sec( int seconds ){ 
    clock_t endwait; 
    endwait = clock () + seconds * CLOCKS_PER_SEC; 
    while (clock() < endwait) {} 
} 

int main (void){ 
    time_t rawtime, ini_time, now; 
    struct tm *ptm; 

    time ( &ini_time ); 
    for(;;){ 
        time ( &rawtime ); 
        //ptm = gmtime ( &rawtime ); 
        //printf ("%2d:%02d:%02d\n", ptm_2->tm_hour, ptm_2->tm_min, ptm_2->tm_sec); 
        now = rawtime - ini_time; 
        ptm = gmtime ( &now ); 
        printf ("%2d:%02d:%02d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); 
        delay_sec(1); 
    } 
    return 0; 
}  
/*timer.c*/
#包括
#包括
无效延迟秒(整数秒){
时钟等待;
endwait=时钟()+秒*每秒时钟数;
while(clock()tm_小时,ptm_2->tm_分钟,ptm_2->tm_秒);
现在=原始时间-初始时间;
ptm=gmtime(&now);
printf(“%2d:%02d:%02d\n”,ptm->tm_小时,ptm->tm_分钟,ptm->tm_秒);
延迟秒(1);
} 
返回0;
}  
使用
时间(0)
参见此示例

/* timer.c */ 
#include <stdio.h> 
#include <time.h> 

void delay_sec( int seconds ){ 
    clock_t endwait; 
    endwait = clock () + seconds * CLOCKS_PER_SEC; 
    while (clock() < endwait) {} 
} 

int main (void){ 
    time_t rawtime, ini_time, now; 
    struct tm *ptm; 

    time ( &ini_time ); 
    for(;;){ 
        time ( &rawtime ); 
        //ptm = gmtime ( &rawtime ); 
        //printf ("%2d:%02d:%02d\n", ptm_2->tm_hour, ptm_2->tm_min, ptm_2->tm_sec); 
        now = rawtime - ini_time; 
        ptm = gmtime ( &now ); 
        printf ("%2d:%02d:%02d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); 
        delay_sec(1); 
    } 
    return 0; 
}  
/*timer.c*/
#包括
#包括
无效延迟秒(整数秒){
时钟等待;
endwait=时钟()+秒*每秒时钟数;
while(clock()tm_小时,ptm_2->tm_分钟,ptm_2->tm_秒);
现在=原始时间-初始时间;
ptm=gmtime(&now);
printf(“%2d:%02d:%02d\n”,ptm->tm_小时,ptm->tm_分钟,ptm->tm_秒);
延迟秒(1);
} 
返回0;
}  

我相信你知道
1000毫秒
等于
1秒

#include <stdio.h>  
#include <time.h>
#define mydelay 1000

void delay(int mseconds)
{
    clock_t wait = mseconds + clock();
    while (wait > clock());
}

int main()  
{  
   int i=100;
   while(1)
   {
           printf("%d\n",i);
           delay(mydelay);
   }              
   return 0;  
}
#包括
#包括
#定义mydelay 1000
无效延迟(整数毫秒)
{
时钟等待=毫秒+时钟();
while(wait>clock());
}
int main()
{  
int i=100;
而(1)
{
printf(“%d\n”,i);
延迟(mydelay);
}              
返回0;
}

我相信你知道
1000毫秒
等于
1秒

#include <stdio.h>  
#include <time.h>
#define mydelay 1000

void delay(int mseconds)
{
    clock_t wait = mseconds + clock();
    while (wait > clock());
}

int main()  
{  
   int i=100;
   while(1)
   {
           printf("%d\n",i);
           delay(mydelay);
   }              
   return 0;  
}
#包括
#包括
#定义mydelay 1000
无效延迟(整数毫秒)
{
时钟等待=毫秒+时钟();
while(wait>clock());
}
int main()
{  
int i=100;
而(1)
{
printf(“%d\n”,i);
延迟(mydelay);
}              
返回0;
}
如果您感兴趣的所有操作都是以1秒的间隔打印变量的值,使用或按照其他答案中的建议就足够了。一般来说,我不推荐这些技术


如果您的程序更复杂,我建议您研究使用或函数以1秒的间隔异步向应用程序发送信号

下面的示例使用无限期阻塞,以最小化与忙等待技术相关的CPU使用。阻塞
select()
调用被中断,并在捕获信号时返回。在
SIGALRM
信号的情况下,设置
print_variable
标志,并打印
variable
的值

示例1:使用
报警()

如果您感兴趣的是以1秒的间隔打印变量的值,那么使用或按照其他答案中的建议就足够了。一般来说,我不推荐这些技术


如果您的程序更复杂,我建议您研究使用或函数以1秒的间隔异步向应用程序发送信号

下面的示例使用无限期阻塞,以最小化与忙等待技术相关的CPU使用。阻塞
select()
调用被中断,并在捕获信号时返回。在
SIGALRM
信号的情况下,设置
print_variable
标志,并打印
variable
的值

示例1:使用
报警()


不要使用忙等待,因为您的CPU利用率为100%。 您必须使用将进程转换为睡眠模式的系统功能,例如
select()

#包括
#包括
作废您的_回调()
{
printf(“%s\n”,函数);
}
int main()
{
结构时间值t;
而(1){
t、 tv_sec=1;
t、 tv_usec=0;
选择(0,NULL,NULL,NULL,&t);
你的_回调();
}
返回0;
}

不要使用忙等待,因为您的CPU利用率为100%。 您必须使用将进程转换为睡眠模式的系统功能,例如
select()

#包括
#包括
作废您的_回调()
{
printf(“%s\n”,函数);
}
int main()
{
结构时间值t;
而(1){
t、 tv_sec=1;
t、 tv_usec=0;
选择(0,NULL,NULL,NULL,&t);
你的_回调();
}
返回0;
}

打印变量值的简单示例