]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/nanosleep.2
Reordered sections to be more consistent, in some cases renaming
[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.
43 In this case, it returns \-1, sets \fIerrno\fR to
44 .BR EINTR ,
45 and writes the
46 remaining time into the structure pointed to by
47 .IR rem
48 unless
49 .I rem
50 is NULL.
51 The value of
52 .I *rem
53 can then be used to call
54 .BR nanosleep ()
55 again and complete the specified pause.
56
57 The structure
58 .I timespec
59 is used to specify intervals of time with nanosecond precision.
60 It is
61 specified in
62 .I <time.h>
63 and has the form
64 .sp
65 .RS
66 .nf
67 struct timespec {
68 time_t tv_sec; /* seconds */
69 long tv_nsec; /* nanoseconds */
70 };
71 .fi
72 .RE
73 .PP
74 The value of the nanoseconds field must be in the range 0 to 999999999.
75
76 Compared to
77 .BR sleep (3)
78 and
79 .BR usleep (3),
80 .BR nanosleep ()
81 has the advantage of not affecting any signals, it is standardized by
82 POSIX, it provides higher timing resolution, and it allows to continue
83 a sleep that has been interrupted by a signal more easily.
84 .SH "RETURN VALUE"
85 On successfully sleeping for the requested interval,
86 .BR nanosleep ()
87 returns 0.
88 If the call is interrupted by a signal handler or encounters an error,
89 then it returns \-1, with
90 .I errno
91 set to indicate the error.
92 .SH ERRORS
93 .TP
94 .B EFAULT
95 Problem with copying information from user space.
96 .TP
97 .B EINTR
98 The pause has been interrupted by a non-blocked signal that was
99 delivered to the process.
100 The remaining sleep time has been written
101 into *\fIrem\fR so that the process can easily call
102 .BR nanosleep ()
103 again and continue with the pause.
104 .TP
105 .B EINVAL
106 The value in the
107 .I tv_nsec
108 field was not in the range 0 to 999999999 or
109 .I tv_sec
110 was negative.
111 .SH "CONFORMING TO"
112 POSIX.1-2001.
113 .SH BUGS
114 The current implementation of
115 .BR nanosleep ()
116 is based on the normal kernel timer mechanism, which has a resolution
117 of 1/\fIHZ\fR\ s (see
118 .BR time (7)).
119 Therefore,
120 .BR nanosleep ()
121 pauses always for at least the specified time, however it can take up
122 to 10 ms longer than specified until the process becomes runnable
123 again.
124 For the same reason, the value returned in case of a delivered
125 signal in *\fIrem\fR is usually rounded to the next larger multiple of
126 1/\fIHZ\fR\ s.
127 .SS "Old behaviour"
128 In order to support applications requiring much more precise pauses
129 (e.g., in order to control some time-critical hardware),
130 .BR nanosleep ()
131 would handle pauses of up to 2\ ms by busy waiting with microsecond
132 precision when called from a process scheduled under a real-time policy
133 like
134 .I SCHED_FIFO
135 or
136 .IR SCHED_RR .
137 This special extension was removed in kernel 2.5.39,
138 hence is still present in
139 current 2.4 kernels, but not in 2.6 kernels.
140 .PP
141 In Linux 2.4, if
142 .BR nanosleep ()
143 is stopped by a signal (e.g., SIGTSTP),
144 then the call fails with the error
145 .BR EINTR
146 after the process is resumed by a SIGCONT signal.
147 If the system call is subsequently restarted,
148 then the time that the process spent in the stopped state is
149 \fInot\fP counted against the sleep interval.
150 .SH "SEE ALSO"
151 .BR sched_setscheduler (2),
152 .BR timer_create (2),
153 .BR sleep (3),
154 .BR usleep (3),
155 .BR feature_test_macros (7),
156 .BR time (7)