PCLOSE(3POSIX)
π PCLOSE(3POSIX) π
okay bestie, welcome to the POSIX Programmer's Manual entry for pclose, and no, this is not the Linux manual, it's the POSIX one, they're different girlies okay π«Ά
πΈ PROLOG πΈ
okay so real talk before we get into it: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β so if you're on Linux, go consult the corresponding Linux manual page for the actual Linux behavior, mkay? Or the interface might not even be implemented on Linux at all. It's giving "read the fine print" energy, but like, lovingly π
β¨ NAME β¨
pclose β close a pipe stream to or from a process. yes queen, that's the whole job. shutting down the pipe and waiting for your little process child to finish its thing π
π SYNOPSIS π
#include <stdio.h>
int pclose(FILE *stream);
π¦ DESCRIPTION π¦
okay so here's the tea β The pclose() function shall close a stream that was opened by popen(), wait for the command to terminate, and return the termination status of the process that was running the command language interpreter.
BUT β and this is the plot twist π β if a call caused the termination status to be unavailable to pclose(), then pclose() shall return -1 with errno set to [ECHILD] to report this situation. This can happen if the application calls one of the following functions:
wait()waitpid()with a pid argument less than or equal to0or equal to the process ID of the command line interpreter- Any other function not defined in this volume of POSIX.1β2017 that could do one of the above
In any case, pclose() shall not return before the child process created by popen() has terminated. periodt. it WAITS. it's a patient queen π
If the command language interpreter cannot be executed, the child termination status returned by pclose() shall be as if the command language interpreter terminated using exit(127) or _exit(127).
The pclose() function shall not affect the termination status of any child of the calling process other than the one created by popen() for the associated stream. mind your own business, basically π
If the argument stream topclose()is not a pointer to a stream created bypopen(), the result ofpclose()is undefined. π do NOT do this.
If a thread is canceled during execution of pclose(), the behavior is undefined. π no cap.
π RETURN VALUE π
Upon successful return, pclose() shall return the termination status of the command language interpreter. slay β¨
Otherwise, pclose() shall return -1 and set errno to indicate the error. it's giving "something went wrong bestie" π
π ERRORS π
The pclose() function shall fail if:
ECHILDβ The status of the child process could not be obtained, as described above. (that's the "you already reaped your child, so now I literally cannot get the status" situation, rip)
The following sections are informative.
π EXAMPLES π
None. yeah. nothing. empty. the void. π«Ά
π§ APPLICATION USAGE π§
None. also empty bestie, we're just vibing πΈ
π RATIONALE π
okay so there's a requirement that pclose() not return before the child process terminates. This is intended to disallow implementations that return [EINTR] if a signal is received while waiting. Because think about it β if pclose() returned before the child terminated, there would be no way for the application to discover which child used to be associated with the stream, and it could not do the cleanup itself. the app would just be standing there like π§ no clue which child is which. tragic.
If the stream pointed to by stream was not created by popen(), historical implementations of pclose() return -1 without setting errno. To avoid requiring pclose() to set errno in this case, POSIX.1β2008 makes the behavior unspecified. An application should not use pclose() to close any stream that was not created by popen(). like, just don't, bestie π
Some historical implementations of pclose() either block or ignore the signals SIGINT, SIGQUIT, and SIGHUP while waiting for the child process to terminate. Since this behavior is not described for the pclose() function in POSIX.1β2008, such implementations are not conforming. Also, some historical implementations return [EINTR] if a signal is received, even though the child process has not terminated. Such implementations are also considered non-conforming. we do not stan π«
Consider, for example, an application that uses:
popen("command", "r")
to start command, which is part of the same application. The parent writes a prompt to its standard output (presumably the terminal) and then reads from the popen()ed stream. The child reads the response from the user, does some transformation on the response (pathname expansion, perhaps) and writes the result to its standard output. The parent process reads the result from the pipe, does something with it, and prints another prompt. The cycle repeats. Assuming that both processes do appropriate buffer flushing, this would be expected to work. it's giving collaborative co-parenting energy, and honestly? iconic π
To conform to POSIX.1β2008, pclose() must use waitpid(), or some similar function, instead of wait(). (yes, wait() is the problematic ex here, we moved on π©)
The code sample below illustrates how the pclose() function might be implemented on a system conforming to POSIX.1β2008.
int pclose(FILE *stream)
{
int stat;
pid_t pid;
pid = <pid for process created for stream by popen()>
(void) fclose(stream);
while (waitpid(pid, &stat, 0) == -1) {
if (errno != EINTR){
stat = -1;
break;
}
}
return(stat);
}
like look at that while loop bestie. that's the whole "keep waiting until it actually works, unless it's a real error" situation. chef's kiss β¨
π FUTURE DIRECTIONS π
None. the future is unclear but this function is staying exactly as it is π
π₯ SEE ALSO π₯
fork()popen()wait()
The Base Definitions volume of POSIX.1β2017, <stdio.h> β go read that header bestie, it's where FILE lives π¦
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page bestie!! π this icon was brought to you by IEEE/The Open Group, dated 2017 β mwah, thanks for the lore, we ate πβ¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms