blob: d29fc719ad04f2063f163fc3d9f4bfca548e261b [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH WAIT 3
2.SH NAME
3await, wait, waitpid \- wait for a process to exit
4.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
13int waitpid(void)
14.PP
15.B
16int await(char *s, int n)
17.SH DESCRIPTION
18.I Wait
19causes a process to wait for any child process (see
20.IR fork (2))
21to exit.
22It returns a
23.B Waitmsg
24holding
25information about the exited child.
26A
27.B Waitmsg
28has this structure:
29.IP
30.EX
31.ta 6n +\w'long 'u +\w'msg[ERRLEN]; 'u
32typedef
33struct Waitmsg
34{
35 int pid; /* of loved one */
36 ulong time[3]; /* of loved one & descendants */
37 char *msg;
38} Waitmsg;
39.EE
40.PP
41.B Pid
42is the child's
43process id.
44The
45.B time
46array contains the time the child and its descendants spent in user code,
47the time spent in system calls, and the child's elapsed real time,
48all in units of milliseconds.
49.B Msg
50contains the message that the child specified in
51.IR exits (2).
52For a normal exit,
53.B msg[0]
54is zero,
55otherwise
56.B msg
57is the exit string
58prefixed by the process name, a blank, the process id, and a colon.
59.PP
60If there are no more children to wait for,
61.I wait
62returns immediately, with return value nil.
63.PP
64The
65.B Waitmsg
66structure is allocated by
67.IR malloc (2)
68and should be freed after use.
69For programs that only need the pid of the exiting program,
70.I waitpid
71returns just the pid and discards the rest of the information.
72.PP
73The underlying system call is
74.IR await ,
75which fills in the n-byte buffer
76.I s
77with a textual representation of the pid, times, and exit string.
78There is no terminal NUL.
79The return value is the length, in bytes, of the data.
80.PP
81The buffer filled in by
82.I await
83may be parsed (after appending a NUL) using
84.IR tokenize
85(see
86.IR getfields (2));
87the resulting fields are, in order, pid, the three times, and the exit string,
88which will be
89.B ''
90for normal exit.
91If the representation is longer than
92.I n
93bytes, it is truncated but, if possible, properly formatted.
94The information that does not fit in the buffer is discarded, so
95a subsequent call to
96.I await
97will return the information about the next exiting child, not the remainder
98of the truncated message.
99In other words, each call to
100.I await
101returns the information about one child, blocking if necessary if no child has exited.
102If the calling process has no living children,
103.I await
104returns
105.BR -1 .
106.SH SOURCE
107.B /sys/src/libc/9syscall
108.SH "SEE ALSO"
109.IR fork (2),
110.IR exits (2),
111the
112.B wait
113file in
114.IR proc (3)
115.SH DIAGNOSTICS
116These routines set
117.IR errstr .