The sysinfo system call can be used to retrieve current system up-time. The function get_uptime() in the following code snippet returns system uptime in seconds.

[neo@techpulp ~]$ cat uptime.c
#include <stdio.h>
#include <sys/sysinfo.h>

long get_uptime(void)
{
struct sysinfo sinfo;

sysinfo(&sinfo);
return (sinfo.uptime);
}

int main(int argc, char *argv[])
{
printf("System is up for %ld seconds\n", get_uptime());
return 0;
}

The following shows the ouput of the above C example program.

[neo@techpulp ~]# gcc uptime.c -o uptime
[neo@techpulp ~]# ./uptime
System is up for 374080 seconds
[neo@techpulp ~]#
[neo@techpulp ~]# ./uptime
System is up for 374085 seconds
[neo@techpulp ~]#