Do I need to close streams when using popen

As the name says, I'm not sure if I should close the stream that was opened with popen.

I am not sure, because every time I call pclose on a thread that was opened with popen, I get a return code of -1.

If I make a perror call after this, I get the following message.

pclose: no child processes

The code that I use below is mainly for running the command and its output. I get an 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);
}
+3
source share
3 answers

, . . pclose(). , , wait4() pclose().

Update0

FILE * ( , -1), pclose() fclose() . , FILE * , . , , pclose(), FILE * proc, . , , pclose(), . FILE , , - waitpid().

, ECHILD, , pclose() eglibc-2.11.1 , , , glibc, 1-4 .

, valgrind ECHILD. Valgrind , - .

+5

SIGCHLD, popen() .

+2

ECHILD - , waitpid , . , "" .

, pclose, "" .

0

Source: https://habr.com/ru/post/1777468/


All Articles