What API do I call to get the system uptime?

后端 未结 5 1078
面向向阳花
面向向阳花 2020-12-02 20:22

I would like to get the system uptime from within a C application running on a linux-based system. I don\'t want to call uptime(1) and parse the output, I\'d like to call t

5条回答
  •  温柔的废话
    2020-12-02 21:01

    That would be something like this.

    #include 
    #include 
    #include        /* for _syscallX macros/related stuff */
    #include        /* for struct sysinfo */
    #include 
    
    long get_uptime()
    {
        struct sysinfo s_info;
        int error = sysinfo(&s_info);
        if(error != 0)
        {
            printf("code error = %d\n", error);
        }
        return s_info.uptime;
    }
    

    See "man sysinfo" for more info.

提交回复
热议问题