LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-25-2004, 12: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, 01:47 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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

Code:
  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);
    }
:)


Cheers,
Tink
 
  


Reply



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 02:28 PM
is there any fun() available in linux to find system uptime without using sysinfo uppala MEPIS 2 06-15-2005 09:45 AM
uptime shows 3 users on system when there is only me. Is this normal? MichaelD Linux - Security 3 05-21-2005 11:05 PM
need help with system function in C program veereshai Programming 6 10-26-2004 03:20 PM
uptime system call ?? Agent007 Programming 4 01-10-2004 04:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:55 AM.

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