Sponsored Content
Top Forums Programming popen hangs program during cmd execution Post 302389160 by Jess83 on Saturday 23rd of January 2010 01:57:35 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
POPEN(3)						   BSD Library Functions Manual 						  POPEN(3)

NAME
popen, pclose -- process I/O LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdio.h> FILE * popen(const char *command, const char *type); int pclose(FILE *stream); DESCRIPTION
The popen() function ``opens'' a process by creating a bidirectional pipe forking, and invoking the shell. Any streams opened by previous popen() calls in the parent process are closed in the new child process. Historically, popen() was implemented with a unidirectional pipe; hence many implementations of popen() only allow the type argument to specify reading or writing, not both. Since popen() is now implemented using a bidirectional pipe, the type argument may request a bidirectional data flow. The type argument is a pointer to a null-terminated string which must be 'r' for reading, 'w' for writing, or 'r+' for reading and writing. A letter 'e' may be appended to that to request that the underlying file descriptor be set close-on-exec. The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that called popen(), unless this is altered by the command itself. Conversely, reading from a ``popened'' stream reads the command's standard output, and the command's standard input is the same as that of the process that called popen(). Note that output popen() streams are fully buffered by default. The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4(2). RETURN VALUES
The popen() function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory. The pclose() function returns -1 if stream is not associated with a ``popened'' command, if stream already ``pclosed'', or if wait4(2) returns an error. ERRORS
The popen() function does not reliably set errno. SEE ALSO
sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3), stdio(3), system(3) HISTORY
A popen() and a pclose() function appeared in Version 7 AT&T UNIX. Bidirectional functionality was added in FreeBSD 2.2.6. BUGS
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can be avoided by calling fflush(3) before popen(). Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The only hint is an exit status of 127. The popen() function always calls sh(1), never calls csh(1). BSD
May 20, 2013 BSD
All times are GMT -4. The time now is 03:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy