Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-25-2004, 01:58 AM   #1
D M Mehta
LQ Newbie
Registered: Feb 2004
Posts: 1

Rep: Reputation: 0
Question How to get System UpTime using C program ?

How do we get the system up time using C programming in Linux ??
i.e how to get the time when the system started using C Program.??
Old 02-25-2004, 02:47 AM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,047
Blog Entries: 11

Rep: Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907Reputation: 907
This is how GNU's coreutils
uptime does it

  fp = fopen ("/proc/uptime", "r");
  if (fp != NULL)
      char buf[BUFSIZ];
      int res;
      char *b = fgets (buf, BUFSIZ, fp);
      if (b == buf)
          /* The following sscanf must use the C locale.  */
          setlocale (LC_NUMERIC, "C");
          res = sscanf (buf, "%lf", &upsecs);
          setlocale (LC_NUMERIC, "");
          if (res == 1)
            uptime = (time_t) upsecs;

      fclose (fp);



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
'Uptime' system load averages miknight Linux - General 14 01-30-2008 03:28 PM
is there any fun() available in linux to find system uptime without using sysinfo uppala MEPIS 2 06-15-2005 10:45 AM
uptime shows 3 users on system when there is only me. Is this normal? MichaelD Linux - Security 3 05-22-2005 12:05 AM
need help with system function in C program veereshai Programming 6 10-26-2004 04:20 PM
uptime system call ?? Agent007 Programming 4 01-10-2004 05:31 AM

All times are GMT -5. The time now is 07:13 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration