LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-09-2004, 02:42 PM   #1
Agent007
LQ Newbie
 
Registered: Oct 2003
Posts: 28

Rep: Reputation: 15
uptime system call ??


How to find the uptime value from a linux server using a C program

NOTE: without using shell commands or reading proc files, I just want a pure system call
 
Old 01-09-2004, 02:49 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Check out the manpage on sysinfo. It gets you all kinds of information.

And try out this code:
Code:
#include <sys/sysinfo.h>

int main (void)
{
    struct sysinfo *info;

    sysinfo(info);
    printf("Uptime = %d\n",info->uptime);
    return 0;
}
info->uptime is in seconds so you will have to do some math to figure out hours, minutes, days, ect.

Last edited by jtshaw; 01-09-2004 at 02:56 PM.
 
Old 01-09-2004, 09:05 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Quote:

#include <sys/sysinfo.h>

int main (void)
{
struct sysinfo *info;

sysinfo(info);
printf("Uptime = %d\n",info->uptime);
return 0;
}
This would undoubtedly segfault. You have to declare a sysinfo struct, not a pointer to one. So like this:

Code:
#include <sys/sysinfo.h>

int main (void)
{
    struct sysinfo info;

    sysinfo(&info);
    printf("Uptime = %d\n",info.uptime);
    return 0;
}
 
Old 01-09-2004, 09:11 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Ya, your right, I wrote a program that worked but forgot an important line when I re-typed it here... the malloc line.
 
Old 01-10-2004, 05:31 AM   #5
Agent007
LQ Newbie
 
Registered: Oct 2003
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks guys, you are the best
 
  


Reply


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
Is it possible to use system() and get the return value from the system call newguy21 Programming 1 08-11-2004 02:37 PM
How to get System UpTime using C program ? D M Mehta Programming 1 02-25-2004 02:47 AM


All times are GMT -5. The time now is 01:48 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration