LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2011, 08:57 AM   #1
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Rep: Reputation: 0
Question Help in getting return status of popen sys call


Hi folks,

i using Popen() sys call in my code to run a script.

i know it will return file pointer. but requirement is some how i need to get return status of process which is invoked by popen.

is there any possibility to get its status, if so please let me know.

FYI
i cant use system() here becoz of some limitations

thanks in adv

~shankar
 
Old 03-23-2011, 05:57 PM   #2
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Lenny
Posts: 351

Rep: Reputation: 47
Do you want to use it for reading or writing ?
popen can use it for reading or writing and not both. What I usually do in these situations is to ask the program to print the return value to the STDOUT when run from shell in the end and when I have a read pipe, I get the last line of the output and then get the value

Eg:- your program a.out

use peopen to open "./a.out;echo $?"
this will run the output and also echo the return value in the end which you can read in the read popen stream and hence dervice the result
(nasty solution but works ... :-( ). You can always hide the output and decide what to show and what not to :-)
 
Old 03-23-2011, 11:43 PM   #3
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
hi folks,

i came across this solution while searching in internet.

if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, BUFSIZ, ptr) != NULL)
(void) printf("%s", buf);
stat = pclose(ptr);
printf("The exit status is: %d\n", WEXITSTATUS(stat));


is there any limitation with this thing if so please let me know.

thankyou in adv

~shankar
 
Old 03-27-2011, 05:35 PM   #4
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Lenny
Posts: 351

Rep: Reputation: 47
As long as there are no other threads this WEXITSTATUS will work fine.
 
Old 03-28-2011, 12:37 AM   #5
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
hi bhasker,

i didnt get you completly.is there any problem with this approch? if so please let me know.


~shankar
 
Old 03-28-2011, 03:04 PM   #6
bhaslinux
Member
 
Registered: Oct 2003
Location: UnitedKingdom
Distribution: Debian Lenny
Posts: 351

Rep: Reputation: 47
WEXITSTATUS will give the return status of last exited child. if your program you are writing is single threaded, then the peopen will be the only child and the WEXITSTATUS will give you the return value. If there is one more thread/child which exits before the popen ends and you call WEXITSTATUS then the result of your peopen will get overwritten.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ popen return EOF second time aw_wolfe Programming 3 09-01-2008 09:46 PM
question about select sys call xatzipol Programming 1 10-30-2005 09:22 AM
Any sys call to list all users ? dileepkk Linux - General 5 07-06-2004 06:04 PM
Kill sys call eshwar_ind Programming 4 05-07-2004 11:41 AM
any sys call to get ip address of NIC jmdey Linux - General 2 05-30-2002 03:09 PM


All times are GMT -5. The time now is 07:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration