How can I know whether writing to a named pipe would block?
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I want to write to a named pipe only if it already has a reader. Currently, I'm using timeout
to detect whether the attempt to write to the pipe blocked, like so:
#! /usr/bin/env bash
rm -f pipe
mkfifo pipe
sleep 5
timeout 1 bash -c "echo Hello > pipe"
if [[ $? == 0 ]]
then
echo Somebody got our message
else
echo Nobody read from pipe, so we didn't send a message
fi
This works. If I tail -f pipe
in a separate terminal during the sleep I get one message, and if I don't, I get the other. But is there a better way? Ideally it would be something that doesn't rely on a timeout.
bash fifo
add a comment |Â
up vote
4
down vote
favorite
I want to write to a named pipe only if it already has a reader. Currently, I'm using timeout
to detect whether the attempt to write to the pipe blocked, like so:
#! /usr/bin/env bash
rm -f pipe
mkfifo pipe
sleep 5
timeout 1 bash -c "echo Hello > pipe"
if [[ $? == 0 ]]
then
echo Somebody got our message
else
echo Nobody read from pipe, so we didn't send a message
fi
This works. If I tail -f pipe
in a separate terminal during the sleep I get one message, and if I don't, I get the other. But is there a better way? Ideally it would be something that doesn't rely on a timeout.
bash fifo
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I want to write to a named pipe only if it already has a reader. Currently, I'm using timeout
to detect whether the attempt to write to the pipe blocked, like so:
#! /usr/bin/env bash
rm -f pipe
mkfifo pipe
sleep 5
timeout 1 bash -c "echo Hello > pipe"
if [[ $? == 0 ]]
then
echo Somebody got our message
else
echo Nobody read from pipe, so we didn't send a message
fi
This works. If I tail -f pipe
in a separate terminal during the sleep I get one message, and if I don't, I get the other. But is there a better way? Ideally it would be something that doesn't rely on a timeout.
bash fifo
I want to write to a named pipe only if it already has a reader. Currently, I'm using timeout
to detect whether the attempt to write to the pipe blocked, like so:
#! /usr/bin/env bash
rm -f pipe
mkfifo pipe
sleep 5
timeout 1 bash -c "echo Hello > pipe"
if [[ $? == 0 ]]
then
echo Somebody got our message
else
echo Nobody read from pipe, so we didn't send a message
fi
This works. If I tail -f pipe
in a separate terminal during the sleep I get one message, and if I don't, I get the other. But is there a better way? Ideally it would be something that doesn't rely on a timeout.
bash fifo
bash fifo
edited 12 hours ago
asked yesterday
MatrixManAtYrService
1705
1705
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
If you want to write to the pipe, only if there are some process that has opened it for reading, you could open it for writing in non-blocking mode.
With GNU dd
:
echo Hello | dd oflag=nonblock of=pipe status=none &&
echo message has been sent
And you'll get the error message for ENXIO if there was no reader.
Note that the above may still hang if the pipe is full (if there is a reader, but it is not currently reading).
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
If you want to write to the pipe, only if there are some process that has opened it for reading, you could open it for writing in non-blocking mode.
With GNU dd
:
echo Hello | dd oflag=nonblock of=pipe status=none &&
echo message has been sent
And you'll get the error message for ENXIO if there was no reader.
Note that the above may still hang if the pipe is full (if there is a reader, but it is not currently reading).
add a comment |Â
up vote
2
down vote
accepted
If you want to write to the pipe, only if there are some process that has opened it for reading, you could open it for writing in non-blocking mode.
With GNU dd
:
echo Hello | dd oflag=nonblock of=pipe status=none &&
echo message has been sent
And you'll get the error message for ENXIO if there was no reader.
Note that the above may still hang if the pipe is full (if there is a reader, but it is not currently reading).
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
If you want to write to the pipe, only if there are some process that has opened it for reading, you could open it for writing in non-blocking mode.
With GNU dd
:
echo Hello | dd oflag=nonblock of=pipe status=none &&
echo message has been sent
And you'll get the error message for ENXIO if there was no reader.
Note that the above may still hang if the pipe is full (if there is a reader, but it is not currently reading).
If you want to write to the pipe, only if there are some process that has opened it for reading, you could open it for writing in non-blocking mode.
With GNU dd
:
echo Hello | dd oflag=nonblock of=pipe status=none &&
echo message has been sent
And you'll get the error message for ENXIO if there was no reader.
Note that the above may still hang if the pipe is full (if there is a reader, but it is not currently reading).
answered 16 hours ago
Stéphane Chazelas
287k53531868
287k53531868
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473816%2fhow-can-i-know-whether-writing-to-a-named-pipe-would-block%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password