获取ubuntu PC开机多久时间小程序

来源:互联网 发布:淘宝怎么联系人工客服 编辑:程序博客网 时间:2024/03/29 01:01

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <linux/kernel.h>

static unsigned long print_uptime(void)

{
    int fd = -1;
    int size = 0;
    int i = 0;
    unsigned long second = 0;
    char tmpBuf[100];
    char buffer[100];
    fd = open("/proc/uptime", O_RDONLY);
    if(-1 == fd)
    {
        return -1;
    }
    size = read(fd, tmpBuf, sizeof(tmpBuf));
    close(fd);
    while(tmpBuf[i] != '.')
    {
        buffer[i] = tmpBuf[i];
        i++;
    }
    buffer[i] = '\0';
    second = atol(buffer);
    printf("Machine run time seconds : %ld \n", second);
    return second;

}

void main(void)

{

print_uptime();

}