2011-04-22 64 views

回答

1

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

更新: 这期间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; 
} 
+0

不,我想在每1秒后打印一个变量。所以我们的程序如何知道1秒钟过去了,以便它可以打印变量。 – piyush 2011-04-22 12:07:55

+0

这里'时间(0) - c'将是0(假),而目前的秒数正在进行。 您也可以在Windows中使用'Sleep()'和unix中的'sleep()'来在指定的时间内使您的程序休眠。 – 2011-04-22 12:12:34

0

使用time(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; 
} 
0

我相信你知道1000 Milliseconds等于1 Second

#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; 
} 
+0

实际上,在我们的程序中,我创建了100个线程,因此需要花费时间,因此我想在每秒钟后打印变量而不应用等待我们的睡眠。 – piyush 2011-04-22 12:14:02

+0

向我们展示一些代码。 – Sadique 2011-04-22 12:14:41

3

如果所有你有兴趣做的是打印变量的值在间隔一秒,使用time(2)clock(3)在其他的答案可能就足够了建议。一般来说,我不会推荐这些技术。


如果你的程序比较复杂,我建议你调查使用alarm(2)settimer(2)功能在一秒钟的时间间隔异步传递一个信号,你的应用程序。

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

实施例1:使用alarm()

#include <signal.h> 
#include <stdio.h> 
#include <sys/select.h> 
#include <unistd.h> 

volatile unsigned int variable = 0; 
volatile unsigned int print_variable = 0; 

void alarm_handler(int signum) 
{ 
    variable++; 
    print_variable = 1; 
    alarm(1); 
} 

int main() 
{   
    signal(SIGALRM, alarm_handler); 
    alarm(1); 

    for (;;) 
    { 
     select(0, NULL, NULL, NULL, NULL); 

     if (print_variable) 
     { 
      printf("Variable = %u\n", variable); 
     } 
    } 
} 

注:错误检查从为了简化上述代码删去。

一个printf()功能可能被称为SIGALRM处理程序中,但在信号处理程序调用非重入函数一般不提倡。


一秒的超时也可以被传递到select(),但如果它是由任何信号中断,附加的逻辑是必要的,以确保所述一个第二超时的剩余部分被兑现。幸运的是,在Linux上,select()修改了超时值以反映未睡眠的时间量。这允许检测到中断情况,然后通过后续呼叫select()完成超时。

例2:使用select()

#include <errno.h> 
#include <stdio.h> 
#include <sys/select.h> 

volatile unsigned int variable = 0; 

int main() 
{ 
    struct timeval tv; 
    int val; 

    for (;;) 
    { 
     tv.tv_sec = 1; 
     tv.tv_usec = 0; 

     do 
     { 
      val = select(0, NULL, NULL, NULL, &tv); 
     } while (val != 0 && errno == EINTR); 

     printf("Variable = %u\n", ++variable); 
    } 
} 
4

不要使用忙等待,因为你已经有了100%的CPU使用率。 必须使用系统功能,其变成处理进入睡眠模式,例如select()

#include <stdio.h> 
#include <sys/select.h> 

void your_callback() 
{ 
    printf("%s\n", __FUNCTION__); 
} 

int main() 
{ 
    struct timeval t; 

    while (1) { 
     t.tv_sec = 1; 
     t.tv_usec = 0; 

     select(0, NULL, NULL, NULL, &t); 

     your_callback(); 
    } 

    return 0; 
} 
0

一个简单的例子,其打印变量a的值对每1秒:

#include<stdio.h> 

void main(void) 
{ 
    int a = 10; 
    while(a--) 
    { 
    printf("Value of a = %d\n", a); 
    sleep(1); 
    } 

} 

输出:

a = 9的值 ...
a = 0的值