473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

popen: how to test for end of program

I am trying to write a program which uses popen but I have a problem. I
want to detect if the program I call with popen has ended. For example:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
FILE *pipe = NULL;
int i=0;

while(1) {
if (i == 10) {
pipe = popen("ls", "w");
}

if(pipe != NULL && feof(pipe)) {
printf("Program has ended\n");
}

i++;
usleep(1000);
}
}

This doesn't work.

Does anyone know how to test if the program has ended ?

Thanks,
Bastiaan

Nov 14 '05 #1
17 5185
ba***********@gmail.com wrote:
# I am trying to write a program which uses popen but I have a problem. I
# want to detect if the program I call with popen has ended. For example:

You don't have that much control with popen/pclose. The available stdout has been
exhausted when you get an EOF; the child has exitted when pclose returns. If you
need finer level control, you have to do the fork(), pipe(), exec*(), and
wait*() calls yourself.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who's leading this mob?
Nov 14 '05 #2
ba***********@gmail.com wrote:

I am trying to write a program which uses popen but I have a problem. I
want to detect if the program I call with popen has ended. For example:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
.... snip ...
Does anyone know how to test if the program has ended ?


Since nothing you mention, apart from <stdio.h>, exists in the
portable standard C language, the answer is NO, nobody here knows.
However, if you were to move to a newsgroup where those things are
topical, possibly with unix in its name, the answer might be
different.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

Nov 14 '05 #3
ba***********@gmail.com wrote:
I am trying to write a program which uses popen but I have a problem. I
want to detect if the program I call with popen has ended. For example: #include <stdio.h>
#include <sys/types.h>
#include <unistd.h> int main()
{
FILE *pipe = NULL;
int i=0; while(1) {
if (i == 10) {
pipe = popen("ls", "w");
}

if(pipe != NULL && feof(pipe)) {
printf("Program has ended\n");
}

i++;
usleep(1000);
}
} This doesn't work. Does anyone know how to test if the program has ended ?


Probably, but since it's off-topic here in clc most people won't
discuss it here since it's not a C problem - standard C doesn't
know about other programs running at the same time or about func-
tions like popen(). So you better ask these kinds of questions
in a group like comp.unix.programmer, where it's on-topic (but
also there you probably will get told that you hardly can get
that to work with popen() and that you will need to use fork()
and one of the exec()-family of functions - and that you need
"r" as the second argument to popen() when you it with "ls",
ls is supposed to create output for you to read, isn't it?).

But there's also a C related problem with your program: feof()
won't tell you anything reasonable before you actually have read
something from a file. feof() can't predict if EOF will get re-
turned on the next read, it only can tell you afterwards that
this was the reason why a read from the file failed. So, even if
your program spawned by using popen() already died and closed its
side of the pipe, just calling feof() won't help you a bit since
you never read anything, and only that (plus emptying the pipe)
could lead to feof() returning something other than 0.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
On Thu, 12 May 2005 15:12:10 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
ba***********@gmail.com wrote:
# I am trying to write a program which uses popen but I have a problem. I
# want to detect if the program I call with popen has ended. For example:

You don't have that much control with popen/pclose. The available stdout has been
exhausted when you get an EOF; the child has exitted when pclose returns. If you
need finer level control, you have to do the fork(), pipe(), exec*(), and
wait*() calls yourself.


Please do the OP the service of directing him to a newsgroup where
this question is topical. You can answer it there, and he can get
input and review from others as well.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #5
# Please do the OP the service of directing him to a newsgroup where
# this question is topical. You can answer it there, and he can get
# input and review from others as well.

Maybe Chris can divulge the secret answer.

# From: t...@elf.ee.lbl.gov (Chris Torek)
# Newsgroups: comp.unix.questions,comp.lang.c
# Subject: Re: UNIX commands in C
# Message-ID: <12***@dog.ee.lbl.gov>
# Date: 29 Apr 91 20:15:41 GMT
# References: <19********************@jack.sns.com> <GL*****************@curie.ces.cwru.edu> <17*@shasta.Stanford.EDU>
# Reply-To: t...@elf.ee.lbl.gov (Chris Torek)
# Organization: Lawrence Berkeley Laboratory, Berkeley
# Lines: 21
# X-Local-Date: Mon, 29 Apr 91 13:15:41 PDT
#
# (Having just written a new man page for popen, I cannot resist following
# up, even though I should be off getting some food....)
#
# In article <1...@shasta.Stanford.EDU> s...@shasta.Stanford.EDU (shap) writes:
# >popen(3) also has an interesting "feature." ... Et Voilla! instant deadlock.
#
# This cannot happen directly because of popen(). The reason is
# trivially simple: popen opens either for reading or for writing, never
# both. Thus popen() is unable to create a loop, and deadlock occurs
# only in the presence of a pipe loop. (The situation described in `...'
# is the simplest case, where A writes to B and then reads back from B:
# A->B->A. The problem also occurs in longer loops such as A->B->C->D->A.)
#
# >For those of you who don't believe this happens, talk to some people
# >who have had to port various window managers.
#
# These generally use bidirectional entities such as ptys and must
# therefore provide their own synchronization mechanisms.
# --
# In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
# Berkeley, CA Domain: t...@ee.lbl.gov

--
SM Ryan http://www.rawbw.com/~wyrmwif/
You face forward, or you face the possibility of shock and damage.
Nov 14 '05 #6
On Fri, 13 May 2005 14:11:50 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
# Please do the OP the service of directing him to a newsgroup where
# this question is topical. You can answer it there, and he can get
# input and review from others as well.

Maybe Chris can divulge the secret answer.

# From: t...@elf.ee.lbl.gov (Chris Torek)
# Newsgroups: comp.unix.questions,comp.lang.c
# Subject: Re: UNIX commands in C
# Message-ID: <12***@dog.ee.lbl.gov>
# Date: 29 Apr 91 20:15:41 GMT

<remainder snipped>

I have no idea of the intent or relevance of your remark, or the
purpose of quoting a 14 year old, cross-posted message from Chris
Torek.

Please set your mail client to use the conventional quote character.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #7
SM Ryan wrote:

# Please do the OP the service of directing him to a newsgroup where
# this question is topical. You can answer it there, and he can get
# input and review from others as well.

Maybe Chris can divulge the secret answer.


PLONK for idiocy, topposting, bad quote marks, general annoyance.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #8
CBFalconer <cb********@yahoo.com> wrote:
# PLONK for idiocy, topposting, bad quote marks, general annoyance.

How many times are you going to have plonk before it sticks? You need
to repair your killfile.

Nov 14 '05 #9
Alan Balmer <al******@att.net> wrote:
# On Fri, 13 May 2005 14:11:50 -0000, SM Ryan
# <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
#
# ># Please do the OP the service of directing him to a newsgroup where
# ># this question is topical. You can answer it there, and he can get
# ># input and review from others as well.
# >
# >Maybe Chris can divulge the secret answer.
# >
# ># From: t...@elf.ee.lbl.gov (Chris Torek)
# ># Newsgroups: comp.unix.questions,comp.lang.c
# ># Subject: Re: UNIX commands in C
# ># Message-ID: <12***@dog.ee.lbl.gov>
# ># Date: 29 Apr 91 20:15:41 GMT
# <remainder snipped>
#
# I have no idea of the intent or relevance of your remark, or the

Your pretence of ignorance is amusing. You really don't want to let the
viewers
at home realise the little coup d'etat of comp.lang.c, overthrowing a
once useful
newsgroup, making it the home of a obnoxious clique that is perfectly
willing
to rant (on and on and) on plainly off-topic issues.

# Please set your mail client to use the conventional quote character.

Explain to the new newsgroup the RFC that defined quote characters and
the
importance of using Usenet-approved response formats.

Nov 14 '05 #10
"SM Ryan" <sm****@acm.org> writes:
Alan Balmer <al******@att.net> wrote: [...] # Please set your mail client to use the conventional quote character.

Explain to the new newsgroup the RFC that defined quote characters and
the
importance of using Usenet-approved response formats.


As far as I know, there is no RFC that specifies what quote character
to use. That's not the point. "> " is the *conventional* prefix.
Your use of "# " causes problems for some of your readers. Your use
of long lines (significantly longer than 72 columns, sometimes longer
than 80 columns) causes even more problems.

For example, I read news in an 80-column window. My newsreader lets
me reformat an article to fit; it even adjusts "> " quoting characters
as it reformats the text. But it doesn't recognize '#' as a quoting
character. Perhaps that's a flaw in my newsreader, but it's an
understandable one, since hardly anybody uses '# as a quoting
character.

I've asked you before why you persist in doing this. I'd ask you
again, but you probably won't answer. I see no benefit to it, unless
you're deliberately trying to annoy people.

BTW, I just noticed that you added a cross-post to alt.usenet.kooks;
I've deleted it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
On 13 May 2005 23:01:06 -0700, in comp.lang.c , "SM Ryan"
<sm****@acm.org> wrote:
CBFalconer <cb********@yahoo.com> wrote:
# PLONK for idiocy, topposting, bad quote marks, general annoyance.

How many times are you going to have plonk before it sticks? You need
to repair your killfile.


okay, I'll plonk you too, you're being too dang annoying now.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #12
Mark McIntyre wrote:
"SM Ryan" <sm****@acm.org> wrote:
CBFalconer <cb********@yahoo.com> wrote:

# PLONK for idiocy, topposting, bad quote marks, general annoyance.

How many times are you going to have plonk before it sticks? You
need to repair your killfile.


okay, I'll plonk you too, you're being too dang annoying now.


Well, your quote showed that the PLONK has taken effect properly.
For general information, about once every year or two I clear my
killfile, both to reduce the filtering work and to give reformed
nuisance idiots a chance. Ryan obviously has not reformed.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #13
schlectersamen <un****@fauna.start> writes:
[snip]
Feel free to start screaming any time now.


Please keep this out of comp.lang.c. Thank you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14
schlectersamen wrote:
Keith Thompson <ks***@mib.org> wrote in
schlectersamen <un****@fauna.start> writes:
[snip]
Feel free to start screaming any time now.


Please keep this out of comp.lang.c.


Request denied.


PLONK

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #15

CBFalconer wrote:
# schlectersamen wrote:
# > Keith Thompson <ks***@mib.org> wrote in
# >> schlectersamen <un****@fauna.start> writes:
# >> [snip]
# >>> Feel free to start screaming any time now.
# >>
# >> Please keep this out of comp.lang.c.
# >
# > Request denied.

Now you've done it. You got yourself a really big

# PLONK

all caps and everything. Watch out or next time he'll pull
out the big guns. Like trying to play with the followup-to
header.

Nov 14 '05 #16
On 13 May 2005 23:34:44 -0700, "SM Ryan" <sm****@acm.org> wrote:
Alan Balmer <al******@att.net> wrote:
# On Fri, 13 May 2005 14:11:50 -0000, SM Ryan
# <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
#
# ># Please do the OP the service of directing him to a newsgroup where
# ># this question is topical. You can answer it there, and he can get
# ># input and review from others as well.
# >
# >Maybe Chris can divulge the secret answer.
# >
# ># From: t...@elf.ee.lbl.gov (Chris Torek)
# ># Newsgroups: comp.unix.questions,comp.lang.c
# ># Subject: Re: UNIX commands in C
# ># Message-ID: <12***@dog.ee.lbl.gov>
# ># Date: 29 Apr 91 20:15:41 GMT
# <remainder snipped>
#
# I have no idea of the intent or relevance of your remark, or the

Your pretence of ignorance is amusing. You really don't want to let the
viewers
at home realise the little coup d'etat of comp.lang.c, overthrowing a
once useful
newsgroup, making it the home of a obnoxious clique that is perfectly
willing
to rant (on and on and) on plainly off-topic issues.

# Please set your mail client to use the conventional quote character.

Explain to the new newsgroup the RFC that defined quote characters and
the
importance of using Usenet-approved response formats.


Bye, now. Have a good life after your recovery.

<Plonk>

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #17
On Sat, 14 May 2005 16:22:39 GMT, CBFalconer <cb********@yahoo.com>
wrote:
Mark McIntyre wrote:
"SM Ryan" <sm****@acm.org> wrote:
CBFalconer <cb********@yahoo.com> wrote:

# PLONK for idiocy, topposting, bad quote marks, general annoyance.

How many times are you going to have plonk before it sticks? You
need to repair your killfile.


okay, I'll plonk you too, you're being too dang annoying now.


Well, your quote showed that the PLONK has taken effect properly.
For general information, about once every year or two I clear my
killfile, both to reduce the filtering work and to give reformed
nuisance idiots a chance. Ryan obviously has not reformed.


Apparently, he's afraid to give his advice in a topical newsgroup when
there are other subject experts watching.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: M | last post by:
Hi Folks, I would like to use popen and redirect output from a program, except it doesn't seem to work. I am using IIS 5.1 on Windows XP Pro. Error messages are as follows: Warning:...
9
by: Bryan | last post by:
i have a batch file that contains these two lines: -- args.bat echo %1 echo %2 when i run args.bat from the command line it works correctly... notice that abc has quotes in the first...
0
by: I. Myself | last post by:
I want my Python program to invoke a compiled C program, and capture the text output. Here's a fragment from a program that works, using subprocess.Popen: p = Popen(execName, stdout=PIPE)...
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
4
by: zane.selvans | last post by:
Hi there, I've been banging my head against this for a day, and I can't take it anymore. It's probably a stupid error, but I don't see where. I'm trying to use Python to call an external...
15
by: Daniel Klein | last post by:
I'm trying to get popen to work on Windows. Here's a simplified example of what I'm trying to get working: I have a hw.c program as follows: #include <stdio.h> main() { printf ("Hello...
11
by: John Nagle | last post by:
I passed a dict for the "env" variable to Popen with Unicode strings for the dictionary values. Got: File "D:\Python24\lib\subprocess.py", line 706, in _execute_child TypeError: environment...
7
by: skunkwerk | last post by:
Hi, i'm trying to call subprocess.popen on the 'rename' function in linux. When I run the command from the shell, like so: rename -vn 's/\.htm$/\.html/' *.htm it works fine... however when I...
8
by: clyfish | last post by:
In cmd, I can use find like this. C:\>netstat -an | find "445" TCP 0.0.0.0:445 0.0.0.0:0 LISTENING UDP 0.0.0.0:445 *:* C:\> And os.system is OK....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.