我需要找到系统时间,因为电源是在我的c代码中的linux机器上使用的。像' time ()‘和gettimeofday()这样的函数返回的是自纪元以来的时间,而不是通电以来的时间。如何查找开机后的时间或时钟滴答数?
提前感谢!
发布于 2013-07-18 23:51:28
此信息是通过/proc
文件系统API提供的。具体地说,/proc/uptime
。在C中,只需将其作为普通文件打开,并从中读取一个浮点数。这将是开机后的秒数。如果您读取另一个fp值,它将是系统空闲的总秒数。不过,您只对第一个数字感兴趣。
示例:
float uptime;
FILE* proc_uptime_file = fopen("/proc/uptime", "r");
fscanf(proc_uptime_file, "%f", &uptime);
该值以秒为单位,浮点数。
您可以将其转换回时钟节拍:
uptime * sysconfig(_SC_CLK_TCK);
发布于 2013-07-18 23:50:34
请参阅:Wgat API do I call to get the System Time
请参阅中的sysinfo
sys/sysinfo.h
struct sysinfo {
long uptime; /* Seconds since boot */
unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
unsigned long totalram; /* Total usable main memory size */
unsigned long freeram; /* Available memory size */
unsigned long sharedram; /* Amount of shared memory */
unsigned long bufferram; /* Memory used by buffers */
unsigned long totalswap; /* Total swap space size */
unsigned long freeswap; /* swap space still available */
unsigned short procs; /* Number of current processes */
unsigned long totalhigh; /* Total high memory size */
unsigned long freehigh; /* Available high memory size */
unsigned int mem_unit; /* Memory unit size in bytes */
char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */
};
发布于 2013-07-18 23:49:58
如果你想知道C的API,
这个问题可能是What API do I call to get the system uptime?的重复
如果您想通过shell了解正常运行时间,请使用uptime命令。
$uptime
https://stackoverflow.com/questions/17728173
复制相似问题