blob: 28d5d731ea066b165e5ce8cde1a913cc1533eed0 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH WAIT 3
2.SH NAME
rsc058b0112005-01-03 06:40:20 +00003await, awaitnohang, awaitfor, wait, waitnohang, waitfor, waitpid \- wait for a process to exit
rsccfa37a72004-04-10 18:53:55 +00004.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.PP
9.B
10Waitmsg* wait(void)
11.PP
12.B
rsc058b0112005-01-03 06:40:20 +000013Waitmsg* waitnohang(void)
14.PP
15.B
16Waitmsg* waitfor(int pid)
17.PP
18.B
rsccfa37a72004-04-10 18:53:55 +000019int waitpid(void)
20.PP
21.B
22int await(char *s, int n)
rsc058b0112005-01-03 06:40:20 +000023.PP
24.B
25int awaitnohang(char *s, int n)
26.PP
27.B
28int awaitfor(int pid, char *s, int n)
rsccfa37a72004-04-10 18:53:55 +000029.SH DESCRIPTION
30.I Wait
31causes a process to wait for any child process (see
rsc058b0112005-01-03 06:40:20 +000032.IR fork (2)
33and
34.IR rfork (3))
rsccfa37a72004-04-10 18:53:55 +000035to exit.
36It returns a
37.B Waitmsg
38holding
39information about the exited child.
40A
41.B Waitmsg
42has this structure:
43.IP
44.EX
45.ta 6n +\w'long 'u +\w'msg[ERRLEN]; 'u
46typedef
47struct Waitmsg
48{
49 int pid; /* of loved one */
50 ulong time[3]; /* of loved one & descendants */
51 char *msg;
52} Waitmsg;
53.EE
54.PP
55.B Pid
56is the child's
57process id.
58The
59.B time
60array contains the time the child and its descendants spent in user code,
61the time spent in system calls, and the child's elapsed real time,
62all in units of milliseconds.
63.B Msg
64contains the message that the child specified in
rscbf8a59f2004-04-11 03:42:27 +000065.IR exits (3).
rsccfa37a72004-04-10 18:53:55 +000066For a normal exit,
67.B msg[0]
68is zero,
69otherwise
70.B msg
71is the exit string
72prefixed by the process name, a blank, the process id, and a colon.
73.PP
74If there are no more children to wait for,
75.I wait
76returns immediately, with return value nil.
77.PP
78The
79.B Waitmsg
80structure is allocated by
rscbf8a59f2004-04-11 03:42:27 +000081.IR malloc (3)
rsccfa37a72004-04-10 18:53:55 +000082and should be freed after use.
83For programs that only need the pid of the exiting program,
84.I waitpid
85returns just the pid and discards the rest of the information.
86.PP
rsc058b0112005-01-03 06:40:20 +000087.I Waitnohang
88is like
89.I wait
90but does not block if there are no more children to wait for.
91Instead it returns immediately and sets
92.IR errstr .
93.PP
94.I Waitfor
95is like
96.I wait
97but waits for a particular
98.IR pid .
99.PP
100The underlying calls are
rsccfa37a72004-04-10 18:53:55 +0000101.IR await ,
rsc058b0112005-01-03 06:40:20 +0000102.IR awaitnohang ,
103and
104.IR awaitfor ,
105which fill in the
106.IR n -byte
107buffer
rsccfa37a72004-04-10 18:53:55 +0000108.I s
109with a textual representation of the pid, times, and exit string.
110There is no terminal NUL.
111The return value is the length, in bytes, of the data.
112.PP
rsc058b0112005-01-03 06:40:20 +0000113The filled-in buffer
rsccfa37a72004-04-10 18:53:55 +0000114may be parsed (after appending a NUL) using
115.IR tokenize
116(see
rscbf8a59f2004-04-11 03:42:27 +0000117.IR getfields (3));
rsccfa37a72004-04-10 18:53:55 +0000118the resulting fields are, in order, pid, the three times, and the exit string,
119which will be
120.B ''
121for normal exit.
122If the representation is longer than
123.I n
124bytes, it is truncated but, if possible, properly formatted.
125The information that does not fit in the buffer is discarded, so
126a subsequent call to
127.I await
128will return the information about the next exiting child, not the remainder
129of the truncated message.
130In other words, each call to
131.I await
132returns the information about one child, blocking if necessary if no child has exited.
133If the calling process has no living children,
134.I await
135returns
136.BR -1 .
137.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000138.B \*9/src/lib9/wait.c
rsc058b0112005-01-03 06:40:20 +0000139.PP
rscc3674de2005-01-11 17:37:33 +0000140.B \*9/src/lib9/await.c
rsccfa37a72004-04-10 18:53:55 +0000141.SH "SEE ALSO"
rsc058b0112005-01-03 06:40:20 +0000142.IR rfork (3),
rscbf8a59f2004-04-11 03:42:27 +0000143.IR exits (3),
rsccfa37a72004-04-10 18:53:55 +0000144.SH DIAGNOSTICS
145These routines set
146.IR errstr .
rscc8b63422005-01-13 04:49:19 +0000147.SH BUGS
148To avoid name conflicts with the underlying system,
149.IR wait ,
150.IR waitpid ,
151and
152.I waitfor
153are preprocessor macros defined as
154.IR p9wait ,
155.IR p9waitpid ,
156and
157.IR p9waitfor ;
158see
159.IR intro (3).