C: system call

This is a discussion on C: system call within the C Programming forums, part of the General Programming Boards category; Hi I'm studing the system call. I've written a small program that return the time spent in doing some operations. ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    56

    C: system call

    Hi

    I'm studing the system call. I've written a small program that return the time spent in doing some operations. Now I'd like to write one that return the time spent in user mode of a process.
    I'm reading that i should use the tms struct:

    Code:
    clock_t times(struct tms *buf);
    
    
    struct tms {
                   clock_t tms_utime;  /* user time */
                   clock_t tms_stime;  /* system time */
                   clock_t tms_cutime; /* user time of children */
                   clock_t tms_cstime; /* system time of children */
               };
    But It's not very clear. Surfing on the net I've found joust hard examples.

    Can anyone help with a small example that show how i can use the struct?

    Thanks

    D.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    reading the inline comments would help /* user time */

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    hi

    I'm trying to do something and looking around i've written this code:

    Code:
      
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(void){
    
    int i,j=0,max=100000;
    double usr_time, sys_time;
    clock_t tcstart, tcend;
    
    struct tms end,start;
    
    if ((tcstart=times(&start))==-1){
    	printf("Error to get start time\n");
    	return 1;
    
    }
    
    for(i=0;i<max;i++){
    j++;
    
    
    }
    
    if ((tcend=times(&end))==-1){
    	printf("Error to get final time\n");
    	return 1;
    }
    
    usr_time=end.tms_utime-start.tms_utime;
    sys_time=end.tms_stime-start.tms_stime;
    
    printf("%lf",usr_time);
    printf("%lf",sys_time);
    
    
    printf("\n");
    
    return 0;
    }
    but when i try to compile i get:
    error: storage size of ‘end’ isn’t known
    error: storage size of ‘start’ isn’t known
    I've included the time.h. anyone can help?

    thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    According to the man pages, those (*nix) functions/structs/etc. are in <sys/times.h>, not in the standard header <time.h>

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    you're right. but i've already tried and i get:

    error: ‘clock_t’ undeclared (first use in this function)
    error: (Each undeclared identifier is reported only once
    error: for each function it appears in.)
    error: expected ‘;’ before ‘tcstart’
    error: storage size of ‘end’ isn’t known
    error: storage size of ‘start’ isn’t known
    error: ‘tcstart’ undeclared (first use in this function)
    error: ‘tcend’ undeclared (first use in this function)
    Is there something i miss?

    Thanks for the ans

    D

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Sorry.
    I got it
    my mistake in typing time instead times

    thanks

    D

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Dedalus View Post
    you're right. but i've already tried and i get:



    Is there something i miss?

    Thanks for the ans

    D
    The standard clock_t is in time.h, yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 01:54 PM
  4. nanosleep() -system call does some confusing things
    By jtk in forum Linux Programming
    Replies: 5
    Last Post: 08-30-2007, 04:15 AM
  5. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21