LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-29-2011, 01:24 AM   #1
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Rep: Reputation: 0
Angry Help...!!! Popen() hang issue


hi,

iam excuting a scripts in multi-threaded environment

iam using popen()to run my script in a thread and iam waiting for my popen() to exit gracefully (using pclose())

in the script i entered a invalid command the script exited my printing some message in stderr
but my pclose() is still waiting to take script exit status...!
how can solve this issue..?!

here is my code
scriptt3.sh
exit 5 5 //invalid command

int mythread3(void*p)
{
FILE *Fp;
int status;
Fp=popen("./script3.sh","r");
status=pclose(Fp); //hanging here
printf("done\n");

if (WIFEXITED(status))
{
printf("t3exited, status=%d\n", WEXITSTATUS(status));
}
else if (WIFSIGNALED(status))
{
printf("t3killed by signal %d\n", WTERMSIG(status));
}
else if (WIFSTOPPED(status))
{
printf("t3stopped by signal %d\n", WSTOPSIG(status));
}
return 0;
}

what i can do to make it work fine..? i have to catch that exit status of script (although it is terminated by shell)

if you have any suggestion please share with me

thanks in adv

~shankar
 
Old 03-30-2011, 11:35 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,478

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
You really need to read in a loop. popen() simply opens a pipe as a file descriptor.

So,
Code:
#include <stdio.h>
#include <stdlib.h>

int     main    (void)
{
        int     c;
        FILE    *in;

        /*      open the input pipe                     */
        if ((in = popen ("cat script3.sh", "r")) == (FILE *) NULL) {
                (void) fprintf (stderr, "popen failed\n");
                exit (EXIT_FAILURE);
        }
        while ((c = getc (in)) != EOF) {
                /*
                 *      do something with what you just read
                 *      here, we're just going to write to stdout
                */
                putc (c, stdout);
        }
        /*      close the input pipe                    */
        (void) pclose (in);
        exit (EXIT_SUCCESS);
}
This will read a byte at a time from the input pipe and write it on stdout (your terminal). When you run out of input, the while will exit and the pipe will be closed.

Hope this helps some.
 
Old 03-31-2011, 12:38 AM   #3
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
hi tronayne,

thanks for you reply

it helped me a lot

may i know the reason why it is hanging..?!

thank you
~shankar

Last edited by shankar.489; 03-31-2011 at 12:39 AM.
 
Old 03-31-2011, 08:51 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,478

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Keep in mind the purpose of a pipe, whether in the world or in software. A pipe just sits there doing nothing until something is put into it. So, Fp=popen("./script3.sh","r"); just opens but doesn't do anything.

The purpose of popen() is to give a command line to the shell for execution, connecting its input or output to the program; thus you want to "cat filename" rather than just "filename."

If you have a large compressed data file (compressed with gzip, bzip2 or even zip), you do not need to uncompress the file to be able to do something with the data. You can simply
Code:
        /*      open the input pipe                     */
        if ((in = popen ("zcat filename.gz", "r")) == (FILE *) NULL) {
                (void) fprintf (stderr, "popen failed\n");
                exit (EXIT_FAILURE);
        }
and that will work just fine.

You may want to adopt the method shown so that your program will fail gracefully if "filename" does not exist, is not readable or some other problem exists. Better to know there's a problem before wasting a lot of time and resources trying to process something that doesn't exist, eh?

Hope this helps some.
 
Old 04-06-2011, 02:32 AM   #5
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Original Poster
Rep: Reputation: 0
Wink

Quote:
Originally Posted by tronayne View Post
Keep in mind the purpose of a pipe, whether in the world or in software. A pipe just sits there doing nothing until something is put into it. So, Fp=popen("./script3.sh","r"); just opens but doesn't do anything.

The purpose of popen() is to give a command line to the shell for execution, connecting its input or output to the program; thus you want to "cat filename" rather than just "filename."

If you have a large compressed data file (compressed with gzip, bzip2 or even zip), you do not need to uncompress the file to be able to do something with the data. You can simply
Code:
        /*      open the input pipe                     */
        if ((in = popen ("zcat filename.gz", "r")) == (FILE *) NULL) {
                (void) fprintf (stderr, "popen failed\n");
                exit (EXIT_FAILURE);
        }
and that will work just fine.

You may want to adopt the method shown so that your program will fail gracefully if "filename" does not exist, is not readable or some other problem exists. Better to know there's a problem before wasting a lot of time and resources trying to process something that doesn't exist, eh?

Hope this helps some.
thanks for your info.
 
  


Reply

Tags
[c]


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
Issue with PCI controller causing udevadm to hang? ankur655 Linux - Hardware 1 07-08-2009 02:36 PM
Fedora 9 %1 Boot Hang Issue GridLock88 Fedora 1 12-24-2008 05:27 AM
Grub issue of HD failure (System Hang on HP Omnibook 600) shortname Linux - Hardware 2 04-26-2005 10:00 AM
strange xfree hang issue - gentoo 1.4r5 w/ 2.4.20 kernel digital_usa Linux - Software 4 01-19-2004 10:26 PM
RH 9 install hang at boot: possible raid driver issue jamespf Red Hat 1 09-15-2003 07:58 AM


All times are GMT -5. The time now is 01:07 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