How to determine if there is a POSIX. works within 5 minutes after system boot?
From a C ++ app. compiled for AIX, HP-UX, Linux, OSX and Solaris, there is an easy way to determine if an application is. works within 5 minutes after system boot?
On Windows, I can do this:
// return true if OS has recently booted up
bool at_os_boot_time()
{
/* GetTickCount() returns the number of miliseconds since system start.
So "...the time will wrap around to zero if the system is run
continuously for 49.7 days" - so this function will erroneously
return true for a 5 minute period 49.7 days after boot */
return ::GetTickCount() < 5 * 60 * 1000;
}
I cannot find an equivalent in the Unix world.
source to share
this is from kernel.h on linux:
struct sysinfo;
extern int do_sysinfo(struct sysinfo *info);
#endif /* __KERNEL__ */
#define SI_LOAD_SHIFT 16
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 short pad; /* explicit padding for m68k */
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: libc5 uses this.. */
};
source to share
The most portable method of determining the time of the last boot of the system is to use getutxid()
with ut_type
of BOOT_TIME
to get it using the system credentials. I think all the systems you mentioned should support this, although it might not be particularly effective. You can also invoke a command line program such uptime
as who
-b
or w
.
Some systems support a more efficient interface for obtaining this information, but it will be OS specific. Mac OS X (and FreeBSD) has . HP-UX has . Tru64 has TBL_SYSINFO parameter. As mentioned in another Linux answer , but you can also read the time since boot from (this is the first number).sysctl()
CTL_KERN
KERN_BOOTTIME
pstat_getstatic()
table()
sysinfo()
/proc/uptime
source to share