File handlings

Discussion in 'Perl Misc' started by Glodalec, Feb 3, 2004.

  1. Glodalec

    Glodalec Guest

    Hi !

    My B program is a simple script, which reads a string from STDIN,
    converts it to uppercase and prints it to STDOUT.

    B.pl
    #!/bin/perl
    use strict ;
    my $string=<STDIN>;
    print STDOUT uc $string,"\n" ;

    Now, I have A program, which listens on a TCP socket. Whenever string
    come over socket, it should be put on STDIN buffer (yes, STDIN), forking
    a new child, redirecting the parent SOCKET to child's STDOUT and exec
    B.pl. (The same way Apache & CGI modules work). I was trying to use
    select() but no luck.

    A.pl
    #!/bin/perl
    use strict ;
    ....
    SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE
    ....

    TCP settings....
    .......
    if (my $pid=fork)
    {
    waitpid($pid,0); # Ok, here is parent
    } else
    {
    my $string="This must be put in STDIN buffer" ; << Don't know how
    my $old=select(SOCKET) ; << This is not working
    exec("A.pl"); # and child
    select $oldstdout ;
    }

    Any help would be appreciated
    Glodalec, Feb 3, 2004
    #1
    1. Advertising

  2. Glodalec

    Ben Morrow Guest

    Glodalec <> wrote:
    > Now, I have A program, which listens on a TCP socket. Whenever string
    > come over socket, it should be put on STDIN buffer (yes, STDIN), forking
    > a new child, redirecting the parent SOCKET to child's STDOUT and exec
    > B.pl. (The same way Apache & CGI modules work). I was trying to use
    > select() but no luck.


    No, that's not how filehandles work at all. You can't push stuff back
    onto them: if you want to pass stuff to your child you have to use a
    pipe.

    > A.pl
    > #!/bin/perl
    > use strict ;
    > ...
    > SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE
    > ...
    >
    > TCP settings....
    > ......
    > if (my $pid=fork)


    fork can fail, in which case it returns undef.

    > {
    > waitpid($pid,0); # Ok, here is parent
    > } else
    > {
    > my $string="This must be put in STDIN buffer" ; << Don't know how
    > my $old=select(SOCKET) ; << This is not working
    > exec("A.pl"); # and child
    > select $oldstdout ;
    > }
    >
    > Any help would be appreciated


    my $pid = open my $TOCHILD, '|-', 'A.pl'
    or die "fork failed: $!";

    print $TOCHILD "The child will read this on its STDIN";

    See perldoc -f open.

    Ben

    --
    For the last month, a large number of PSNs in the Arpa[Inter-]net have been
    reporting symptoms of congestion ... These reports have been accompanied by an
    increasing number of user complaints ... As of June,... the Arpanet contained
    47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] *
    Ben Morrow, Feb 3, 2004
    #2
    1. Advertising

  3. Glodalec

    Glodalec Guest

    In article <bvoe9t$t31$>,
    says...
    >
    > Glodalec <> wrote:
    > > Now, I have A program, which listens on a TCP socket. Whenever string
    > > come over socket, it should be put on STDIN buffer (yes, STDIN), forking
    > > a new child, redirecting the parent SOCKET to child's STDOUT and exec
    > > B.pl. (The same way Apache & CGI modules work). I was trying to use
    > > select() but no luck.

    >
    > No, that's not how filehandles work at all. You can't push stuff back
    > onto them: if you want to pass stuff to your child you have to use a
    > pipe.

    Had never used them (except on command line), will read about them.
    >
    > > A.pl
    > > #!/bin/perl
    > > use strict ;
    > > ...
    > > SOCKET CREATION, LISTENING, GETTING LINE FROM A SOCKET INTO $LINE
    > > ...
    > >
    > > TCP settings....
    > > ......
    > > if (my $pid=fork)

    >
    > fork can fail, in which case it returns undef.

    I know, this script has been arranged a lot when posting here.
    >
    > > {
    > > waitpid($pid,0); # Ok, here is parent
    > > } else
    > > {
    > > my $string="This must be put in STDIN buffer" ; << Don't know how
    > > my $old=select(SOCKET) ; << This is not working
    > > exec("A.pl"); # and child
    > > select $oldstdout ;
    > > }
    > >
    > > Any help would be appreciated

    >
    > my $pid = open my $TOCHILD, '|-', 'A.pl'
    > or die "fork failed: $!";
    >
    > print $TOCHILD "The child will read this on its STDIN";

    THanks for advice. How about a child exec, which should actually write
    to parent's SESSION handler (got from accept()), instead of its own
    STDOUT.?
    >
    > See perldoc -f open.
    >
    > Ben
    >
    >
    Glodalec, Feb 3, 2004
    #3
  4. Glodalec

    Ben Morrow Guest

    Glodalec <> wrote:
    > > print $TOCHILD "The child will read this on its STDIN";

    > THanks for advice. How about a child exec, which should actually write
    > to parent's SESSION handler (got from accept()), instead of its own
    > STDOUT.?


    It's probably easiest to do that manually, viz (untested):

    accept my $NEWCONN, $LISTENER or die "accept failed: $!";

    # or you'd probably be better off using IO::Socket::INET
    my $NEWCONN = $LISTENER->accept or die "accept failed: $!";

    pipe my ($FROMPARENT, $TOCHILD) or die "pipe failed: $!";

    my $pid = fork;
    defined $pid or die "fork failed: $!";
    unless ($pid) {
    open STDIN, '<&', $FROMPARENT or die "dup2 to STDIN failed: $!";
    open STDOUT, '>&', $NEWCONN or die "dup2 to STDOUT failed: $!";
    exec 'A.pl' or die "exec failed: $!";
    }

    close $FROMPARENT;
    close $NEWCONN;

    Obviously you want to put all this in a sub, so you can say

    my $TOCHILD = accept_new_child($LISTENER);

    If you get errors from the open '<&' syntax, you may need a newer
    version of perl; or you can replace them with

    open STDIN, '<&' . fileno $FROMPARENT or ...

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
    Ben Morrow, Feb 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,408
    Scott Allen
    Jun 4, 2005
  2. AnetaKvel

    Applets and File handlings

    AnetaKvel, Jan 6, 2007, in forum: Java
    Replies:
    1
    Views:
    302
    Andrew Thompson
    Jan 6, 2007
  3. Dave
    Replies:
    1
    Views:
    990
    Juan T. Llibre
    Jun 8, 2007
  4. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    787
    Alf P. Steinbach
    Jul 10, 2007
  5. scad
    Replies:
    4
    Views:
    952
    James Kanze
    May 28, 2009
Loading...

Share This Page