How can I know whether writing to a named pipe would block?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












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.










share|improve this question



























    up vote
    4
    down vote

    favorite
    1












    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.










    share|improve this question

























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 12 hours ago

























      asked yesterday









      MatrixManAtYrService

      1705




      1705




















          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).






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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).






            share|improve this answer
























              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).






              share|improve this answer






















                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).






                share|improve this answer












                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).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 16 hours ago









                Stéphane Chazelas

                287k53531868




                287k53531868



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay