1PCLOSE(P)                  POSIX Programmer's Manual                 PCLOSE(P)
2
3
4

NAME

6       pclose - close a pipe stream to or from a process
7

SYNOPSIS

9       #include <stdio.h>
10
11       int pclose(FILE *stream);
12
13

DESCRIPTION

15       The  pclose() function shall close a stream that was opened by popen(),
16       wait for the command to terminate, and return the termination status of
17       the  process  that  was running the command language interpreter.  How‐
18       ever, if a call caused the termination  status  to  be  unavailable  to
19       pclose(),  then  pclose() shall return -1 with errno set to [ECHILD] to
20       report this situation. This can happen if the application calls one  of
21       the following functions:
22
23        * wait()
24
25        * waitpid()  with  a  pid argument less than or equal to 0 or equal to
26          the process ID of the command line interpreter
27
28        * Any   other   function   not   defined    in    this    volume    of
29          IEEE Std 1003.1-2001 that could do one of the above
30
31       In any case, pclose() shall not return before the child process created
32       by popen() has terminated.
33
34       If the command language interpreter cannot be executed, the child  ter‐
35       mination  status  returned  by pclose() shall be as if the command lan‐
36       guage interpreter terminated using exit(127) or _exit(127).
37
38       The pclose() function shall not affect the termination  status  of  any
39       child  of the calling process other than the one created by popen() for
40       the associated stream.
41
42       If the argument stream to pclose() is not a pointer to a stream created
43       by popen(), the result of pclose() is undefined.
44

RETURN VALUE

46       Upon successful return, pclose() shall return the termination status of
47       the command language interpreter. Otherwise, pclose() shall  return  -1
48       and set errno to indicate the error.
49

ERRORS

51       The pclose() function shall fail if:
52
53       ECHILD The  status  of  the  child  process  could  not be obtained, as
54              described above.
55
56
57       The following sections are informative.
58

EXAMPLES

60       None.
61

APPLICATION USAGE

63       None.
64

RATIONALE

66       There is a requirement  that  pclose()  not  return  before  the  child
67       process  terminates.  This is intended to disallow implementations that
68       return [EINTR] if a signal  is  received  while  waiting.  If  pclose()
69       returned  before  the  child  terminated, there would be no way for the
70       application to discover which child used  to  be  associated  with  the
71       stream, and it could not do the cleanup itself.
72
73       If the stream pointed to by stream was not created by popen(), histori‐
74       cal implementations of pclose() return -1 without  setting  errno.   To
75       avoid    requiring    pclose()    to    set   errno   in   this   case,
76       IEEE Std 1003.1-2001 makes the behavior  unspecified.   An  application
77       should  not  use  pclose()  to close any stream that was not created by
78       popen().
79
80       Some historical implementations of pclose() either block or ignore  the
81       signals SIGINT, SIGQUIT, and SIGHUP while waiting for the child process
82       to terminate. Since this behavior is not  described  for  the  pclose()
83       function in IEEE Std 1003.1-2001, such implementations are not conform‐
84       ing. Also, some historical implementations return [EINTR] if  a  signal
85       is  received,  even  though the child process has not terminated.  Such
86       implementations are also considered non-conforming.
87
88       Consider, for example, an application that uses:
89
90
91              popen("command", "r")
92
93       to start command, which is part of the  same  application.  The  parent
94       writes  a  prompt  to its standard output (presumably the terminal) and
95       then reads from the popen()ed stream. The child reads the response from
96       the user, does some transformation on the response (pathname expansion,
97       perhaps) and writes the result to  its  standard  output.   The  parent
98       process  reads  the  result  from the pipe, does something with it, and
99       prints another prompt. The cycle repeats. Assuming that both  processes
100       do appropriate buffer flushing, this would be expected to work.
101
102       To  conform  to  IEEE Std 1003.1-2001,  pclose() must use waitpid(), or
103       some similar function, instead of wait().
104
105       The code sample below illustrates how the pclose()  function  might  be
106       implemented on a system conforming to IEEE Std 1003.1-2001.
107
108
109              int pclose(FILE *stream)
110              {
111                  int stat;
112                  pid_t pid;
113
114
115                  pid = <pid for process created for stream by popen()>
116                  (void) fclose(stream);
117                  while (waitpid(pid, &stat, 0) == -1) {
118                      if (errno != EINTR){
119                          stat = -1;
120                          break;
121                      }
122                  }
123                  return(stat);
124              }
125

FUTURE DIRECTIONS

127       None.
128

SEE ALSO

130       fork()  ,  popen()  ,  waitpid()  ,  the  Base  Definitions  volume  of
131       IEEE Std 1003.1-2001, <stdio.h>
132
134       Portions of this text are reprinted and reproduced in  electronic  form
135       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
136       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
137       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
138       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
139       event of any discrepancy between this version and the original IEEE and
140       The Open Group Standard, the original IEEE and The Open Group  Standard
141       is  the  referee document. The original Standard can be obtained online
142       at http://www.opengroup.org/unix/online.html .
143
144
145
146IEEE/The Open Group                  2003                            PCLOSE(P)
Impressum