Pipes and non blocking writing

  • Thread starter Sébastien Cottalorda
  • Start date
S

Sébastien Cottalorda

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
 
A

Anno Siegel

Sébastien Cottalorda said:
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
 
S

Sébastien Cottalorda

Anno said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top