Questions tagged [pclose]

In C, the pclose() function closes a stream that was opened by popen().

In C, the pclose() function closes a stream that was opened by popen(), waits for the command to terminate, and returns the termination status of the process that was running the command interpreter. However, if a call caused the termination status to be unavailable to pclose(), then pclose() returns -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 the pid argument of -1 or equal to the process ID of the command interpreter.

In any case, pclose() does not return before the child process created by popen() terminates.

If some error prevents the command interpreter from executing after the child process is created, the return value from pclose() is as if the command language interpreter had terminated using exit(127) or _exit(127).

Source: Man page for pclose() -- close pipe stream at mkSoftware

33 questions
1
vote
1 answer

Why does this pclose() implementation return early with ECHILD unless invocation is delayed after popen()?

I recently wanted to get my hands dirty with understanding how to fork/exec a child process and redirecting stdin, stdout, and stderr thereof, by way of which I wrote my own popen() and pclose()-like functions named my_popen() and my_pclose(),…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
1
vote
0 answers

Why does pclose return prematurely?

UPDATE 1: This question has been updated to eliminate the multithreading, simplifying its scope. The original problem popened in the main thread, and pclosed the child process in a different thread. The problem being asked about is reproducible much…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
1
vote
1 answer

Segmentation fault when pclose after fscanf

I try to get lost packets number (from ping command on raspberry) which I use for sim switching on router. I tried to do that with code at the bottom (reduced to error causing part), but when I try to free (pclose) buffer I get segmentation fault.…
Bartosz
  • 13
  • 2
1
vote
0 answers

Segmentation fault after realloc

this is my code: void Available_firmware_version(){ char *version = (char *) malloc(2); strcpy(version, ""); FILE *fp; int status = 0; char path[PATH_MAX]; fp = popen("swift list Manto", "r"); if (fp == NULL){ printf("Error: Popen is NULL…
1
vote
1 answer

Why the pclose(3) don't wait shell command terminate

I want to test the pclose(3) whether it will wait the shell command terminate.I write two little shell program. //a.sh #!/bin/bash sleep 3 //b.sh #!/bin/bash echo "something" sleep 3 c program: //ptest.c #include #include…
yuxing
  • 47
  • 1
  • 6
1
vote
1 answer

how to run php as background process

I have a problem when executing the PHP files as background process. I have two PHP file as follow : index.php
0
votes
1 answer

PHP on Windows, how to run a script in the background?

I've tried all these commands: $cmd = "start /B php $rootPath\\$scriptName "; $cmd = "php $rootPath\\$scriptName > NUL 2>&1 &"; $cmd = "php $rootPath\\$scriptName "; With these PHP functions: pclose(popen($cmd, 'r')); exec($cmd); And in all cases…
Jodes
  • 14,118
  • 26
  • 97
  • 156
0
votes
0 answers

C++ Windows _pclose return value: _cwait needed?

I want to get the exit status of a pipe in C++, both on Linux and on Windows, to check whether a command ran successfully. On Linux (or POSIX more generally), it appears that the macros in are needed to get the correct exit status, such…
RL-S
  • 734
  • 6
  • 21
0
votes
0 answers

Is there any problem with popen stream to be closed after fdopen

I'm seeing a strange issue. Sample code is included below When this code is run with valgrind, it complains that the memory allocated with popen is still reachable. Should i worry about this warning? If yes, what is a possible solution? …
0
votes
0 answers

popen, pclose and getdelim functions not included in Turbo C library

This question is pretty straightforward.... The pipe functions (popen, and pclose) and getdelim functions are not included in the Borland Turbo C Libraries I'm using. I'm unable to compile any code involving these functions. MinGw's Library includes…
sagar_02
  • 69
  • 6
0
votes
1 answer

Pclose seems to make process fail

This question is a follow up of this question : Controlling a C daemon from another program My goal is to control daemon process execution from another program. The daemon's code is really simple. int main() { printf("Daemon starting ...\n"); …
Arkaik
  • 852
  • 2
  • 19
  • 39
0
votes
2 answers

When to call _pclose?

I have a function in which I call _open a few times. If _popen returns NULL do I need to call _pclose before the function returns? I've marked 3 locations where I think _pclose might need to be called. At which of these locations must I call…
user3731622
  • 4,844
  • 8
  • 45
  • 84
0
votes
1 answer

Can't Run a background PHP Script With Popen

I have a PHP code that needs to run in background (no cronjobs or similar options) that is complete, but I just can't call it in the background. I'm trying the following: $files = $_GET['files']; $id = $_GET['id']; pclose(popen("start /B…
Metal Sonic
  • 61
  • 1
  • 8
0
votes
1 answer

why same output is returned when popn() is calling again and again

I am trying below example #include #include #include #include #include std::string exec(char* cmd, boost::uint16_t *piOutVid, boost::uint16_t *piOutPid) { …
Dig The Code
  • 658
  • 2
  • 15
  • 32
0
votes
1 answer

PHP - Windows background commands working but still waiting on background process to finish

I have tried NUMEROUS methods to start a background php script from another php script. I need to create a zip file of database files for download. I have it set up to download instantly for single files, but if the files need to be zipped, I want a…
Monkeybutt
  • 15
  • 5