Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15498770
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2008-09-12 13:55:07

源码time﹑localtime﹑mktime﹑asctime﹑ctime﹑difftime﹑alarm和setitimer

#define TIM_MOD 4
#if (TIM_MOD==0)
#include <time.h>
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
    time_t start,end;
    start = time(NULL);
    system("ps aux");
    sleep(1);
    end = time(NULL);
    printf("The 'ps aux' used %f seconds.\n",difftime(end,start));
    return 0;
}
#endif

#if (TIM_MOD==1)
#include <time.h>
#include <stdio.h>
/*
struct tm
{
     int tm_sec;
     int tm_min;
     int tm_hour;
     int tm_mday;
     int tm_mon;
     int tm_year;
     int tm_wday;
     int tm_yday;
     int tm_isdst;
};
 asctime()函数和ctime()函数将时间以固定的格式显示出来
 */

int main(void)
{
    struct tm *local;
    time_t t;
    t = time(NULL);
    local=localtime(&t);
    printf("asctime(local) = %s", asctime(local));
    printf("ctime(&t) = %s", ctime(&t));
    local=gmtime(&t);
    printf("asctime(local) = %s", asctime(local));
    printf("ctime(&t) = %s", ctime(&t));
    return 0;
}
#endif

#if (TIM_MOD==2)
#include <time.h>
#include <stdio.h>
int main(void)
{
    struct tm t;
    time_t t_of_day;
    t.tm_year=2008-1900;
    t.tm_mon=9;
    t.tm_mday=11;
    t.tm_hour=13;
    t.tm_min=38;
    t.tm_sec=28;
    t.tm_isdst=0;
    t_of_day=mktime(&t);
    printf(ctime(&t_of_day));
    return 0;
}
#endif

#if (TIM_MOD==3)
/*
 * sleep和usleep用于延时,或者
 * int select(0,NULL,NULL,NULL,struct timeval *tv);
 * */

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int alarm_count;
void sigalrm_fn(int sig)
{
    /* Do something */
    printf("alarm=%d\n", alarm_count++);
    alarm(2);
    return;
}
 
int main(void)
{
    signal(SIGALRM, sigalrm_fn);
    alarm(2);

    /* Do someting */
    while(1) pause();
}
#endif


#if (TIM_MOD==4)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
 
int sec;
void sigroutine(int signo){
 
     switch (signo){
     case SIGALRM:
         printf("Catch a signal -- SIGALRM \n");
         signal(SIGALRM, sigroutine);
         break;
     case SIGVTALRM:
         printf("Catch a signal -- SIGVTALRM \n");
         signal(SIGVTALRM, sigroutine);
         break;
     }
     return;
}
 
int main()
{
     struct itimerval value, ovalue, value2;
   
     sec = 5;
     printf("process id is %d ", getpid());
     signal(SIGALRM, sigroutine);
     signal(SIGVTALRM, sigroutine);
     value.it_value.tv_sec = 1;
     value.it_value.tv_usec = 0;
     value.it_interval.tv_sec = 1;
     value.it_interval.tv_usec = 0;
     setitimer(ITIMER_REAL, &value, &ovalue);
 
     value2.it_value.tv_sec = 0;
     value2.it_value.tv_usec = 500000;
     value2.it_interval.tv_sec = 0;
     value2.it_interval.tv_usec = 500000;
     setitimer(ITIMER_VIRTUAL, &value2, &ovalue);
     for(;;)pause();
}
#endif
//以上代码摘自《》

阅读(2547) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~