#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();
}

本文介绍了一个简单的C程序,该程序通过读取/proc/uptime文件来获取系统的运行时间,并将其转换为秒数进行显示。这种方法常用于监控系统运行状态。
2147

被折叠的 条评论
为什么被折叠?



