C语言popen()函数 pclose()函数(打开关闭流入或流出进程的管道流)
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言popen()函数 pclose()函数(打开关闭流入或流出进程的管道流)相关的知识,希望对你有一定的参考价值。
20220602 man
POPEN(3) Linux Programmer's Manual POPEN(3)
NAME
popen, pclose - pipe stream to or from a process //流入或流出进程的管道流
SYNOPSIS
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
popen(), pclose():
_POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
DESCRIPTION
The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only.
//popen() 函数通过创建管道、分叉和调用 shell 来打开一个进程。
//由于管道根据定义是单向的,类型参数可以只指定读或写,不能同时指定;
//结果流相应地是只读的或只写的。
The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; inter‐pretation, if any, is performed by the shell.
//command 参数是一个指向包含 shell 命令行的以空字符结尾的字符串的指针。
//该命令使用 -c 标志传递给 /bin/sh;
//解释(如果有)由 shell 执行。
The type argument is a pointer to a null-terminated string which must contain either the letter 'r' for reading or the letter 'w' for writing. Since glibc 2.9, this argument can additionally include the letter 'e', which causes the close-on-exec flag (FD_CLOEXEC) to be set on the underlying file descriptor; see the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.
//type 参数是一个指向以 null 结尾的字符串的指针,该字符串必须包含用于读取的字母“r”或用于写入的字母“w”。
//从 glibc 2.9 开始,这个参数还可以包含字母 'e',这会导致在底层文件描述符上设置 close-on-exec 标志(FD_CLOEXEC);
//请参阅 open(2) 中对 O_CLOEXEC 标志的说明,以了解这可能有用的原因。
The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(3). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that called popen(), unless this is altered by the command itself. Conversely, reading from the stream reads the command's standard output, and the command's standard input is the same as that of the process that called popen().
//popen() 的返回值在所有方面都是一个普通的标准 I/O 流,除了它必须用 pclose() 而不是 fclose(3) 关闭。
//写入这种流会写入命令的标准输入;
//该命令的标准输出与调用 popen() 的进程的标准输出相同,除非它被命令本身更改。
//反之,从流中读取是读取命令的标准输出,命令的标准输入与调用popen()的进程是一样的。
Note that output popen() streams are block buffered by default.
//请注意,默认情况下,输出 popen() 流是块缓冲的。
The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4(2).
//pclose() 函数等待关联的进程终止并返回由 wait4(2) 返回的命令的退出状态。
RETURN VALUE
The popen() function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory.
The pclose() function returns -1 if wait4(2) returns an error, or some other error is detected. In the event of an error, these functions set errno to
indicate the cause of the error.
//如果 fork(2) 或 pipe(2) 调用失败,或者无法分配内存,popen() 函数将返回 NULL。
//如果 wait4(2) 返回错误或检测到其他错误,则 pclose() 函数返回 -1。 如果发生错误,这些函数会设置 errno 以指示错误的原因。
ERRORS
The popen() function does not set errno if memory allocation fails. If the underlying fork(2) or pipe(2) fails, errno is set appropriately. If the type
argument is invalid, and this condition is detected, errno is set to EINVAL.
If pclose() cannot obtain the child status, errno is set to ECHILD.
//如果内存分配失败,popen() 函数不会设置 errno。
//如果底层 fork(2) 或 pipe(2) 失败,则正确设置 errno。
//如果 type 参数无效,并且检测到此条件,则将 errno 设置为 EINVAL。
//如果 pclose() 无法获取子状态,则将 errno 设置为 ECHILD。
ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
//有关本节中使用的术语的解释,请参见属性 (7)。
┌──────────────────┬───────────────┬─────────┐
│Interface │ Attribute │ Value │
├──────────────────┼───────────────┼─────────┤
│popen(), pclose() │ Thread safety │ MT-Safe │
└──────────────────┴───────────────┴─────────┘
CONFORMING TO
POSIX.1-2001, POSIX.1-2008.
The 'e' value for type is a Linux extension.
BUGS
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process has done a
buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with
that of the original process. The latter can be avoided by calling fflush(3) before popen().
Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The only hint is an
exit status of 127.
//由于打开用于读取的命令的标准输入与调用 popen() 的进程共享其查找偏移量,因此如果原始进程进行了缓冲读取,则命令的输入位置可能与预期不同。
//类似地,打开用于写入的命令的输出可能会与原始进程的输出混合。
//后者可以通过在 popen() 之前调用 fflush(3) 来避免。
//执行 shell 失败与 shell 执行命令失败或命令立即退出没有区别。
//唯一的提示是退出状态 127。
SEE ALSO
sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3), stdio(3), system(3)
COLOPHON
This page is part of release 4.04 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version
of this page, can be found at http://www.kernel.org/doc/man-pages/.
GNU 2015-08-08 POPEN(3)
Manual page popen(3) line 38/79 (END) (press h for help or q to quit)
开发者涨薪指南
48位大咖的思考法则、工作方式、逻辑体系
以上是关于C语言popen()函数 pclose()函数(打开关闭流入或流出进程的管道流)的主要内容,如果未能解决你的问题,请参考以下文章