Unix Shell - Skip writing to a named pipe if the reading process is not running

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > Skip writing to a named pipe if the reading process is not running





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Skip writing to a named pipe if the reading process is not running
Mark Hobley

2006-01-22, 6:10 pm

I am using a script to write to a named pipe:

echo hello > /tmp/mypipe.pipe

If the reading process is not running, then the script will hang waiting for
something to read from the pipe.

How can I skip the write to the pipe if the reading process is not running?

For example, is it possible for echo to exit with an error, if there is no
reading process?

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Bill Marcum

2006-01-22, 6:10 pm

On Sun, 22 Jan 2006 20:08:24 GMT, Mark Hobley
<markhobley@hotpop.deletethisbit.com> wrote:
> I am using a script to write to a named pipe:
>
> echo hello > /tmp/mypipe.pipe
>
> If the reading process is not running, then the script will hang waiting for
> something to read from the pipe.
>
> How can I skip the write to the pipe if the reading process is not running?
>
> For example, is it possible for echo to exit with an error, if there is no
> reading process?
>

Use "ps | grep" or pgrep, or make the reading process a script with a
trap command that removes the named pipe. Then the writing process has
to test whether the pipe exists.


--
Outside of a dog, a book is a man's best friend. Inside a dog it's too
dark to read.
-- Groucho Marx
Mark Hobley

2006-01-23, 2:55 am

Bill Marcum <bmarcum@iglou.com> wrote:

> Use "ps | grep" or pgrep, or make the reading process a script with a
> trap command that removes the named pipe. Then the writing process has
> to test whether the pipe exists.


The writing process tests whether the pipe exists. If the reading
process has died, the pipe may still exist.

I want to open the pipe in non-blocking mode for writing, so that instead of
waiting, I get an error, if there is no read process attached to the pipe.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Kenny McCormack

2006-01-23, 7:52 am

In article <4asda3-3j5.ln1@neptune.markhobley.yi.org>,
Mark Hobley <markhobley@hotpop.deletethisbit.com> wrote:
>Bill Marcum <bmarcum@iglou.com> wrote:
>
>
>The writing process tests whether the pipe exists. If the reading
>process has died, the pipe may still exist.
>
>I want to open the pipe in non-blocking mode for writing, so that instead of
>waiting, I get an error, if there is no read process attached to the pipe.


I think the answer you are looking for is: No built-in way; that is, no way
that is going to be really reliable, i.e., not vulnerable to race
conditions.

The cleanest way to handle this is to, as the previous poster alludes, make
sure that the reader process always removes the pipe before exiting.
However, as we all know, life is not fair, and it's not always possible to
cover every case. And so it goes...

Stephane Chazelas

2006-01-23, 7:52 am

On Sun, 22 Jan 2006 20:08:24 GMT, Mark Hobley wrote:
> I am using a script to write to a named pipe:
>
> echo hello > /tmp/mypipe.pipe
>
> If the reading process is not running, then the script will hang waiting for
> something to read from the pipe.
>
> How can I skip the write to the pipe if the reading process is not running?
>
> For example, is it possible for echo to exit with an error, if there is no
> reading process?

[...]

maybe something like:

sleep 1 < /tmp/mypipe.pipe &
gotpipe=0
trap gotpipe=1 PIPE
exec 3> /tmp/mypipe.pipe
wait
echo hello >&3
exec 3>&-
trap - PIPE
if [ "$gotpipe" -eq 1 ]; then
echo "all processes reading from that pipe are gone"
fi

You need a command like sleep as you need it to be still alive
at the time you open the pipe for writing later on.

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com