System time

How can i retrieve the system time that is elapsed since Linux was started?

Thanks!!
linux_newbAsked:
Who is Participating?
 
scnCommented:
man 2 times
times( ) returns the number of jiffies elapsed since system was started.
Seconds = jiffies / HZ  (HZ is defined in include "asm/param.h")
0
 
sunnycoderCommented:
uptime
0
 
linux_newbAuthor Commented:
uptime is a shell command . I want to use this value in my prog. Isn't there any function which can be used to assign this value to a variable inside my program?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
sunnycoderCommented:
you can invoke it using popen() and read in the string into a char array
0
 
S_KuMaaaaRCommented:
var=`uptime`

where var is ur program variable.

sanjeev
0
 
linux_newbAuthor Commented:
> you can invoke it using popen() and read in the string into a char array

i hope the format of info by uptime remains same for all versions of  redHat... else it would be difficult to extract the reqd part  from the string.
0
 
sunnycoderCommented:
>i hope the format of info by uptime remains same for all versions of  redHat
for redhat it should be the same (cant say definitely about <6) ... any case I feel that scn's method is far superior in terms of portability and ease

from the times man page

The function times returns the number of clock ticks that have elapsed since an arbitrary point in the past. For Linux this point is the moment the system was booted. This return value may overflow the possible range of type clock_t. On error, (clock_t) -1 is returned, and errno is set appropriately.

0
 
linux_newbAuthor Commented:
i meant i am relying on the fact that elapsed time immediately follows word 'up'
0
 
sunnycoderCommented:
>i meant i am relying on the fact that elapsed time immediately follows word 'up'
should work .... if you are concerned then it can also be obtained from the proc filesystem ...
cat /proc/uptime
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.