alarm信号、select、RTC定时器

#include <unistd.h>
#include <signal.h>

void car_handler(int signo)
{
    if(SIGALRM == signo)
            printf("Not enter the test\n");
}
//设置5秒停止录音
signal(SIGALRM, car_handler); 
alarm(5);//设置定时5s
原型:
int select(int nfds, fd_set *readfds, fd_set *writefds,
                  fd_set *exceptfds, struct timeval *timeout);
例一:

timeval的结构如下:
struct timeval{
    long tv_sec;/*secons*
    long tv_usec;/*microseconds*/
}

一、秒级定时器
复制代码 代码如下:

void seconds_sleep(unsigned seconds){
    struct timeval tv;
    tv.tv_sec=seconds;
    tv.tv_usec=0;
    int err;
    do{
       err=select(0,NULL,NULL,NULL,&tv);
    }while(err<0 && errno==EINTR);
}

二、毫秒级别定时器
代码如下:

void milliseconds_sleep(unsigned long mSec){
    struct timeval tv;
    tv.tv_sec=mSec/1000;
    tv.tv_usec=(mSec%1000)*1000;
    int err;
    do{
       err=select(0,NULL,NULL,NULL,&tv);
    }while(err<0 && errno==EINTR);
}

三、微妙级别定时器
代码如下:

void microseconds_sleep(unsigned long uSec){
    struct timeval tv;
    tv.tv_sec=uSec/1000000;
    tv.tv_usec=uSec%1000000;
    int err;
    do{
        err=select(0,NULL,NULL,NULL,&tv);
    }while(err<0 && errno==EINTR);
}


四、test:

#include <stdio.h>
#include <sys/time.h>
#include <errno.h>
int main()
{
    int i;
    for(i=0;i<5;++i){
    printf("%d\n",i);
    //seconds_sleep(1);
    //milliseconds_sleep(1500);
    microseconds_sleep(1900000);
    }
}
例二:

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

    /*seconds: the seconds; mseconds: the micro seconds*/  
    void setTimer(int seconds, int mseconds)  
    {  
            struct timeval temp;  

            temp.tv_sec = seconds;  
            temp.tv_usec = mseconds;  

            select(0, NULL, NULL, NULL, &temp);  
            printf("timer\n");  

            return ;  
    }  

    int main()  
    {  
            int i;  

            for(i = 0 ; i < 100; i++)  
                    setTimer(1, 0);  

            return 0;  
    }  

RTC:


    #include <stdio.h>  
    #include <linux/rtc.h>  
    #include <sys/ioctl.h>  
    #include <sys/time.h>  
    #include <sys/types.h>  
    #include <fcntl.h>  
    #include <unistd.h>  
    #include <errno.h>  
    #include <stdlib.h>  

    int main(int argc, char* argv[])  
    {  
            unsigned long i = 0;  
            unsigned long data = 0;  
            int retval = 0;  
            int fd = open ("/dev/rtc", O_RDONLY);  

            if(fd < 0)  
            {  
                    perror("open");  
                    exit(errno);  
            }  

            /*Set the freq as 4Hz*/  
            if(ioctl(fd, RTC_IRQP_SET, 1) < 0)  
            {  
                    perror("ioctl(RTC_IRQP_SET)");  
                    close(fd);  
                    exit(errno);  
            }  
            /* Enable periodic interrupts */  
            if(ioctl(fd, RTC_PIE_ON, 0) < 0)  
            {  
                    perror("ioctl(RTC_PIE_ON)");  
                    close(fd);  
                    exit(errno);  
            }  

            for(i = 0; i < 100; i++)  
            {  
                    if(read(fd, &data, sizeof(unsigned long)) < 0)  
                    {  
                            perror("read");  
                            close(fd);  
                            exit(errno);  

                    }  
                    printf("timer\n");  
            }  
            /* Disable periodic interrupts */  
            ioctl(fd, RTC_PIE_OFF, 0);  
            close(fd);  

            return 0;  
    }  



通过RTC获取当前时间:

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <fcntl.h>

#include <linux/rtc.h>

#include <sys/time.h>

#include <sys/ioctl.h>



int main(){

       intfd;

       structrtc_time rtc;



       fd= open("/dev/rtc0", O_RDONLY);



       if(!fd){

              perror("Opening rtc0");

              exit(EXIT_FAILURE);

       }



       ioctl(fd,RTC_RD_TIME,&rtc);

       printf("\n\nCurrentRTC data/time is %d-%d-%d, %02d:%02d:%02d.\n", rtc.tm_mday, rtc.tm_mon +1,rtc.tm_year + 1900, rtc.tm_hour, rtc.tm_min, rtc.tm_sec);      



       close(fd);

       exit(EXIT_SUCCESS);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值