]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/nanosleep.2
ffix
[thirdparty/man-pages.git] / man2 / nanosleep.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (C) Markus Kuhn, 1996
4 .\"
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
9 .\"
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
14 .\"
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 .\" GNU General Public License for more details.
19 .\"
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, write to the Free
22 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
23 .\" USA.
24 .\"
25 .\" 1996-04-10 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
26 .\" First version written
27 .\" Modified, 2004-10-24, aeb
28 .TH NANOSLEEP 2 2004-10-24 "Linux 2.6.9" "Linux Programmer's Manual"
29 .SH NAME
30 nanosleep \- pause execution for a specified time
31 .SH SYNOPSIS
32 .B #define _POSIX_C_SOURCE 199309
33 .B #include <time.h>
34 .sp
35 \fBint nanosleep(const struct timespec *\fIreq\fB, struct timespec *\fIrem\fB);
36 .fi
37 .SH DESCRIPTION
38 .BR nanosleep ()
39 delays the execution of the program for at least the time specified in
40 .IR *req .
41 The function can return earlier if a signal has been delivered to the
42 process. In this case, it returns \-1, sets \fIerrno\fR to
43 .BR EINTR ,
44 and writes the
45 remaining time into the structure pointed to by
46 .IR rem
47 unless
48 .I rem
49 is NULL.
50 The value of
51 .I *rem
52 can then be used to call
53 .BR nanosleep ()
54 again and complete the specified pause.
55
56 The structure
57 .I timespec
58 is used to specify intervals of time with nanosecond precision. It is
59 specified in
60 .I <time.h>
61 and has the form
62 .sp
63 .RS
64 .nf
65 struct timespec {
66 time_t tv_sec; /* seconds */
67 long tv_nsec; /* nanoseconds */
68 };
69 .fi
70 .RE
71 .PP
72 The value of the nanoseconds field must be in the range 0 to 999999999.
73
74 Compared to
75 .BR sleep (3)
76 and
77 .BR usleep (3),
78 .BR nanosleep ()
79 has the advantage of not affecting any signals, it is standardized by
80 POSIX, it provides higher timing resolution, and it allows to continue
81 a sleep that has been interrupted by a signal more easily.
82 .SH ERRORS
83 In case of an error or exception, the
84 .BR nanosleep ()
85 system call returns \-1 instead of 0 and sets
86 .I errno
87 to one of the following values:
88 .TP
89 .B EFAULT
90 Problem with copying information from user space.
91 .TP
92 .B EINTR
93 The pause has been interrupted by a non-blocked signal that was
94 delivered to the process. The remaining sleep time has been written
95 into *\fIrem\fR so that the process can easily call
96 .BR nanosleep ()
97 again and continue with the pause.
98 .TP
99 .B EINVAL
100 The value in the
101 .I tv_nsec
102 field was not in the range 0 to 999999999 or
103 .I tv_sec
104 was negative.
105 .SH BUGS
106 The current implementation of
107 .BR nanosleep ()
108 is based on the normal kernel timer mechanism, which has a resolution
109 of 1/\fIHZ\fR\ s (i.e, 10\ ms on Linux/i386 and 1\ ms on Linux/Alpha).
110 Therefore,
111 .BR nanosleep ()
112 pauses always for at least the specified time, however it can take up
113 to 10 ms longer than specified until the process becomes runnable
114 again. For the same reason, the value returned in case of a delivered
115 signal in *\fIrem\fR is usually rounded to the next larger multiple of
116 1/\fIHZ\fR\ s.
117
118 .SS "Old behaviour"
119 In order to support applications requiring much more precise pauses
120 (e.g., in order to control some time-critical hardware),
121 .BR nanosleep ()
122 would handle pauses of up to 2\ ms by busy waiting with microsecond
123 precision when called from a process scheduled under a real-time policy
124 like
125 .I SCHED_FIFO
126 or
127 .IR SCHED_RR .
128 This special extension was removed in kernel 2.5.39, hence is still present in
129 current 2.4 kernels, but not in 2.6 kernels.
130 .SH "CONFORMING TO"
131 POSIX.1b (formerly POSIX.4).
132 .SH "SEE ALSO"
133 .BR sched_setscheduler (2),
134 .BR timer_create (2),
135 .BR sleep (3),
136 .BR usleep (3)