1 @node Date and Time, Non-Local Exits, Arithmetic, Top
4 This chapter describes functions for manipulating dates and times,
5 including functions for determining what the current time is and
6 conversion between different time representations.
8 The time functions fall into three main categories:
12 Functions for measuring elapsed CPU time are discussed in @ref{Processor
16 Functions for measuring absolute clock or calendar time are discussed in
20 Functions for setting alarms and timers are discussed in @ref{Setting
25 * Processor Time:: Measures processor time used by a program.
26 * Calendar Time:: Manipulation of ``real'' dates and times.
27 * Setting an Alarm:: Sending a signal after a specified time.
28 * Sleeping:: Waiting for a period of time.
29 * Resource Usage:: Measuring various resources used.
30 * Limits on Resources:: Specifying limits on resource usage.
31 * Priority:: Reading or setting process run priority.
35 @section Processor Time
37 If you're trying to optimize your program or measure its efficiency, it's
38 very useful to be able to know how much @dfn{processor time} or @dfn{CPU
39 time} it has used at any given point. Processor time is different from
40 actual wall clock time because it doesn't include any time spent waiting
41 for I/O or when some other process is running. Processor time is
42 represented by the data type @code{clock_t}, and is given as a number of
43 @dfn{clock ticks} relative to an arbitrary base time marking the beginning
44 of a single program invocation.
46 @cindex processor time
49 @cindex time, elapsed CPU
52 * Basic CPU Time:: The @code{clock} function.
53 * Detailed CPU Time:: The @code{times} function.
57 @subsection Basic CPU Time Inquiry
59 To get the elapsed CPU time used by a process, you can use the
60 @code{clock} function. This facility is declared in the header file
64 In typical usage, you call the @code{clock} function at the beginning and
65 end of the interval you want to time, subtract the values, and then divide
66 by @code{CLOCKS_PER_SEC} (the number of clock ticks per second), like this:
76 @dots{} /* @r{Do the work.} */
78 elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
82 Different computers and operating systems vary wildly in how they keep
83 track of processor time. It's common for the internal processor clock
84 to have a resolution somewhere between hundredths and millionths of a
87 In the GNU system, @code{clock_t} is equivalent to @code{long int} and
88 @code{CLOCKS_PER_SEC} is an integer value. But in other systems, both
89 @code{clock_t} and the type of the macro @code{CLOCKS_PER_SEC} can be
90 either integer or floating-point types. Casting processor time values
91 to @code{double}, as in the example above, makes sure that operations
92 such as arithmetic and printing work properly and consistently no matter
93 what the underlying representation is.
97 @deftypevr Macro int CLOCKS_PER_SEC
98 The value of this macro is the number of clock ticks per second measured
99 by the @code{clock} function.
104 @deftypevr Macro int CLK_TCK
105 This is an obsolete name for @code{CLOCKS_PER_SEC}.
110 @deftp {Data Type} clock_t
111 This is the type of the value returned by the @code{clock} function.
112 Values of type @code{clock_t} are in units of clock ticks.
117 @deftypefun clock_t clock (void)
118 This function returns the elapsed processor time. The base time is
119 arbitrary but doesn't change within a single process. If the processor
120 time is not available or cannot be represented, @code{clock} returns the
121 value @code{(clock_t)(-1)}.
125 @node Detailed CPU Time
126 @subsection Detailed Elapsed CPU Time Inquiry
128 The @code{times} function returns more detailed information about
129 elapsed processor time in a @w{@code{struct tms}} object. You should
130 include the header file @file{sys/times.h} to use this facility.
135 @deftp {Data Type} {struct tms}
136 The @code{tms} structure is used to return information about process
137 times. It contains at least the following members:
140 @item clock_t tms_utime
141 This is the CPU time used in executing the instructions of the calling
144 @item clock_t tms_stime
145 This is the CPU time used by the system on behalf of the calling process.
147 @item clock_t tms_cutime
148 This is the sum of the @code{tms_utime} values and the @code{tms_cutime}
149 values of all terminated child processes of the calling process, whose
150 status has been reported to the parent process by @code{wait} or
151 @code{waitpid}; see @ref{Process Completion}. In other words, it
152 represents the total CPU time used in executing the instructions of all
153 the terminated child processes of the calling process, excluding child
154 processes which have not yet been reported by @code{wait} or
157 @item clock_t tms_cstime
158 This is similar to @code{tms_cutime}, but represents the total CPU time
159 used by the system on behalf of all the terminated child processes of the
163 All of the times are given in clock ticks. These are absolute values; in a
164 newly created process, they are all zero. @xref{Creating a Process}.
169 @deftypefun clock_t times (struct tms *@var{buffer})
170 The @code{times} function stores the processor time information for
171 the calling process in @var{buffer}.
173 The return value is the same as the value of @code{clock()}: the elapsed
174 real time relative to an arbitrary base. The base is a constant within a
175 particular process, and typically represents the time since system
176 start-up. A value of @code{(clock_t)(-1)} is returned to indicate failure.
179 @strong{Portability Note:} The @code{clock} function described in
180 @ref{Basic CPU Time}, is specified by the ANSI C standard. The
181 @code{times} function is a feature of POSIX.1. In the GNU system, the
182 value returned by the @code{clock} function is equivalent to the sum of
183 the @code{tms_utime} and @code{tms_stime} fields returned by
187 @section Calendar Time
189 This section describes facilities for keeping track of dates and times
190 according to the Gregorian calendar.
191 @cindex Gregorian calendar
192 @cindex time, calendar
193 @cindex date and time
195 There are three representations for date and time information:
199 @dfn{Calendar time} (the @code{time_t} data type) is a compact
200 representation, typically giving the number of seconds elapsed since
201 some implementation-specific base time.
202 @cindex calendar time
205 There is also a @dfn{high-resolution time} representation (the @code{struct
206 timeval} data type) that includes fractions of a second. Use this time
207 representation instead of ordinary calendar time when you need greater
209 @cindex high-resolution time
212 @dfn{Local time} or @dfn{broken-down time} (the @code{struct
213 tm} data type) represents the date and time as a set of components
214 specifying the year, month, and so on, for a specific time zone.
215 This time representation is usually used in conjunction with formatting
216 date and time values.
218 @cindex broken-down time
222 * Simple Calendar Time:: Facilities for manipulating calendar time.
223 * High-Resolution Calendar:: A time representation with greater precision.
224 * Broken-down Time:: Facilities for manipulating local time.
225 * Formatting Date and Time:: Converting times to strings.
226 * TZ Variable:: How users specify the time zone.
227 * Time Zone Functions:: Functions to examine or specify the time zone.
228 * Time Functions Example:: An example program showing use of some of
232 @node Simple Calendar Time
233 @subsection Simple Calendar Time
235 This section describes the @code{time_t} data type for representing
236 calendar time, and the functions which operate on calendar time objects.
237 These facilities are declared in the header file @file{time.h}.
243 @deftp {Data Type} time_t
244 This is the data type used to represent calendar time.
245 When interpreted as an absolute time
246 value, it represents the number of seconds elapsed since 00:00:00 on
247 January 1, 1970, Coordinated Universal Time. (This date is sometimes
248 referred to as the @dfn{epoch}.) POSIX requires that this count
249 ignore leap seconds, but on some hosts this count includes leap seconds
250 if you set @code{TZ} to certain values (@pxref{TZ Variable}).
252 In the GNU C library, @code{time_t} is equivalent to @code{long int}.
253 In other systems, @code{time_t} might be either an integer or
259 @deftypefun double difftime (time_t @var{time1}, time_t @var{time0})
260 The @code{difftime} function returns the number of seconds elapsed
261 between time @var{time1} and time @var{time0}, as a value of type
262 @code{double}. The difference ignores leap seconds unless leap
263 second support is enabled.
265 In the GNU system, you can simply subtract @code{time_t} values. But on
266 other systems, the @code{time_t} data type might use some other encoding
267 where subtraction doesn't work directly.
272 @deftypefun time_t time (time_t *@var{result})
273 The @code{time} function returns the current time as a value of type
274 @code{time_t}. If the argument @var{result} is not a null pointer, the
275 time value is also stored in @code{*@var{result}}. If the calendar
276 time is not available, the value @w{@code{(time_t)(-1)}} is returned.
280 @node High-Resolution Calendar
281 @subsection High-Resolution Calendar
283 The @code{time_t} data type used to represent calendar times has a
284 resolution of only one second. Some applications need more precision.
286 So, the GNU C library also contains functions which are capable of
287 representing calendar times to a higher resolution than one second. The
288 functions and the associated data types described in this section are
289 declared in @file{sys/time.h}.
294 @deftp {Data Type} {struct timeval}
295 The @code{struct timeval} structure represents a calendar time. It
296 has the following members:
299 @item long int tv_sec
300 This represents the number of seconds since the epoch. It is equivalent
301 to a normal @code{time_t} value.
303 @item long int tv_usec
304 This is the fractional second value, represented as the number of
307 Some times struct timeval values are used for time intervals. Then the
308 @code{tv_sec} member is the number of seconds in the interval, and
309 @code{tv_usec} is the number of additional microseconds.
315 @deftp {Data Type} {struct timezone}
316 The @code{struct timezone} structure is used to hold minimal information
317 about the local time zone. It has the following members:
320 @item int tz_minuteswest
321 This is the number of minutes west of UTC.
324 If nonzero, daylight saving time applies during some part of the year.
327 The @code{struct timezone} type is obsolete and should never be used.
328 Instead, use the facilities described in @ref{Time Zone Functions}.
331 It is often necessary to subtract two values of type @w{@code{struct
332 timeval}}. Here is the best way to do this. It works even on some
333 peculiar operating systems where the @code{tv_sec} member has an
337 /* @r{Subtract the `struct timeval' values X and Y,}
338 @r{storing the result in RESULT.}
339 @r{Return 1 if the difference is negative, otherwise 0.} */
342 timeval_subtract (result, x, y)
343 struct timeval *result, *x, *y;
345 /* @r{Perform the carry for the later subtraction by updating @var{y}.} */
346 if (x->tv_usec < y->tv_usec) @{
347 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
348 y->tv_usec -= 1000000 * nsec;
351 if (x->tv_usec - y->tv_usec > 1000000) @{
352 int nsec = (y->tv_usec - x->tv_usec) / 1000000;
353 y->tv_usec += 1000000 * nsec;
357 /* @r{Compute the time remaining to wait.}
358 @r{@code{tv_usec} is certainly positive.} */
359 result->tv_sec = x->tv_sec - y->tv_sec;
360 result->tv_usec = x->tv_usec - y->tv_usec;
362 /* @r{Return 1 if result is negative.} */
363 return x->tv_sec < y->tv_sec;
369 @deftypefun int gettimeofday (struct timeval *@var{tp}, struct timezone *@var{tzp})
370 The @code{gettimeofday} function returns the current date and time in the
371 @code{struct timeval} structure indicated by @var{tp}. Information about the
372 time zone is returned in the structure pointed at @var{tzp}. If the @var{tzp}
373 argument is a null pointer, time zone information is ignored.
375 The return value is @code{0} on success and @code{-1} on failure. The
376 following @code{errno} error condition is defined for this function:
380 The operating system does not support getting time zone information, and
381 @var{tzp} is not a null pointer. The GNU operating system does not
382 support using @w{@code{struct timezone}} to represent time zone
383 information; that is an obsolete feature of 4.3 BSD.
384 Instead, use the facilities described in @ref{Time Zone Functions}.
390 @deftypefun int settimeofday (const struct timeval *@var{tp}, const struct timezone *@var{tzp})
391 The @code{settimeofday} function sets the current date and time
392 according to the arguments. As for @code{gettimeofday}, time zone
393 information is ignored if @var{tzp} is a null pointer.
395 You must be a privileged user in order to use @code{settimeofday}.
397 The return value is @code{0} on success and @code{-1} on failure. The
398 following @code{errno} error conditions are defined for this function:
402 This process cannot set the time because it is not privileged.
405 The operating system does not support setting time zone information, and
406 @var{tzp} is not a null pointer.
412 @deftypefun int adjtime (const struct timeval *@var{delta}, struct timeval *@var{olddelta})
413 This function speeds up or slows down the system clock in order to make
414 gradual adjustments in the current time. This ensures that the time
415 reported by the system clock is always monotonically increasing, which
416 might not happen if you simply set the current time.
418 The @var{delta} argument specifies a relative adjustment to be made to
419 the current time. If negative, the system clock is slowed down for a
420 while until it has lost this much time. If positive, the system clock
421 is speeded up for a while.
423 If the @var{olddelta} argument is not a null pointer, the @code{adjtime}
424 function returns information about any previous time adjustment that
425 has not yet completed.
427 This function is typically used to synchronize the clocks of computers
428 in a local network. You must be a privileged user to use it.
429 The return value is @code{0} on success and @code{-1} on failure. The
430 following @code{errno} error condition is defined for this function:
434 You do not have privilege to set the time.
438 @strong{Portability Note:} The @code{gettimeofday}, @code{settimeofday},
439 and @code{adjtime} functions are derived from BSD.
442 @node Broken-down Time
443 @subsection Broken-down Time
444 @cindex broken-down time
445 @cindex calendar time and broken-down time
447 Calendar time is represented as a number of seconds. This is convenient
448 for calculation, but has no resemblance to the way people normally
449 represent dates and times. By contrast, @dfn{broken-down time} is a binary
450 representation separated into year, month, day, and so on. Broken down
451 time values are not useful for calculations, but they are useful for
452 printing human readable time.
454 A broken-down time value is always relative to a choice of local time
455 zone, and it also indicates which time zone was used.
457 The symbols in this section are declared in the header file @file{time.h}.
461 @deftp {Data Type} {struct tm}
462 This is the data type used to represent a broken-down time. The structure
463 contains at least the following members, which can appear in any order:
467 This is the number of seconds after the minute, normally in the range
468 @code{0} through @code{59}. (The actual upper limit is @code{60}, to allow
469 for leap seconds if leap second support is available.)
473 This is the number of minutes after the hour, in the range @code{0} through
477 This is the number of hours past midnight, in the range @code{0} through
481 This is the day of the month, in the range @code{1} through @code{31}.
484 This is the number of months since January, in the range @code{0} through
488 This is the number of years since @code{1900}.
491 This is the number of days since Sunday, in the range @code{0} through
495 This is the number of days since January 1, in the range @code{0} through
499 @cindex Daylight Saving Time
501 This is a flag that indicates whether Daylight Saving Time is (or was, or
502 will be) in effect at the time described. The value is positive if
503 Daylight Saving Time is in effect, zero if it is not, and negative if the
504 information is not available.
506 @item long int tm_gmtoff
507 This field describes the time zone that was used to compute this
508 broken-down time value, including any adjustment for daylight saving; it
509 is the number of seconds that you must add to UTC to get local time.
510 You can also think of this as the number of seconds east of UTC. For
511 example, for U.S. Eastern Standard Time, the value is @code{-5*60*60}.
512 The @code{tm_gmtoff} field is derived from BSD and is a GNU library
513 extension; it is not visible in a strict ANSI C environment.
515 @item const char *tm_zone
516 This field is the name for the time zone that was used to compute this
517 broken-down time value. Like @code{tm_gmtoff}, this field is a BSD and
518 GNU extension, and is not visible in a strict ANSI C environment.
524 @deftypefun {struct tm *} localtime (const time_t *@var{time})
525 The @code{localtime} function converts the calendar time pointed to by
526 @var{time} to broken-down time representation, expressed relative to the
527 user's specified time zone.
529 The return value is a pointer to a static broken-down time structure, which
530 might be overwritten by subsequent calls to @code{ctime}, @code{gmtime},
531 or @code{localtime}. (But no other library function overwrites the contents
534 Calling @code{localtime} has one other effect: it sets the variable
535 @code{tzname} with information about the current time zone. @xref{Time
541 @deftypefun {struct tm *} gmtime (const time_t *@var{time})
542 This function is similar to @code{localtime}, except that the broken-down
543 time is expressed as Coordinated Universal Time (UTC)---that is, as
544 Greenwich Mean Time (GMT)---rather than relative to the local time zone.
546 Recall that calendar times are @emph{always} expressed in coordinated
552 @deftypefun time_t mktime (struct tm *@var{brokentime})
553 The @code{mktime} function is used to convert a broken-down time structure
554 to a calendar time representation. It also ``normalizes'' the contents of
555 the broken-down time structure, by filling in the day of week and day of
556 year based on the other date and time components.
558 The @code{mktime} function ignores the specified contents of the
559 @code{tm_wday} and @code{tm_yday} members of the broken-down time
560 structure. It uses the values of the other components to compute the
561 calendar time; it's permissible for these components to have
562 unnormalized values outside of their normal ranges. The last thing that
563 @code{mktime} does is adjust the components of the @var{brokentime}
564 structure (including the @code{tm_wday} and @code{tm_yday}).
566 If the specified broken-down time cannot be represented as a calendar time,
567 @code{mktime} returns a value of @code{(time_t)(-1)} and does not modify
568 the contents of @var{brokentime}.
570 Calling @code{mktime} also sets the variable @code{tzname} with
571 information about the current time zone. @xref{Time Zone Functions}.
574 @node Formatting Date and Time
575 @subsection Formatting Date and Time
577 The functions described in this section format time values as strings.
578 These functions are declared in the header file @file{time.h}.
583 @deftypefun {char *} asctime (const struct tm *@var{brokentime})
584 The @code{asctime} function converts the broken-down time value that
585 @var{brokentime} points to into a string in a standard format:
588 "Tue May 21 13:46:22 1991\n"
591 The abbreviations for the days of week are: @samp{Sun}, @samp{Mon},
592 @samp{Tue}, @samp{Wed}, @samp{Thu}, @samp{Fri}, and @samp{Sat}.
594 The abbreviations for the months are: @samp{Jan}, @samp{Feb},
595 @samp{Mar}, @samp{Apr}, @samp{May}, @samp{Jun}, @samp{Jul}, @samp{Aug},
596 @samp{Sep}, @samp{Oct}, @samp{Nov}, and @samp{Dec}.
598 The return value points to a statically allocated string, which might be
599 overwritten by subsequent calls to @code{asctime} or @code{ctime}.
600 (But no other library function overwrites the contents of this
606 @deftypefun {char *} ctime (const time_t *@var{time})
607 The @code{ctime} function is similar to @code{asctime}, except that the
608 time value is specified as a @code{time_t} calendar time value rather
609 than in broken-down local time format. It is equivalent to
612 asctime (localtime (@var{time}))
615 @code{ctime} sets the variable @code{tzname}, because @code{localtime}
616 does so. @xref{Time Zone Functions}.
622 @deftypefun size_t strftime (char *@var{s}, size_t @var{size}, const char *@var{template}, const struct tm *@var{brokentime})
623 This function is similar to the @code{sprintf} function (@pxref{Formatted
624 Input}), but the conversion specifications that can appear in the format
625 template @var{template} are specialized for printing components of the date
626 and time @var{brokentime} according to the locale currently specified for
627 time conversion (@pxref{Locales}).
629 Ordinary characters appearing in the @var{template} are copied to the
630 output string @var{s}; this can include multibyte character sequences.
631 Conversion specifiers are introduced by a @samp{%} character, followed
632 by an optional flag which can be one of the following. These flags,
633 which are GNU extensions, affect only the output of numbers:
637 The number is padded with spaces.
640 The number is not padded at all.
643 The default action is to pad the number with zeros to keep it a constant
644 width. Numbers that do not have a range indicated below are never
645 padded, since there is no natural width for them.
647 An optional modifier can follow the optional flag. The modifiers, which
648 are POSIX.2 extensions, are:
652 Use the locale's alternate representation for date and time. This
653 modifier applies to the @code{%c}, @code{%C}, @code{%x}, @code{%X},
654 @code{%y} and @code{%Y} format specifiers. In a Japanese locale, for
655 example, @code{%Ex} might yield a date format based on the Japanese
659 Use the locale's alternate numeric symbols for numbers. This modifier
660 applies only to numeric format specifiers.
663 A modifier is ignored if no alternate representation is available.
665 The conversion specifier ends with a format specifier taken from the
666 following list. The whole @samp{%} sequence is replaced in the output
671 The abbreviated weekday name according to the current locale.
674 The full weekday name according to the current locale.
677 The abbreviated month name according to the current locale.
680 The full month name according to the current locale.
683 The preferred date and time representation for the current locale.
686 The century of the year. This is equivalent to the greatest integer not
687 greater than the year divided by 100.
689 This format is a POSIX.2 extension.
692 The day of the month as a decimal number (range @code{01} through @code{31}).
695 The date using the format @code{%m/%d/%y}.
697 This format is a POSIX.2 extension.
700 The day of the month like with @code{%d}, but padded with blank (range
701 @code{ 1} through @code{31}).
703 This format is a POSIX.2 extension.
706 The year corresponding to the ISO week number, but without the century
707 (range @code{00} through @code{99}). This has the same format and value
708 as @code{%y}, except that if the ISO week number (see @code{%V}) belongs
709 to the previous or next year, that year is used instead.
711 This format is a GNU extension.
714 The year corresponding to the ISO week number. This has the same format
715 and value as @code{%Y}, except that if the ISO week number (see
716 @code{%V}) belongs to the previous or next year, that year is used
719 This format is a GNU extension.
722 The abbreviated month name according to the current locale. The action
723 is the same as for @code{%b}.
725 This format is a POSIX.2 extension.
728 The hour as a decimal number, using a 24-hour clock (range @code{00} through
732 The hour as a decimal number, using a 12-hour clock (range @code{01} through
736 The day of the year as a decimal number (range @code{001} through @code{366}).
739 The hour as a decimal number, using a 24-hour clock like @code{%H}, but
740 padded with blank (range @code{ 0} through @code{23}).
742 This format is a GNU extension.
745 The hour as a decimal number, using a 12-hour clock like @code{%I}, but
746 padded with blank (range @code{ 1} through @code{12}).
748 This format is a GNU extension.
751 The month as a decimal number (range @code{01} through @code{12}).
754 The minute as a decimal number (range @code{00} through @code{59}).
757 A single @samp{\n} (newline) character.
759 This format is a POSIX.2 extension.
762 Either @samp{AM} or @samp{PM}, according to the given time value; or the
763 corresponding strings for the current locale. Noon is treated as
764 @samp{PM} and midnight as @samp{AM}.
767 The complete time using the AM/PM format of the current locale.
769 This format is a POSIX.2 extension.
772 The hour and minute in decimal numbers using the format @code{%H:%M}.
774 This format is a GNU extension.
777 The number of seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
778 Leap seconds are not counted unless leap second support is available.
780 This format is a GNU extension.
783 The second as a decimal number (range @code{00} through @code{60}).
786 A single @samp{\t} (tabulator) character.
788 This format is a POSIX.2 extension.
791 The time using decimal numbers using the format @code{%H:%M:%S}.
793 This format is a POSIX.2 extension.
796 The day of the week as a decimal number (range @code{1} through
797 @code{7}), Monday being @code{1}.
799 This format is a POSIX.2 extension.
802 The week number of the current year as a decimal number (range @code{00}
803 through @code{53}), starting with the first Sunday as the first day of
804 the first week. Days preceding the first Sunday in the year are
805 considered to be in week @code{00}.
808 The @w{ISO 8601:1988} week number as a decimal number (range @code{01}
809 through @code{53}). ISO weeks start with Monday and end with Sunday.
810 Week @code{01} of a year is the first week which has the majority of its
811 days in that year; this is equivalent to the week containing the year's
812 first Thursday, and it is also equivalent to the week containing January
813 4. Week @code{01} of a year can contain days from the previous year.
814 The week before week @code{01} of a year is the last week (@code{52} or
815 @code{53}) of the previous year even if it contains days from the new
818 This format is a POSIX.2 extension.
821 The day of the week as a decimal number (range @code{0} through
822 @code{6}), Sunday being @code{0}.
825 The week number of the current year as a decimal number (range @code{00}
826 through @code{53}), starting with the first Monday as the first day of
827 the first week. All days preceding the first Monday in the year are
828 considered to be in week @code{00}.
831 The preferred date representation for the current locale, but without the
835 The preferred time representation for the current locale, but with no date.
838 The year without a century as a decimal number (range @code{00} through
839 @code{99}). This is equivalent to the year modulo 100.
842 The year as a decimal number, using the Gregorian calendar. Years
843 before the year @code{1} are numbered @code{0}, @code{-1}, and so on.
846 @w{RFC 822}/@w{ISO 8601:1988} style numeric time zone (e.g.,
847 @code{-0600} or @code{+0100}), or nothing if no time zone is
850 This format is a GNU extension.
853 The time zone abbreviation (empty if the time zone can't be determined).
856 A literal @samp{%} character.
859 The @var{size} parameter can be used to specify the maximum number of
860 characters to be stored in the array @var{s}, including the terminating
861 null character. If the formatted time requires more than @var{size}
862 characters, the excess characters are discarded. The return value from
863 @code{strftime} is the number of characters placed in the array @var{s},
864 not including the terminating null character. If the value equals
865 @var{size}, it means that the array @var{s} was too small; you should
866 repeat the call, providing a bigger array.
868 If @var{s} is a null pointer, @code{strftime} does not actually write
869 anything, but instead returns the number of characters it would have written.
871 For an example of @code{strftime}, see @ref{Time Functions Example}.
875 @subsection Specifying the Time Zone with @code{TZ}
877 In POSIX systems, a user can specify the time zone by means of the
878 @code{TZ} environment variable. For information about how to set
879 environment variables, see @ref{Environment Variables}. The functions
880 for accessing the time zone are declared in @file{time.h}.
884 You should not normally need to set @code{TZ}. If the system is
885 configured properly, the default time zone will be correct. You might
886 set @code{TZ} if you are using a computer over the network from a
887 different time zone, and would like times reported to you in the time zone
888 that local for you, rather than what is local for the computer.
890 In POSIX.1 systems the value of the @code{TZ} variable can be of one of
891 three formats. With the GNU C library, the most common format is the
892 last one, which can specify a selection from a large database of time
893 zone information for many regions of the world. The first two formats
894 are used to describe the time zone information directly, which is both
895 more cumbersome and less precise. But the POSIX.1 standard only
896 specifies the details of the first two formats, so it is good to be
897 familiar with them in case you come across a POSIX.1 system that doesn't
898 support a time zone information database.
900 The first format is used when there is no Daylight Saving Time (or
901 summer time) in the local time zone:
904 @r{@var{std} @var{offset}}
907 The @var{std} string specifies the name of the time zone. It must be
908 three or more characters long and must not contain a leading colon or
909 embedded digits, commas, or plus or minus signs. There is no space
910 character separating the time zone name from the @var{offset}, so these
911 restrictions are necessary to parse the specification correctly.
913 The @var{offset} specifies the time value one must add to the local time
914 to get a Coordinated Universal Time value. It has syntax like
915 [@code{+}|@code{-}]@var{hh}[@code{:}@var{mm}[@code{:}@var{ss}]]. This
916 is positive if the local time zone is west of the Prime Meridian and
917 negative if it is east. The hour must be between @code{0} and
918 @code{23}, and the minute and seconds between @code{0} and @code{59}.
920 For example, here is how we would specify Eastern Standard Time, but
921 without any daylight saving time alternative:
927 The second format is used when there is Daylight Saving Time:
930 @r{@var{std} @var{offset} @var{dst} [@var{offset}]@code{,}@var{start}[@code{/}@var{time}]@code{,}@var{end}[@code{/}@var{time}]}
933 The initial @var{std} and @var{offset} specify the standard time zone, as
934 described above. The @var{dst} string and @var{offset} specify the name
935 and offset for the corresponding daylight saving time time zone; if the
936 @var{offset} is omitted, it defaults to one hour ahead of standard time.
938 The remainder of the specification describes when daylight saving time is
939 in effect. The @var{start} field is when daylight saving time goes into
940 effect and the @var{end} field is when the change is made back to standard
941 time. The following formats are recognized for these fields:
945 This specifies the Julian day, with @var{n} between @code{1} and @code{365}.
946 February 29 is never counted, even in leap years.
949 This specifies the Julian day, with @var{n} between @code{0} and @code{365}.
950 February 29 is counted in leap years.
952 @item M@var{m}.@var{w}.@var{d}
953 This specifies day @var{d} of week @var{w} of month @var{m}. The day
954 @var{d} must be between @code{0} (Sunday) and @code{6}. The week
955 @var{w} must be between @code{1} and @code{5}; week @code{1} is the
956 first week in which day @var{d} occurs, and week @code{5} specifies the
957 @emph{last} @var{d} day in the month. The month @var{m} should be
958 between @code{1} and @code{12}.
961 The @var{time} fields specify when, in the local time currently in
962 effect, the change to the other time occurs. If omitted, the default is
965 For example, here is how one would specify the Eastern time zone in the
966 United States, including the appropriate daylight saving time and its dates
967 of applicability. The normal offset from UTC is 5 hours; since this is
968 west of the prime meridian, the sign is positive. Summer time begins on
969 the first Sunday in April at 2:00am, and ends on the last Sunday in October
973 EST+5EDT,M4.1.0/2,M10.5.0/2
976 The schedule of daylight saving time in any particular jurisdiction has
977 changed over the years. To be strictly correct, the conversion of dates
978 and times in the past should be based on the schedule that was in effect
979 then. However, this format has no facilities to let you specify how the
980 schedule has changed from year to year. The most you can do is specify
981 one particular schedule---usually the present day schedule---and this is
982 used to convert any date, no matter when. For precise time zone
983 specifications, it is best to use the time zone information database
986 The third format looks like this:
992 Each operating system interprets this format differently; in the GNU C
993 library, @var{characters} is the name of a file which describes the time
996 @pindex /etc/localtime
998 If the @code{TZ} environment variable does not have a value, the
999 operation chooses a time zone by default. In the GNU C library, the
1000 default time zone is like the specification @samp{TZ=:/etc/localtime}
1001 (or @samp{TZ=:/usr/local/etc/localtime}, depending on how GNU C library
1002 was configured; @pxref{Installation}). Other C libraries use their own
1003 rule for choosing the default time zone, so there is little we can say
1006 @cindex time zone database
1007 @pindex /share/lib/zoneinfo
1009 If @var{characters} begins with a slash, it is an absolute file name;
1010 otherwise the library looks for the file
1011 @w{@file{/share/lib/zoneinfo/@var{characters}}}. The @file{zoneinfo}
1012 directory contains data files describing local time zones in many
1013 different parts of the world. The names represent major cities, with
1014 subdirectories for geographical areas; for example,
1015 @file{America/New_York}, @file{Europe/London}, @file{Asia/Hong_Kong}.
1016 These data files are installed by the system administrator, who also
1017 sets @file{/etc/localtime} to point to the data file for the local time
1018 zone. The GNU C library comes with a large database of time zone
1019 information for most regions of the world, which is maintained by a
1020 community of volunteers and put in the public domain.
1022 @node Time Zone Functions
1023 @subsection Functions and Variables for Time Zones
1027 @deftypevar {char *} tzname [2]
1028 The array @code{tzname} contains two strings, which are the standard
1029 names of the pair of time zones (standard and daylight
1030 saving) that the user has selected. @code{tzname[0]} is the name of
1031 the standard time zone (for example, @code{"EST"}), and @code{tzname[1]}
1032 is the name for the time zone when daylight saving time is in use (for
1033 example, @code{"EDT"}). These correspond to the @var{std} and @var{dst}
1034 strings (respectively) from the @code{TZ} environment variable. If
1035 daylight saving time is never used, @code{tzname[1]} is the empty string.
1037 The @code{tzname} array is initialized from the @code{TZ} environment
1038 variable whenever @code{tzset}, @code{ctime}, @code{strftime},
1039 @code{mktime}, or @code{localtime} is called. If multiple abbreviations
1040 have been used (e.g. @code{"EWT"} and @code{"EDT"} for U.S. Eastern War
1041 Time and Eastern Daylight Time), the array contains the most recent
1044 The @code{tzname} array is required for POSIX.1 compatibility, but in
1045 GNU programs it is better to use the @code{tm_zone} member of the
1046 broken-down time structure, since @code{tm_zone} reports the correct
1047 abbreviation even when it is not the latest one.
1053 @deftypefun void tzset (void)
1054 The @code{tzset} function initializes the @code{tzname} variable from
1055 the value of the @code{TZ} environment variable. It is not usually
1056 necessary for your program to call this function, because it is called
1057 automatically when you use the other time conversion functions that
1058 depend on the time zone.
1061 The following variables are defined for compatibility with System V
1062 Unix. Like @code{tzname}, these variables are set by calling
1063 @code{tzset} or the other time conversion functions.
1067 @deftypevar {long int} timezone
1068 This contains the difference between UTC and the latest local standard
1069 time, in seconds west of UTC. For example, in the U.S. Eastern time
1070 zone, the value is @code{5*60*60}. Unlike the @code{tm_gmtoff} member
1071 of the broken-down time structure, this value is not adjusted for
1072 daylight saving, and its sign is reversed. In GNU programs it is better
1073 to use @code{tm_gmtoff}, since it contains the correct offset even when
1074 it is not the latest one.
1079 @deftypevar int daylight
1080 This variable has a nonzero value if daylight savings time rules apply.
1081 A nonzero value does not necessarily mean that daylight savings time is
1082 now in effect; it means only that daylight savings time is sometimes in
1086 @node Time Functions Example
1087 @subsection Time Functions Example
1089 Here is an example program showing the use of some of the local time and
1090 calendar time functions.
1093 @include strftim.c.texi
1096 It produces output like this:
1099 Wed Jul 31 13:02:36 1991
1100 Today is Wednesday, July 31.
1101 The time is 01:02 PM.
1105 @node Setting an Alarm
1106 @section Setting an Alarm
1108 The @code{alarm} and @code{setitimer} functions provide a mechanism for a
1109 process to interrupt itself at some future time. They do this by setting a
1110 timer; when the timer expires, the process receives a signal.
1112 @cindex setting an alarm
1113 @cindex interval timer, setting
1114 @cindex alarms, setting
1115 @cindex timers, setting
1116 Each process has three independent interval timers available:
1120 A real-time timer that counts clock time. This timer sends a
1121 @code{SIGALRM} signal to the process when it expires.
1122 @cindex real-time timer
1123 @cindex timer, real-time
1126 A virtual timer that counts CPU time used by the process. This timer
1127 sends a @code{SIGVTALRM} signal to the process when it expires.
1128 @cindex virtual timer
1129 @cindex timer, virtual
1132 A profiling timer that counts both CPU time used by the process, and CPU
1133 time spent in system calls on behalf of the process. This timer sends a
1134 @code{SIGPROF} signal to the process when it expires.
1135 @cindex profiling timer
1136 @cindex timer, profiling
1138 This timer is useful for profiling in interpreters. The interval timer
1139 mechanism does not have the fine granularity necessary for profiling
1141 @c @xref{profil} !!!
1144 You can only have one timer of each kind set at any given time. If you
1145 set a timer that has not yet expired, that timer is simply reset to the
1148 You should establish a handler for the appropriate alarm signal using
1149 @code{signal} or @code{sigaction} before issuing a call to @code{setitimer}
1150 or @code{alarm}. Otherwise, an unusual chain of events could cause the
1151 timer to expire before your program establishes the handler, and in that
1152 case it would be terminated, since that is the default action for the alarm
1153 signals. @xref{Signal Handling}.
1155 The @code{setitimer} function is the primary means for setting an alarm.
1156 This facility is declared in the header file @file{sys/time.h}. The
1157 @code{alarm} function, declared in @file{unistd.h}, provides a somewhat
1158 simpler interface for setting the real-time timer.
1164 @deftp {Data Type} {struct itimerval}
1165 This structure is used to specify when a timer should expire. It contains
1166 the following members:
1168 @item struct timeval it_interval
1169 This is the interval between successive timer interrupts. If zero, the
1170 alarm will only be sent once.
1172 @item struct timeval it_value
1173 This is the interval to the first timer interrupt. If zero, the alarm is
1177 The @code{struct timeval} data type is described in @ref{High-Resolution
1183 @deftypefun int setitimer (int @var{which}, struct itimerval *@var{new}, struct itimerval *@var{old})
1184 The @code{setitimer} function sets the timer specified by @var{which}
1185 according to @var{new}. The @var{which} argument can have a value of
1186 @code{ITIMER_REAL}, @code{ITIMER_VIRTUAL}, or @code{ITIMER_PROF}.
1188 If @var{old} is not a null pointer, @code{setitimer} returns information
1189 about any previous unexpired timer of the same kind in the structure it
1192 The return value is @code{0} on success and @code{-1} on failure. The
1193 following @code{errno} error conditions are defined for this function:
1197 The timer interval was too large.
1203 @deftypefun int getitimer (int @var{which}, struct itimerval *@var{old})
1204 The @code{getitimer} function stores information about the timer specified
1205 by @var{which} in the structure pointed at by @var{old}.
1207 The return value and error conditions are the same as for @code{setitimer}.
1215 This constant can be used as the @var{which} argument to the
1216 @code{setitimer} and @code{getitimer} functions to specify the real-time
1221 @item ITIMER_VIRTUAL
1222 @findex ITIMER_VIRTUAL
1223 This constant can be used as the @var{which} argument to the
1224 @code{setitimer} and @code{getitimer} functions to specify the virtual
1231 This constant can be used as the @var{which} argument to the
1232 @code{setitimer} and @code{getitimer} functions to specify the profiling
1238 @deftypefun {unsigned int} alarm (unsigned int @var{seconds})
1239 The @code{alarm} function sets the real-time timer to expire in
1240 @var{seconds} seconds. If you want to cancel any existing alarm, you
1241 can do this by calling @code{alarm} with a @var{seconds} argument of
1244 The return value indicates how many seconds remain before the previous
1245 alarm would have been sent. If there is no previous alarm, @code{alarm}
1249 The @code{alarm} function could be defined in terms of @code{setitimer}
1254 alarm (unsigned int seconds)
1256 struct itimerval old, new;
1257 new.it_interval.tv_usec = 0;
1258 new.it_interval.tv_sec = 0;
1259 new.it_value.tv_usec = 0;
1260 new.it_value.tv_sec = (long int) seconds;
1261 if (setitimer (ITIMER_REAL, &new, &old) < 0)
1264 return old.it_value.tv_sec;
1268 There is an example showing the use of the @code{alarm} function in
1269 @ref{Handler Returns}.
1271 If you simply want your process to wait for a given number of seconds,
1272 you should use the @code{sleep} function. @xref{Sleeping}.
1274 You shouldn't count on the signal arriving precisely when the timer
1275 expires. In a multiprocessing environment there is typically some
1276 amount of delay involved.
1278 @strong{Portability Note:} The @code{setitimer} and @code{getitimer}
1279 functions are derived from BSD Unix, while the @code{alarm} function is
1280 specified by the POSIX.1 standard. @code{setitimer} is more powerful than
1281 @code{alarm}, but @code{alarm} is more widely used.
1286 The function @code{sleep} gives a simple way to make the program wait
1287 for short periods of time. If your program doesn't use signals (except
1288 to terminate), then you can expect @code{sleep} to wait reliably for
1289 the specified amount of time. Otherwise, @code{sleep} can return sooner
1290 if a signal arrives; if you want to wait for a given period regardless
1291 of signals, use @code{select} (@pxref{Waiting for I/O}) and don't
1292 specify any descriptors to wait for.
1293 @c !!! select can get EINTR; using SA_RESTART makes sleep win too.
1297 @deftypefun {unsigned int} sleep (unsigned int @var{seconds})
1298 The @code{sleep} function waits for @var{seconds} or until a signal
1299 is delivered, whichever happens first.
1301 If @code{sleep} function returns because the requested time has
1302 elapsed, it returns a value of zero. If it returns because of delivery
1303 of a signal, its return value is the remaining time in the sleep period.
1305 The @code{sleep} function is declared in @file{unistd.h}.
1308 Resist the temptation to implement a sleep for a fixed amount of time by
1309 using the return value of @code{sleep}, when nonzero, to call
1310 @code{sleep} again. This will work with a certain amount of accuracy as
1311 long as signals arrive infrequently. But each signal can cause the
1312 eventual wakeup time to be off by an additional second or so. Suppose a
1313 few signals happen to arrive in rapid succession by bad luck---there is
1314 no limit on how much this could shorten or lengthen the wait.
1316 Instead, compute the time at which the program should stop waiting, and
1317 keep trying to wait until that time. This won't be off by more than a
1318 second. With just a little more work, you can use @code{select} and
1319 make the waiting period quite accurate. (Of course, heavy system load
1320 can cause unavoidable additional delays---unless the machine is
1321 dedicated to one application, there is no way you can avoid this.)
1323 On some systems, @code{sleep} can do strange things if your program uses
1324 @code{SIGALRM} explicitly. Even if @code{SIGALRM} signals are being
1325 ignored or blocked when @code{sleep} is called, @code{sleep} might
1326 return prematurely on delivery of a @code{SIGALRM} signal. If you have
1327 established a handler for @code{SIGALRM} signals and a @code{SIGALRM}
1328 signal is delivered while the process is sleeping, the action taken
1329 might be just to cause @code{sleep} to return instead of invoking your
1330 handler. And, if @code{sleep} is interrupted by delivery of a signal
1331 whose handler requests an alarm or alters the handling of @code{SIGALRM},
1332 this handler and @code{sleep} will interfere.
1334 On the GNU system, it is safe to use @code{sleep} and @code{SIGALRM} in
1335 the same program, because @code{sleep} does not work by means of
1338 @node Resource Usage
1339 @section Resource Usage
1341 @pindex sys/resource.h
1342 The function @code{getrusage} and the data type @code{struct rusage}
1343 are used for examining the usage figures of a process. They are declared
1344 in @file{sys/resource.h}.
1346 @comment sys/resource.h
1348 @deftypefun int getrusage (int @var{processes}, struct rusage *@var{rusage})
1349 This function reports the usage totals for processes specified by
1350 @var{processes}, storing the information in @code{*@var{rusage}}.
1352 In most systems, @var{processes} has only two valid values:
1355 @comment sys/resource.h
1358 Just the current process.
1360 @comment sys/resource.h
1362 @item RUSAGE_CHILDREN
1363 All child processes (direct and indirect) that have terminated already.
1366 In the GNU system, you can also inquire about a particular child process
1367 by specifying its process ID.
1369 The return value of @code{getrusage} is zero for success, and @code{-1}
1374 The argument @var{processes} is not valid.
1378 One way of getting usage figures for a particular child process is with
1379 the function @code{wait4}, which returns totals for a child when it
1380 terminates. @xref{BSD Wait Functions}.
1382 @comment sys/resource.h
1384 @deftp {Data Type} {struct rusage}
1385 This data type records a collection usage amounts for various sorts of
1386 resources. It has the following members, and possibly others:
1389 @item struct timeval ru_utime
1390 Time spent executing user instructions.
1392 @item struct timeval ru_stime
1393 Time spent in operating system code on behalf of @var{processes}.
1395 @item long int ru_maxrss
1396 The maximum resident set size used, in kilobytes. That is, the maximum
1397 number of kilobytes that @var{processes} used in real memory simultaneously.
1399 @item long int ru_ixrss
1400 An integral value expressed in kilobytes times ticks of execution, which
1401 indicates the amount of memory used by text that was shared with other
1404 @item long int ru_idrss
1405 An integral value expressed the same way, which is the amount of
1406 unshared memory used in data.
1408 @item long int ru_isrss
1409 An integral value expressed the same way, which is the amount of
1410 unshared memory used in stack space.
1412 @item long int ru_minflt
1413 The number of page faults which were serviced without requiring any I/O.
1415 @item long int ru_majflt
1416 The number of page faults which were serviced by doing I/O.
1418 @item long int ru_nswap
1419 The number of times @var{processes} was swapped entirely out of main memory.
1421 @item long int ru_inblock
1422 The number of times the file system had to read from the disk on behalf
1425 @item long int ru_oublock
1426 The number of times the file system had to write to the disk on behalf
1429 @item long int ru_msgsnd
1430 Number of IPC messages sent.
1432 @item long ru_msgrcv
1433 Number of IPC messages received.
1435 @item long int ru_nsignals
1436 Number of signals received.
1438 @item long int ru_nvcsw
1439 The number of times @var{processes} voluntarily invoked a context switch
1440 (usually to wait for some service).
1442 @item long int ru_nivcsw
1443 The number of times an involuntary context switch took place (because
1444 the time slice expired, or another process of higher priority became
1449 An additional historical function for examining usage figures,
1450 @code{vtimes}, is supported but not documented here. It is declared in
1451 @file{sys/vtimes.h}.
1453 @node Limits on Resources
1454 @section Limiting Resource Usage
1455 @cindex resource limits
1456 @cindex limits on resource usage
1457 @cindex usage limits
1459 You can specify limits for the resource usage of a process. When the
1460 process tries to exceed a limit, it may get a signal, or the system call
1461 by which it tried to do so may fail, depending on the limit. Each
1462 process initially inherits its limit values from its parent, but it can
1463 subsequently change them.
1465 @pindex sys/resource.h
1466 The symbols in this section are defined in @file{sys/resource.h}.
1468 @comment sys/resource.h
1470 @deftypefun int getrlimit (int @var{resource}, struct rlimit *@var{rlp})
1471 Read the current value and the maximum value of resource @var{resource}
1472 and store them in @code{*@var{rlp}}.
1474 The return value is @code{0} on success and @code{-1} on failure. The
1475 only possible @code{errno} error condition is @code{EFAULT}.
1478 @comment sys/resource.h
1480 @deftypefun int setrlimit (int @var{resource}, struct rlimit *@var{rlp})
1481 Store the current value and the maximum value of resource @var{resource}
1482 in @code{*@var{rlp}}.
1484 The return value is @code{0} on success and @code{-1} on failure. The
1485 following @code{errno} error condition is possible:
1489 You tried to change the maximum permissible limit value,
1490 but you don't have privileges to do so.
1494 @comment sys/resource.h
1496 @deftp {Data Type} {struct rlimit}
1497 This structure is used with @code{getrlimit} to receive limit values,
1498 and with @code{setrlimit} to specify limit values. It has two fields:
1502 The current value of the limit in question.
1503 This is also called the ``soft limit''.
1507 The maximum permissible value of the limit in question. You cannot set
1508 the current value of the limit to a larger number than this maximum.
1509 Only the super user can change the maximum permissible value.
1510 This is also called the ``hard limit''.
1514 In @code{getrlimit}, the structure is an output; it receives the current
1515 values. In @code{setrlimit}, it specifies the new values.
1518 Here is a list of resources that you can specify a limit for.
1519 Those that are sizes are measured in bytes.
1522 @comment sys/resource.h
1526 The maximum amount of cpu time the process can use. If it runs for
1527 longer than this, it gets a signal: @code{SIGXCPU}. The value is
1528 measured in seconds. @xref{Operation Error Signals}.
1530 @comment sys/resource.h
1533 @vindex RLIMIT_FSIZE
1534 The maximum size of file the process can create. Trying to write a
1535 larger file causes a signal: @code{SIGXFSZ}. @xref{Operation Error
1538 @comment sys/resource.h
1542 The maximum size of data memory for the process. If the process tries
1543 to allocate data memory beyond this amount, the allocation function
1546 @comment sys/resource.h
1549 @vindex RLIMIT_STACK
1550 The maximum stack size for the process. If the process tries to extend
1551 its stack past this size, it gets a @code{SIGSEGV} signal.
1552 @xref{Program Error Signals}.
1554 @comment sys/resource.h
1558 The maximum size core file that this process can create. If the process
1559 terminates and would dump a core file larger than this maximum size,
1560 then no core file is created. So setting this limit to zero prevents
1561 core files from ever being created.
1563 @comment sys/resource.h
1567 The maximum amount of physical memory that this process should get.
1568 This parameter is a guide for the system's scheduler and memory
1569 allocator; the system may give the process more memory when there is a
1572 @comment sys/resource.h
1574 @item RLIMIT_MEMLOCK
1575 The maximum amount of memory that can be locked into physical memory (so
1576 it will never be paged out).
1578 @comment sys/resource.h
1581 The maximum number of processes that can be created with the same user ID.
1582 If you have reached the limit for your user ID, @code{fork} will fail
1583 with @code{EAGAIN}. @xref{Creating a Process}.
1585 @comment sys/resource.h
1588 @vindex RLIMIT_NOFILE
1590 @vindex RLIMIT_OFILE
1591 The maximum number of files that the process can open. If it tries to
1592 open more files than this, it gets error code @code{EMFILE}.
1593 @xref{Error Codes}. Not all systems support this limit; GNU does, and
1596 @comment sys/resource.h
1599 @vindex RLIM_NLIMITS
1600 The number of different resource limits. Any valid @var{resource}
1601 operand must be less than @code{RLIM_NLIMITS}.
1604 @comment sys/resource.h
1606 @defvr Constant int RLIM_INFINITY
1607 This constant stands for a value of ``infinity'' when supplied as
1608 the limit value in @code{setrlimit}.
1611 @c ??? Someone want to finish these?
1612 Two historical functions for setting resource limits, @code{ulimit} and
1613 @code{vlimit}, are not documented here. The latter is declared in
1614 @file{sys/vlimit.h} and comes from BSD.
1617 @section Process Priority
1618 @cindex process priority
1619 @cindex priority of a process
1621 @pindex sys/resource.h
1622 When several processes try to run, their respective priorities determine
1623 what share of the CPU each process gets. This section describes how you
1624 can read and set the priority of a process. All these functions and
1625 macros are declared in @file{sys/resource.h}.
1627 The range of valid priority values depends on the operating system, but
1628 typically it runs from @code{-20} to @code{20}. A lower priority value
1629 means the process runs more often. These constants describe the range of
1633 @comment sys/resource.h
1637 The smallest valid priority value.
1639 @comment sys/resource.h
1643 The smallest valid priority value.
1646 @comment sys/resource.h
1648 @deftypefun int getpriority (int @var{class}, int @var{id})
1649 Read the priority of a class of processes; @var{class} and @var{id}
1650 specify which ones (see below). If the processes specified do not all
1651 have the same priority, this returns the smallest value that any of them
1654 The return value is the priority value on success, and @code{-1} on
1655 failure. The following @code{errno} error condition are possible for
1660 The combination of @var{class} and @var{id} does not match any existing
1664 The value of @var{class} is not valid.
1667 When the return value is @code{-1}, it could indicate failure, or it
1668 could be the priority value. The only way to make certain is to set
1669 @code{errno = 0} before calling @code{getpriority}, then use @code{errno
1670 != 0} afterward as the criterion for failure.
1673 @comment sys/resource.h
1675 @deftypefun int setpriority (int @var{class}, int @var{id}, int @var{priority})
1676 Set the priority of a class of processes to @var{priority}; @var{class}
1677 and @var{id} specify which ones (see below).
1679 The return value is @code{0} on success and @code{-1} on failure. The
1680 following @code{errno} error condition are defined for this function:
1684 The combination of @var{class} and @var{id} does not match any existing
1688 The value of @var{class} is not valid.
1691 You tried to set the priority of some other user's process, and you
1692 don't have privileges for that.
1695 You tried to lower the priority of a process, and you don't have
1696 privileges for that.
1700 The arguments @var{class} and @var{id} together specify a set of
1701 processes you are interested in. These are the possible values for
1705 @comment sys/resource.h
1708 @vindex PRIO_PROCESS
1709 Read or set the priority of one process. The argument @var{id} is a
1712 @comment sys/resource.h
1716 Read or set the priority of one process group. The argument @var{id} is
1719 @comment sys/resource.h
1723 Read or set the priority of one user's processes. The argument @var{id}
1727 If the argument @var{id} is 0, it stands for the current process,
1728 current process group, or the current user, according to @var{class}.
1730 @c ??? I don't know where we should say this comes from.
1733 @deftypefun int nice (int @var{increment})
1734 Increment the priority of the current process by @var{increment}.
1735 The return value is the same as for @code{setpriority}.
1737 Here is an equivalent definition for @code{nice}:
1741 nice (int increment)
1743 int old = getpriority (PRIO_PROCESS, 0);
1744 return setpriority (PRIO_PROCESS, 0, old + increment);