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 the underlying C API I suspect exists. Anyone know if there is such a call, or does uptime(1) simply process records obtained from wtmp?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
There's also clock_gettime (probably needs
-lrt
). The behavior I've seen (I'm not going to claim it's guaranteed) but givenCLOCK_MONOTONIC
as theclk_id
is that it returns the system uptime in the givenstruct timespec *
parameter.Read the file
/proc/uptime
and take the first decimal number as the uptime in seconds.From
man 5 proc
:The system call you're looking for is sysinfo().
It's defined in sys/sysinfo.h
Its signature is: int sysinfo(struct sysinfo *info)
Since kernel 2.4, the structure has looked like this:
Have fun!
That would be something like this.
See "man sysinfo" for more info.