LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-17-2007, 01:39 AM   #1
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Rep: Reputation: 30
time


hi all
i tried like this

long t;
struct tm *tp;
char str[80];
time(&t);
tp=localtime(&t);
strftime(str, 100, "%S", tp);
int munna1=atoi(str);

printf("******** third time:= %d \n",munna1);

it is printing system time(only seconds. i need time that is start from zero seconds(and ends with 60 seconds)

how can i do this

thank you in advance
 
Old 05-17-2007, 02:36 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Could you please clarify what you want? Do you want the number of seconds of elapsed time from the time the program started? The number of seconds of CPU time? The "seconds" part of the elapsed time from the time the program started, even though you may have gone over a minute of elapsed time? What?
 
Old 05-17-2007, 11:29 PM   #3
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
thank you for quick replay

Quote:
Originally Posted by wjevans_7d1@yahoo.co
Could you please clarify what you want? Do you want the number of seconds of elapsed time from the time the program started? The number of seconds of CPU time? The "seconds" part of the elapsed time from the time the program started, even though you may have gone over a minute of elapsed time? What?
hi
i wanna this,
the number of seconds of elapsed time from the time the program started.
not cpu time

can you help me
thank you in advance
 
Old 05-18-2007, 06:10 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
times() returns a struct that has several different time values, including wall time for a process.

See:
http://www.die.net/doc/linux/man/man2/times.2.html
 
Old 05-19-2007, 01:29 PM   #5
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Um, Jim, times() does not offer wall time for the process. The man page to which you point is written sufficiently awkwardly that I could see how someone could read that into it.

The following program which I whipped up right quick shows that times doesn't offer this. The program also offers a way to get wall time for a process.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>  /* note that these two have slightly different names */
#include <sys/times.h> /* note that these two have slightly different names */

int main(void)
{
  long           clock_ticks_per_second;

  clock_t        uptime;

  struct timeval time1;
  struct timeval time2;

  struct tms     times_value;

  if(gettimeofday(&time1,NULL)!=0)
  {
    fprintf(stderr,"first gettimeofday() failed\n");

    exit(1);
  }

  /* Put some non-compute seconds on the wall clock. */

  sleep(10);

  printf("=== for elapsed clock time, you do not want the following\n");

  clock_ticks_per_second=sysconf(_SC_CLK_TCK);
  printf("clock ticks per second=%ld\n",clock_ticks_per_second);

  uptime=times(&times_value);

  printf("system has been running for %lu seconds\n",
         uptime*clock_ticks_per_second
        );

  printf("user   level CPU time for this process    so far is %lu seconds\n",
         times_value.tms_utime*clock_ticks_per_second
        );

  printf("kernel level CPU time for this process    so far is %lu seconds\n",
         times_value.tms_utime*clock_ticks_per_second
        );

  printf("user   level CPU time for child processes so far is %lu seconds\n",
         times_value.tms_utime*clock_ticks_per_second
        );

  printf("kernel level CPU time for child processes so far is %lu seconds\n",
         times_value.tms_utime*clock_ticks_per_second
        );

  printf("of course, there are no child processes :)\n");

  printf("=== for elapsed clock time, you want the following\n");

  if(gettimeofday(&time2,NULL)!=0)
  {
    fprintf(stderr,"second gettimeofday() failed\n");

    exit(1);
  }

  if(time2.tv_usec<time1.tv_usec)
  {
    time2.tv_usec=time2.tv_usec-time1.tv_usec+1000000;
    time2.tv_sec =time2.tv_sec -time1.tv_sec -1;
  }
  else
  {
    time2.tv_usec=time2.tv_usec-time1.tv_usec;
    time2.tv_sec =time2.tv_sec -time1.tv_sec;
  }

  printf("wall clock elapsed time is %ld.%06ld seconds\n",
         time2.tv_sec,
         time2.tv_usec
        );

  return 0;

} /* main() */
The output I got from running it was this:

Code:
=== for elapsed clock time, you do not want the following
clock ticks per second=100
system has been running for 3249928008 seconds
user   level CPU time for this process    so far is 0 seconds
kernel level CPU time for this process    so far is 0 seconds
user   level CPU time for child processes so far is 0 seconds
kernel level CPU time for child processes so far is 0 seconds
of course, there are no child processes :)
=== for elapsed clock time, you want the following
wall clock elapsed time is 10.008889 seconds
Hope this helps.
 
Old 05-20-2007, 07:09 AM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
You're correct - I was thinking about the command line utility which calls times().
 
  


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
System time vs Hardware time and Daylight Savings Time Toadman Linux - General 6 03-17-2007 08:12 AM
System time vs Hardware time and Daylight Savings Time Toadman Linux - Networking 6 03-16-2007 07:14 PM
Where does RH8 daily set system time to hardware clock time smartnorman Red Hat 1 05-24-2006 02:42 PM
no signal when starting xorg for the 1st time (but the second time works fine) bungalowbill Linux - Software 0 06-04-2004 09:56 AM

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

All times are GMT -5. The time now is 11:04 PM.

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