Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6754257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:15:30+00:00 2026-05-26T13:15:30+00:00

I’m trying to implement the times() function in C programming. I’m using the struct

  • 0

I’m trying to implement the times() function in C programming.

I’m using the struct tms structure which consists of the fields: tms_utime, tms_cutime,
tms_stime and tms_cstime.

In order to implement the times() function in my program, I do:

  1. Before I fork and create a child, I call the times function (in the parent process).

    times(&start_tms);
    
  2. I create a pipe and I pass the times of start structure to the pipe when I’m in the child process.
  3. The child executes a simple ls -l command
  4. When the child finishes he execution, the father calls for the second time the times() function.

    times(&end_tms);
    

    Unfortunately, the times of end_tms are all zero! Weird, but I don’t know why.

Some things I don’t understand in my program are:

1) In the first printfs the times of the struct start are negative. Why is that?
2) When I run the program, why do I get zeros for times? What am i doing wrong?

My program is as follows:

Thanks, in advance


#include <sys/times.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main() {

printf("test\n");

int     fd[2]; //two pointers
int nbytes;
char    string[] = "Hello, world!\n";
char    readbuffer[80];
struct tms start_tms;
struct tms end_tms;
clock_t start, end;
double cpu_time_used;

pipe(fd);

//once we have established the pipeline we fork the child
pid_t   childpid;
pid_t pid_waitpid;

//NEW MODIFICATION!!! call times before fork()!!!
times(&start_tms);

//they return negative values, but why???
printf("Test start_tms.tms_utime = %f\n\n",start_tms.tms_utime);
printf("Test start_tms.tms_cutime = %f\n\n",start_tms.tms_cutime);
printf("Test start_tms.tms_stime = %f\n\n",start_tms.tms_stime);


if((childpid = fork()) == -1)
            {
                    perror("fork");
                    exit(1);
            }

if(childpid == 0)
            {

                    /* Child process closes up input side of pipe */
                     close(fd[0]);

                   /* call times function */ 
                   /*times(&start_tms);*/


                      //REMOVED!!!!
                    //write(fd[1], string, (strlen(string)+1));
                    write(fd[1], &start_tms.tms_cutime, sizeof(clock_t));
                    write(fd[1], &start_tms.tms_utime, sizeof(clock_t));
                    write(fd[1], &start_tms.tms_stime, sizeof(clock_t));

                     //execute /bin/ls
                    execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);

                    exit(0);


            }
else
            {
                    /* Parent process closes up output side of pipe */
                    close(fd[1]);

                    /* NEW MODIFICATION, wait for the child!!! */
                     if( (pid_waitpid  = waitpid(childpid,NULL,0) ) == -1)
                    {
                             perror("waitpid");
                             exit(1);
                     }



                    /* call times for capturing end times */
                    times(&end_tms);

                    /* define t1, t2, variables */
                    clock_t t1,t2,t3;


                     //REMOVED!!!!
                    //nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
                    read(fd[0], &t1, sizeof(clock_t));
                    read(fd[0], &t2, sizeof(clock_t));
                    read(fd[0], &t3, sizeof(clock_t));

                    printf("Received string: %s\n\n", readbuffer);
                    printf("Test t1 = %f\n\n",t1);
                    printf("Test end_tms.tms_utime = %f\n\n",end_tms.tms_utime);
                    printf("Test end_tms.tms_cutime = %f\n\n",end_tms.tms_cutime);
                    printf("Test end_tms.tms_stime = %f\n\n",end_tms.tms_stime);

                    /* Calculate times, unfortunately return zero, but why??? */
                    double cpu_time = end_tms.tms_cutime - t1;
                    double utime = end_tms.tms_utime - t2;
                    double stime = end_tms.tms_stime - t3;

                    //Unfortunately printfs return zero, but why???
                    printf("cpu time %f\n\n",cpu_time);
                    printf("cpu Utime %f\n\n",utime);
                    printf("cpu Stime %f\n\n",stime);


}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:15:31+00:00Added an answer on May 26, 2026 at 1:15 pm

    Your logic is very strange. The writes you do in the child simply copy data that is already available to the parent in start_tms, so your whole pipe read/write thing is unnecessary.

    Secondly, clock_t is not a floating point type, it’s an integral type. You can’t use %f to print it. Use %jd and intmax_t to be safe in the printfs.

    And you’re missing #include <sys/wait.h> for waitpid. So turn on your compiler warnings, and read them.

    Here’s a C99 version of your code that works here:

    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <inttypes.h>
    #include <sys/wait.h>
    #include <sys/times.h>
    #include <sys/types.h>
    
    int main() {
        struct tms start_tms;
        struct tms end_tms;
    
        //once we have established the pipeline we fork the child
        pid_t   childpid;
    
        times(&start_tms);
    
        printf("Test start_tms.tms_utime = %jd\n\n",  (intmax_t)start_tms.tms_utime);
        printf("Test start_tms.tms_cutime = %jd\n\n", (intmax_t)start_tms.tms_cutime);
        printf("Test start_tms.tms_stime = %jd\n\n",  (intmax_t)start_tms.tms_stime);
        printf("Test start_tms.tms_cstime = %jd\n\n",  (intmax_t)start_tms.tms_cstime);
    
    
        if((childpid = fork()) == -1)
        {
            perror("fork");
            exit(1);
        }
    
        if(childpid == 0)
        {
            //execute /bin/ls
            execl("/bin/ls", "/bin/ls", "-R", "-t", "-l", (char *) 0);
            exit(0);
        }
        else
        {
            /* Parent process */
    
            /* NEW MODIFICATION, wait for the child!!! */
            if (waitpid(childpid,NULL,0) == -1)
            {
                perror("waitpid");
                exit(1);
            }
    
            /* call times for capturing end times */
            times(&end_tms);
    
            printf("Test end_tms.tms_utime = %jd\n\n",end_tms.tms_utime);
            printf("Test end_tms.tms_cutime = %jd\n\n",end_tms.tms_cutime);
            printf("Test end_tms.tms_stime = %jd\n\n",end_tms.tms_stime);
            printf("Test end_tms.tms_cstime = %jd\n\n",end_tms.tms_cstime);
    
            /* Calculate times, unfortunately return zero, but why??? */
            clock_t cpu_time = end_tms.tms_cutime - start_tms.tms_cutime;
            clock_t utime = end_tms.tms_utime - start_tms.tms_utime;
            clock_t stime = end_tms.tms_stime - start_tms.tms_stime;
            clock_t cstime = end_tms.tms_cstime - start_tms.tms_cstime;
    
            //Unfortunately printfs return zero, but why???
            printf("cpu time %jd\n\n",  (intmax_t)cpu_time);
            printf("cpu Utime %jd\n\n", (intmax_t)utime);
            printf("cpu Stime %jd\n\n", (intmax_t)stime);
            printf("cpu CStime %jd\n\n", (intmax_t)cstime);
        }
    }
    

    If you don’t have intmax_t, check the size of clock_t on your implementation and find a standard integer type that matches it, and use the appropriate format string in your printf calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.