美文网首页
C语言每隔1秒读取文件示例

C语言每隔1秒读取文件示例

作者: jrglinux | 来源:发表于2021-10-28 10:00 被阅读0次

    每间隔一段时间读取/proc/sys/kernel/sched_elastic_debug文件里面的值,C语言例子

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        struct timespec reg, rem;
        FILE *fd;
        int debug;
        size_t st;
        int sec = 1;
        int nsec = 0;
        unsigned long long sleeptime = 0;
        reg.tv_sec = sec;
        reg.tv_nsec = nsec;
    
        while(1){
            fd = fopen("/proc/sys/kernel/sched_elastic_debug", "r");
            if(!fd){
                printf("open file failed\n");
                exit(1);
            }
            fscanf(fd, "%d", &debug);
            printf("sleeptime:%llu(ns), debug is: %d\n", sleeptime, debug);
            fclose(fd);
            if( -1 == nanosleep(&reg, &rem)){
                printf("error\n");
                exit(1);
            }
            else {
                sleeptime += (sec*1000000000 + nsec);
            }
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C语言每隔1秒读取文件示例

          本文链接:https://www.haomeiwen.com/subject/ogomaltx.html