Do streams have to be closed when using popen

本文探讨了使用popen打开流后是否需要关闭的问题,并详细解释了pclose的工作原理及如何正确处理错误返回值。文章通过glibc源代码分析揭示了popen与pclose之间的内部机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来自Stackoverflow

Do streams have to be closed when using popen

病例样本:


As the title says , I am unsure if I should close a stream that was opened using popen.

The reason I am unsure is because every time i call pclose on a stream that was opened using popen I get a -1 return code.

If I make a call to perror after that I get the following message.

pclose: No Child Processes

The code that I am using below is basically to run a command and capture its output.I get the error from the last line (return pclose(fileListingStream);)

int executeCommand(char *command) {
    //The Stream to read that will contain the output of the command
    FILE *fileListingStream;
    char path[PATH_MAX];

    //Run the commmand in read mode
    fileListingStream = popen(command,"r");

    //Ensure that its not null before continuing
    if (fileListingStream == NULL)
        return EXIT_FAILURE;

    //Get the data from the stream and then print to the the console
    while (fgets(path, PATH_MAX, fileListingStream) != NULL)
        printf("%s", path);

    //Close the stream and return its return code
    return pclose(fileListingStream);
}

回复:

- Does your program ignore or handle SIGCHLD by any chance? –  caf Nov 30 '10 at 1:31

- Yes you should. See this answer for an explanation on the inner workings of pclose(). Furthermore you should note that errors in wait4() can be the cause of an apparent failure in pclose().

If the FILE * is valid (internally this is signified by the file descriptor not being -1), pclose() and fclose() will not cause leaks if there is an error. It's worth noting that if the FILE * is not valid, there's nothing to clean up anyway. As discussed in the answer I linked to, there is extra behaviour for pclose(), namely removing the FILE * from the proc file chain, and then waiting for the child to terminate. The internal wait is actually the second last thing done for a pclose(), everything has already been cleaned up by this point. Immediately after waiting, the contents of the FILE are trashed to signify its invalidity, this occurs regardless of any error in waitpid().

Given the error you are receiving, ECHILD, I can definitively say that there is no memory leak for pclose() under eglibc-2.11.1, and likely any glibc-derived library for at least the past 1-4 years.

If you wish to be completely certain, just run your program under valgrind, and trigger the ECHILD error. Valgrind will inform you if anything was leaked.

- Do I still need to worry about memory leaks if the pclose fails ? , also what else can I do to close the file. –  RC1140 Nov 29 '10 at 15:37

-That error is ECHILD - meaning that waitpid returned an error, it could not get the return status of the child process. That can be caused by what 'command' actually int erms of process creation.

As every one else mentioned you should pclose, because it leaves a 'dangling' file descriptor, and can cause child processes not to be cleaned up promptly.

-If your application modify the disposition of SIGCHLD, it could interfere with popen()'s ability to wait for the shell to exit.


-URL:this answer

Disappointed with the generality of the answers so far (I can RTFM, tyvm), I've investigated this thoroughly, by stepping through and reading the glibc source.

In glibc pclose() directly calls fclose() with no additional effect, so the 2 calls are same. In fact you could use pclose() and fclose() interchangeably. I'm sure this is purely a coincidence in the evolved implementation, and the use of pclose() to close a FILE * returned from popen() is still recommended.

The magic is in popen(). FILE *s in glibc contain a jump table with pointers to appropriate functions to handle such calls as fseek(), fread(), and of relevance fclose(). When calling popen(), a different jump table used than the one used by fopen(). The close member in this jump table points to a special function _IO_new_proc_close, which calls waitpid() on the pid stored in the region pointed to by FILE *.

Here's the relevant call stack in my version of glibc, which I've annotated with notes about what is going on:

// linux waitpid system call interface
#0  0x00f9a422 in __kernel_vsyscall ()
#1  0x00c38513 in __waitpid_nocancel () from /lib/tls/i686/cmov/libc.so.6

// removes fp from a chain of proc files
// and waits for the process of the stored pid to terminate
#2  0x00bff248 in _IO_new_proc_close (fp=0x804b008) at iopopen.c:357

// flushes the stream and calls close in its jump table
#3  0x00c09ff3 in _IO_new_file_close_it (fp=0x804b008) at fileops.c:175

// destroys the FILEs buffers
#4  0x00bfd548 in _IO_new_fclose (fp=0x804b008) at iofclose.c:62

// calls fclose
#5  0x00c017fd in __new_pclose (fp=0x804b008) at pclose.c:43

// calls pclose
#6  0x08048788 in main () at opener.c:34

So the short of it is, using popen(), the returned FILE * must not be closed, even if you dup() its file descriptor, because it will block until the child process terminates. Of course, after this you'll be left with a file descriptor to a pipe which will contain whatever the child process managed to write() to it before terminating.

By not fread()ing with the file pointer returned from popen(), the underlying pipe will not be touched, it's safe to use the file descriptor by fileno(), and finish up by calling pclose().



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值