Pipes and non blocking writing

Discussion in 'Perl Misc' started by Sébastien Cottalorda, Apr 20, 2004.

  1. Hi all,

    Several programs need to write in a pipe (created with "mknod /tmp/pipe.tub
    p").
    My problem is the following :
    If no program read the other side of the pipe, all writing operations are
    blocked.
    I don't care if I lost information, but I'd like programs to try to write to
    the pipe and return doing what they are programmed to.

    Here is my program:

    #====================================================================
    #!/usr/bin/perl -w

    use strict;
    use Fcntl;
    my $named_pipe='/tmp/pipe.tub';
    unless (-e $named_pipe){
    die "No way" if (system("/bin/mknod $named_pipe p"));
    }
    unless (sysopen(PIPE, $named_pipe, O_WRONLY|O_NONBLOCK)){
    die "Can\'t open $named_pipe : $!";
    }
    while(1){
    print "Trying to write in the pipe ...";
    print PIPE "Heelo World\n";
    print "Done\n";
    sleep 3;
    }
    close(PIPE);
    exit;
    #=====================================================================

    When I run that program, I get:
    Can`t open pipe: Device not configured at line 10

    What's wrong ????
    My OS: Mandrake 7.1 (kernel 2.2.15-4mdk)
    My Perl: perl 5.6.0

    Thanks in advance for any kind of help.

    Sébastien
    --
    [ retirer NOSPAM pour répondre directement
    remove NOSPAM to reply directly ]
     
    Sébastien Cottalorda, Apr 20, 2004
    #1
    1. Advertising

  2. Sébastien Cottalorda

    Anno Siegel Guest

    Sébastien Cottalorda <> wrote in comp.lang.perl.misc:
    > Hi all,
    >
    > Several programs need to write in a pipe (created with "mknod /tmp/pipe.tub
    > p").
    > My problem is the following :
    > If no program read the other side of the pipe, all writing operations are
    > blocked.
    > I don't care if I lost information, but I'd like programs to try to write to
    > the pipe and return doing what they are programmed to.
    >
    > Here is my program:
    >
    > #====================================================================
    > #!/usr/bin/perl -w
    >
    > use strict;
    > use Fcntl;
    > my $named_pipe='/tmp/pipe.tub';
    > unless (-e $named_pipe){
    > die "No way" if (system("/bin/mknod $named_pipe p"));
    > }
    > unless (sysopen(PIPE, $named_pipe, O_WRONLY|O_NONBLOCK)){
    > die "Can\'t open $named_pipe : $!";
    > }
    > while(1){
    > print "Trying to write in the pipe ...";
    > print PIPE "Heelo World\n";
    > print "Done\n";
    > sleep 3;
    > }
    > close(PIPE);
    > exit;
    > #=====================================================================
    >
    > When I run that program, I get:
    > Can`t open pipe: Device not configured at line 10
    >
    > What's wrong ????
    > My OS: Mandrake 7.1 (kernel 2.2.15-4mdk)
    > My Perl: perl 5.6.0


    Nothing is wrong, it's working to specification. A pipe can't be opened
    for writing when there's no reader at the other end.

    The solution is not to die when open() fails (why sysopen, btw?), but
    do other things an try again later. It would also be a good idea to
    install a SIGPIPE handler in case the reader disappears while you're
    writing.

    Anno
     
    Anno Siegel, Apr 20, 2004
    #2
    1. Advertising

  3. Anno Siegel wrote:

    > Sébastien Cottalorda <> wrote in
    > comp.lang.perl.misc:
    >> Hi all,
    >>
    >> Several programs need to write in a pipe (created with "mknod
    >> /tmp/pipe.tub p").
    >> My problem is the following :
    >> If no program read the other side of the pipe, all writing operations are
    >> blocked.
    >> I don't care if I lost information, but I'd like programs to try to write
    >> to the pipe and return doing what they are programmed to.
    >>
    >> Here is my program:
    >>
    >> #====================================================================
    >> #!/usr/bin/perl -w
    >>
    >> use strict;
    >> use Fcntl;
    >> my $named_pipe='/tmp/pipe.tub';
    >> unless (-e $named_pipe){
    >> die "No way" if (system("/bin/mknod $named_pipe p"));
    >> }
    >> unless (sysopen(PIPE, $named_pipe, O_WRONLY|O_NONBLOCK)){
    >> die "Can\'t open $named_pipe : $!";
    >> }
    >> while(1){
    >> print "Trying to write in the pipe ...";
    >> print PIPE "Heelo World\n";
    >> print "Done\n";
    >> sleep 3;
    >> }
    >> close(PIPE);
    >> exit;
    >> #=====================================================================
    >>
    >> When I run that program, I get:
    >> Can`t open pipe: Device not configured at line 10
    >>
    >> What's wrong ????
    >> My OS: Mandrake 7.1 (kernel 2.2.15-4mdk)
    >> My Perl: perl 5.6.0

    >
    > Nothing is wrong, it's working to specification. A pipe can't be opened
    > for writing when there's no reader at the other end.
    >
    > The solution is not to die when open() fails (why sysopen, btw?), but
    > do other things an try again later. It would also be a good idea to
    > install a SIGPIPE handler in case the reader disappears while you're
    > writing.
    >
    > Anno

    Hi,

    Thanks Ano,

    that's helps me to solve my problem:
    * intercepting SIGPIPE,
    * bypassing open error,
    * re-try to open again the pipe,
    allow me to manage correctly my pipe.

    thanks again.

    Sébastien

    --
    [ retirer NOSPAM pour répondre directement
    remove NOSPAM to reply directly ]
     
    Sébastien Cottalorda, Apr 20, 2004
    #3
    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. Hendra Gunawan
    Replies:
    1
    Views:
    12,959
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    984
    Valentin Tihomirov
    Oct 12, 2003
  3. nukleus
    Replies:
    14
    Views:
    887
    Chris Uppal
    Jan 22, 2007
  4. Tom Plunket
    Replies:
    3
    Views:
    769
    Donn Cave
    Jan 9, 2007
  5. john
    Replies:
    7
    Views:
    254
    Brian McCauley
    Mar 4, 2005
Loading...

Share This Page