Back to index

glibc  2.9
clock.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1996, 1999 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <sys/times.h>
00020 #include <time.h>
00021 #include <unistd.h>
00022 
00023 /* Return the time used by the program so far (user time + system time).  */
00024 clock_t
00025 clock (void)
00026 {
00027   struct tms buf;
00028   long clk_tck = __sysconf (_SC_CLK_TCK);
00029 
00030   /* We don't check for errors here.  The only error the kernel
00031      returns is EFAULT if the value cannot be written to the struct we
00032      pass a pointer to.  Otherwise the kernel returns an `unsigned
00033      long' value which is the number of jiffies since system start.
00034      But this number can be negative (when read as `long') when the
00035      system is up for some time.  Ignoring errors should therefore
00036      have no negative impacts but solve the problem.  */
00037   __times (&buf);
00038 
00039   return
00040     (clk_tck <= CLOCKS_PER_SEC)
00041     ? ((unsigned long) buf.tms_utime + buf.tms_stime) * (CLOCKS_PER_SEC
00042                                                   / clk_tck)
00043     : ((unsigned long) buf.tms_utime + buf.tms_stime) / (clk_tck
00044                                                   / CLOCKS_PER_SEC);
00045 }