Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


pclose(3p) — Linux manual page

PROLOG |NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |EXAMPLES |APPLICATION USAGE |RATIONALE |FUTURE DIRECTIONS |SEE ALSO |COPYRIGHT

PCLOSE(3P)              POSIX Programmer's ManualPCLOSE(3P)

PROLOG        top

       This manual page is part of the POSIX Programmer's Manual.  The       Linux implementation of this interface may differ (consult the       corresponding Linux manual page for details of Linux behavior), or       the interface may not be implemented on Linux.

NAME        top

       pclose — close a pipe stream to or from a process

SYNOPSIS        top

       #include <stdio.h>       int pclose(FILE *stream);

DESCRIPTION        top

       Thepclose() function shall close a stream that was opened bypopen(), wait for the command to terminate, and return the       termination status of the process that was running the command       language interpreter.  However, if a call caused the termination       status to be unavailable topclose(), thenpclose() shall return       -1 witherrno set to[ECHILD]to report this situation. This can       happen if the application calls one of the following functions:        *wait()        *waitpid() with apid argument less than or equal to 0 or 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 bypopen() has terminated.       If the command language interpreter cannot be executed, the child       termination status returned bypclose() shall be as if the command       language interpreter terminated usingexit(127) or_exit(127).       Thepclose() function shall not affect the termination status of       any child of the calling process other than the one created bypopen() for the associated stream.       If the argumentstream topclose() is not a pointer to a stream       created bypopen(), the result ofpclose() is undefined.       If a thread is canceled during execution ofpclose(), the behavior       is undefined.

RETURN VALUE        top

       Upon successful return,pclose() shall return the termination       status of the command language interpreter. Otherwise,pclose()       shall return -1 and seterrno to indicate the error.

ERRORS        top

       Thepclose() function shall fail if:ECHILDThe status of the child process could not be obtained, as              described above.The following sections are informative.

EXAMPLES        top

       None.

APPLICATION USAGE        top

       None.

RATIONALE        top

       There is a requirement thatpclose() not return before the child       process terminates. This is intended to disallow implementations       that return[EINTR]if a signal is received while waiting. Ifpclose() 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.       If the stream pointed to bystream was not created bypopen(),       historical implementations ofpclose() return -1 without settingerrno.  To avoid requiringpclose() to seterrno in this case,       POSIX.1‐2008 makes the behavior unspecified. An application should       not usepclose() to close any stream that was not created bypopen().       Some historical implementations ofpclose() 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 thepclose() 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.       Consider, for example, an application that uses:           popen("command", "r")       to startcommand, which is part of the same application. The       parent writes a prompt to its standard output (presumably the       terminal) and then reads from thepopen()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.       To conform to POSIX.1‐2008,pclose() must usewaitpid(), or some       similar function, instead ofwait().       The code sample below illustrates how thepclose() 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);           }

FUTURE DIRECTIONS        top

       None.

SEE ALSO        top

fork(3p),popen(3p),wait(3p)       The Base Definitions volume of POSIX.1‐2017,stdio.h(0p)

COPYRIGHT        top

       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 athttp://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, seehttps://www.kernel.org/doc/man-pages/reporting_bugs.html .IEEE/The Open Group                2017PCLOSE(3P)

Pages that refer to this page:stdio.h(0p)popen(3p)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp