using popen to execute and feed input to set of executable files

using popen to execute and feed input to set of executable files

Post by saravan_ra.. » Fri, 25 Feb 2005 22:38:27



i am doing a programme which executes a set of programs one by one and
gives input to those programs automatically.(inputs for all those
programs are fixed and are stored in a text file).

i am using popen() to execute and pipe input to those programs.

my problem is how to know ,the input piped my program reaches the other
program are not and how to have the control over the input.?

how to know the child process id which is created by the popen()?

 
 
 

using popen to execute and feed input to set of executable files

Post by Barry Margoli » Fri, 25 Feb 2005 23:31:13




> i am doing a programme which executes a set of programs one by one and
> gives input to those programs automatically.(inputs for all those
> programs are fixed and are stored in a text file).

> i am using popen() to execute and pipe input to those programs.

> my problem is how to know ,the input piped my program reaches the other
> program are not and how to have the control over the input.?

> how to know the child process id which is created by the popen()?

If you need this level of control, you can't use popen().  You have to
call pipe(), fork(), exec(), etc. directly.

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

 
 
 

using popen to execute and feed input to set of executable files

Post by Dan Kore » Sat, 26 Feb 2005 15:36:17



Quote:

> If you need this level of control, you can't use popen().
> You have to call pipe(), fork(), exec(), etc. directly.

Don't forget knife().

Every fork() requires one.

;-))

dk

 
 
 

using popen to execute and feed input to set of executable files

Post by Rama » Sat, 26 Feb 2005 15:45:19





> > i am doing a programme which executes a set of programs one by one and
> > gives input to those programs automatically.(inputs for all those
> > programs are fixed and are stored in a text file).

> > i am using popen() to execute and pipe input to those programs.

> > my problem is how to know ,the input piped my program reaches the other
> > program are not and how to have the control over the input.?

> > how to know the child process id which is created by the popen()?

> If you need this level of control, you can't use popen().  You have to
> call pipe(), fork(), exec(), etc. directly.

please guide me to do the above said pgm by giving sample code
 
 
 

using popen to execute and feed input to set of executable files

Post by Barry Margoli » Sat, 26 Feb 2005 16:36:19








> > > i am doing a programme which executes a set of programs one by one and
> > > gives input to those programs automatically.(inputs for all those
> > > programs are fixed and are stored in a text file).

> > > i am using popen() to execute and pipe input to those programs.

> > > my problem is how to know ,the input piped my program reaches the other
> > > program are not and how to have the control over the input.?

> > > how to know the child process id which is created by the popen()?

> > If you need this level of control, you can't use popen().  You have to
> > call pipe(), fork(), exec(), etc. directly.

> please guide me to do the above said pgm by giving sample code

Any advanced Unix programming textbook is likely to have such examples.  
Or you could simply look at the glibc source code to see how popen() is
written.

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

 
 
 

using popen to execute and feed input to set of executable files

Post by Rama » Thu, 03 Mar 2005 16:10:02









> > > > i am doing a programme which executes a set of programs one by one and
> > > > gives input to those programs automatically.(inputs for all those
> > > > programs are fixed and are stored in a text file).

> > > > i am using popen() to execute and pipe input to those programs.

> > > > my problem is how to know ,the input piped my program reaches the other
> > > > program are not and how to have the control over the input.?

> > > > how to know the child process id which is created by the popen()?

> > > If you need this level of control, you can't use popen().  You have to
> > > call pipe(), fork(), exec(), etc. directly.

> > please guide me to do the above said pgm by giving sample code

> Any advanced Unix programming textbook is likely to have such examples.  
> Or you could simply look at the glibc source code to see how popen() is
> written.

Thanks for ur tip.

as u told i have written my pgm using pipe(),fork()&execl() as follows

main()
{

---
---

while(1)
{
 /*  method to read program name and its i/p from a text file line by
line*/

  if(end of file)
      break;

  if( pipe(fdes) )
  {

     if( (st=fork())  >= 0)
     {

         if(st==0)           /*CHILD */
          {
            close(0);        /* CLOSING STDIN */
            dup(fdes[0]);    /* MAKES FDES[0] AS STDIN */

            close(fdes[1]);
            close(fdes[2]);

            execl(prgm read from the file);
          }
          else     /* PARENT */
          {
            close(fdes[0]);

            /* loop to write inputs to pipe */
              write(fdes[1],input_read_from_the_file,size)

             pwait(&status);

             close(fdes);
           }
      }

  }

  if(status == 0 )
     pgm success
  else
     pgm failed

Quote:} /* WHILE END */
} /* MAIN END */

In this i have the following doubts

  1)is this logic is correct?(this is working fine.but i wanted to
know whether this is memory efficient or not.)
  2)if the program executed by child process becomes end less
process(due to mismatch in I/O),How to trap it in the main process ?
  3)How to send the o/p of parent & child process to 2 different
curses window?

Awaiting for the reply..

 
 
 

using popen to execute and feed input to set of executable files

Post by Barry Margoli » Fri, 04 Mar 2005 11:33:56




> as u told i have written my pgm using pipe(),fork()&execl() as follows

> main()
> {

> ---
> ---

> while(1)
> {
>  /*  method to read program name and its i/p from a text file line by
> line*/

>   if(end of file)
>       break;

>   if( pipe(fdes) )
>   {

>      if( (st=fork())  >= 0)
>      {

>          if(st==0)           /*CHILD */
>           {
>             close(0);        /* CLOSING STDIN */
>             dup(fdes[0]);    /* MAKES FDES[0] AS STDIN */

I generally recommend using dup2() rather than dup(), so that you can
specify the destination descriptor explicitly.

Quote:

>             close(fdes[1]);
>             close(fdes[2]);

You should be closing fdes[0] and fdes[1], there is no fdes[2].

Quote:

>             execl(prgm read from the file);
>           }
>           else     /* PARENT */
>           {
>             close(fdes[0]);

>             /* loop to write inputs to pipe */
>               write(fdes[1],input_read_from_the_file,size)

>              pwait(&status);

I'm not familiar with pwait() -- I assume it's a variant of wait().  You
could also use waitpid() to wait specifically for the PID in st.

Quote:

>              close(fdes);

That should be fdes[1].  And you should probably do this *before*
calling wait(), since many programs don't exit until they read EOF on
stdin.

- Show quoted text -

Quote:>            }
>       }

>   }

>   if(status == 0 )
>      pgm success
>   else
>      pgm failed

> } /* WHILE END */

> } /* MAIN END */

> In this i have the following doubts

>   1)is this logic is correct?(this is working fine.but i wanted to
> know whether this is memory efficient or not.)

See my embedded comments above.

Quote:>   2)if the program executed by child process becomes end less
> process(due to mismatch in I/O),How to trap it in the main process ?

There's no way to do this reliably.  You can use a timeout, but that
could result in a false positive if the program is just taking a long
time to do its work.

Quote:>   3)How to send the o/p of parent & child process to 2 different
> curses window?

You need to hook the child process's stdout to a pipe.  The parent
process can read from the pipe and then send this to a different curses
window than it writes its own output.

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

 
 
 

1. How to input file to a executable file use csh?

Hi, all:
     I have an executable file named a.out, there is a file name needed
to input use keyboard,. Now I want to input file name use csh program.
The following is my program, but it has some problem, the screen file
name can't be transfered to a.out, who can help me to correct it, Thank
in advance!!!
-------------------------------------
#!/bin/csh
set  SRCS=(traj_9803.200 traj_9803.500 traj_9803.1000  \
            traj_9803.1500 traj_9803.2000 traj_9803.3000 )
set num=1
set total=6
while ($num <= $total)
    a.out < $SRCS[$num]
    ctrans -d ps.color gmeta>$SRCS[$num].ps

end

  hzs.vcf
< 1K Download

2. why should "/bin/gzip < larger_than_2GB_file > foo.gz" fails?

3. Executable Binary File vs. Executable Script File

4. Serial line connection

5. Can't execute files that are executable

6. unplumbing the active physical int with IPMP

7. can't execute an executable file.....

8. some ide-scsi commands starve drives on the same cable

9. executable files w/o using 'file'

10. Truncate an input file to a executing script?

11. Redirecting input to a file - echo the input to screen/file?

12. How to get the return code of the program which is executed by calling popen() finction ?