popen hangs program during cmd execution


 
Thread Tools Search this Thread
Top Forums Programming popen hangs program during cmd execution
# 1  
Old 01-22-2010
popen hangs program during cmd execution

How can I get around this? when my program reaches the following popen job it halts the program until the ping/netstat/ipconfig/traceroute is completed then resume to the rest of the program...

Code:
FILE *in;
  extern FILE *popen();
  char buff[1024];
  char newline[100];
  char nstat[1024];
  char nping[1024];
  char tracert[2048];
  char iconfig[1024];
  strcpy(newline, "\r\n");  



  if (!(in = popen("ping -c 3 www.primus.ca ", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(nping, buff);
    }

  strcat(nping, newline); 
 

  pclose(in);

So I figured I could run the process with no hangup and in background so I went

Code:
popen("nohup ping -c 3 www.primus.ca &", "r")

It still halt the program until the ping is completed... the only difference im getting is in the meantime the program is halted I get

nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
nohup: redirecting stderr to stdout
# 2  
Old 01-22-2010
Sorry, am not clear what you are doing.

Are you trying to stream the output of ping as and when it completes 1 round trip. Is this something to be extended to multiple servers running ping in parallel as a background job?
# 3  
Old 01-22-2010
You are getting error output from ping - change the command
Code:
ping -c 3 www.primus.ca 2>&1 
# or if you don't want errors
ping -c 3 www.primus.ca > /dev/null

When you have a subprocess running it generates a system signal (which is being trapped by popen and reported). The signal is SIGTTOU. You cannot use popen without trapping stderr output or blocking signals.
# 4  
Old 01-23-2010
Sorry I should of explained myself better.

I anticipated that the commands would take some time to completed and store the command outputs in the arrays so I put the menu after and made some dummy progress bar that are executed upon chosing the NETWORK TEST option... to allow enough time for the ping/netstat/ipconfig/traceroute to be completed BEFORE the program tries to send the stored command output in the arrays through the socket via smtp to my email.

Problem is that when I execute the program... it takes like 1 minute before the menu appear because the menu doesn't appear until the 4 commands are completed... And I need the menu to appear right upon execution...and these commands to be run in the background WHILE the Im chosing one of the menu option then 4 progress bars comes up and ensure that the tests had enough time to complete then after the 4 progress bar are completed the socket is initiated and the content of the arrays are pushed through it to a smtp and then ends up in my email...

How can I get these commands to be run in the "background" so that the menu appears upon execution of the program?

Code:
void error(char *msg)
{
    perror(msg);
    exit(0);
}


double menu(); 
char id1[4];
char amail[20] = "@primus.ca\r\n";
char id2[20];
char rcp[20] = "RCPT TO: ";


int a;

int main(int argc, char *argv[])
{

 
  FILE *in;
  extern FILE *popen();
  char buff[1024];
  char newline[100];
  char nstat[1024];
  char nping[1024];
  char tracert[2048];
  char iconfig[1024];
  strcpy(newline, "\r\n");  



  if (!(in = popen("ping -c 3 www.primus.ca 2>&1", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(nping, buff);
    }

  strcat(nping, newline); 
 

  pclose(in);



  if (!(in = popen("netstat -wan 2>&1", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(nstat, buff);
    }

  strcat(nstat, newline); 
 

  pclose(in);



  if (!(in = popen("ifconfig 2>&1", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(iconfig, buff);
    }

  strcat(iconfig, newline); 
 

  pclose(in);



  if (!(in = popen("traceroute www.primus.ca 2>&1", "r"))) {
    exit(1);
  }
 
  
  while (fgets(buff, sizeof(buff), in) != NULL ) {
    strcat(tracert, buff);
    }

  strcat(tracert, newline); 
 

  pclose(in);


    
    printf("%", menu());


Last edited by Jess83; 01-23-2010 at 03:03 AM..
# 5  
Old 01-23-2010
Hi Jess83,

I am not sure if I grasped what you are trying to achieve. It seems that you want the popen() functions being executed "in background" while your program is doing other useful tasks, like building the menu. If so, threads could be an elegant solution to your problem. In one thread, you run your popen commands, while another thread takes care of the menu and progress bar.

I paste a simple example to illustrate my point. If you want to compile, you need to enable pthread (on many platforms, this is achieved passing the flag -pthread to the C compiler. But check your local doc).

HTH,
Loïc
--
Code:
#include <pthread.h>
#include <stdio.h>

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
int current_test = -1; 

void
progress_bar(int testno)
{
   for (;;) 
   {
      int progress;
      pthread_mutex_lock(&m);
      progress = current_test;
      pthread_mutex_unlock(&m);
      if (progress==testno) break;
      sleep(1);
      printf("."); fflush(stdout);
   }
}

void*
menu(void* ignore)
{
   printf("Testing in progress");
   printf("\nTest1: ");
   progress_bar(1);
   printf("\nTest2: ");
   progress_bar(2);
   printf("\nTest3: ");
   progress_bar(3);
   printf("\nTest completed!");
   pthread_exit(NULL);
}

void
set_progress(int num)
{
   pthread_mutex_lock(&m);
   current_test = num; 
   pthread_mutex_unlock(&m);
}

void*
mytest(void* ignore)
{
   FILE* fp;
   set_progress(0);
   fp = popen("sleep 5", "r"); 
   pclose(fp);
   set_progress(1);
   fp = popen("sleep 3", "r");
   pclose(fp); 
   set_progress(2);
   fp = popen("sleep 9", "r");
   pclose(fp); 
   set_progress(3);
   pthread_exit(NULL);
} 

int
main()
{
   pthread_t t1,t2;

   pthread_create(&t1, NULL, menu, NULL);
   pthread_create(&t2, NULL, mytest, NULL);
   pthread_join(t1, NULL);
   return 0;
}

# 6  
Old 01-23-2010
Yeah, I knew it could be done with thread I felt a bit lazy I wondered if there was a "quick and dirty" way without having to rewrite too much of my codes. Also I need to port it to Windows after im done with the Linux/Mac by i've seen pthread-win32 so i guess i can work around that Smilie thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Program Hangs

I have two programs, DriverScale.c and scale9.c. DriverScale.c calls scale 9.c which sends a W and a carraige return to the scale which SHOULD return the weight to DriverScale. However scale 9 hangs for at least 10 min, and then finally returns the weight. Compilation: ... (8 Replies)
Discussion started by: Meow613
8 Replies

2. Shell Programming and Scripting

Execution of compressed program

I need UNIX scripts for polling, Uncompressing files and moving files between directory. Also trying to save file paths and any other variables in an independent file (.env) and use these at runtime by executing this file in the main script. (3 Replies)
Discussion started by: new2script
3 Replies

3. Shell Programming and Scripting

How to display a message if program hangs(takes too long)

I have a ksh script (script1) that calls another ksh script (script2). If script2.ksh hangs or takes too long to execute I want script1.ksh to kill the call to script2.ksh and instead just display "Script2 can't run right now". Could someone help me with coding this? (1 Reply)
Discussion started by: mrskittles99
1 Replies

4. Shell Programming and Scripting

problem with sleep cmd in execution of cron...

I am scheduling a task at regular intervals at seconds acuracy using crond and sleep command . my data in crontab file is as below:- the above line is working fine when we are creating this crontab file before 00:05 min . But when we are creating the crontab file at 00:05min , unable to... (10 Replies)
Discussion started by: manoj424
10 Replies

5. Programming

Help with C++ program execution.

//Find the root of the equation (x^2)-2 by bisection method. #include<iostream> using namespace std; double a,x; double f(double x) { return ((x*x)-2); } //Suppose the function is (x*x)-2. void calcx(double a1,double b1) { x =... (2 Replies)
Discussion started by: poonam.gaigole
2 Replies

6. Shell Programming and Scripting

Java hangs even though shell script’s execution is completed

I'm trying to execute a script from within my java code. The execution of the script is over(it's pid is no more), but java is stuck on waitFor() method of the shell script process!. And yes, I'm reading output and error streams in 2 separate threads. Yes, they are being joined at the end(after... (0 Replies)
Discussion started by: pavanlimo
0 Replies

7. Programming

Storing the output of a Unix cmd in my C program

Hello experts, How can I retrieve the output from a Unix command and use it as string variable in my C program? For example, when I issue the command 'date' I get: Tue Jun 11 09:54:16 EEST 2009 I do not want to redirect the output of the command to a file and then open the file from... (3 Replies)
Discussion started by: Goseib
3 Replies

8. Programming

Debugging Program during execution

I have made use of 'valgrind' and -finstrument-functions compiler option for debugging / analyzing code. Both the options lets us know the line / file being executed to some extent. Is there a generic way that lets program dump the file:line it is getting executed dumped to a log file during... (3 Replies)
Discussion started by: uunniixx
3 Replies

9. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

10. Programming

after executing execvp()... program hangs up

Hi , I m actually trying to implement pipes program,but after executing the execvp(),my program is getting hanged up :mad: Actaully i m getting the desired output expected from execvp()...but once results are displayed on the output screen ,program is getting hanged up values of... (3 Replies)
Discussion started by: Crab
3 Replies
Login or Register to Ask a Question