Retaining data in a named pipe after an incomplete read access

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












Prerequisites:



$ mkfifo pipe1



If several lines are put into pipe1



$ (echo line 1; echo line 2) > pipe1


all the lines can be read in another terminal by



$ cat pipe1
line 1
line 2


So far all right.



But



if instead of the latter cat several partial reads are executed



$ head -n 1 pipe1; head -n 1 pipe1
line 1


then only the first line of the data piped into pipe1 is returned (by the first head -n 1); the rest of the data seems to get lost, the second read access is hanging hence waiting for (new) data available in pipe1.



How



to set up a named pipe and pipe contents to it in such a way that several consecutive partial reads are possible?



In other words, how to get this:



$ head -n 1 pipe1; head -n 1 pipe1
line 1
line 2






share|improve this question





















  • IIRC, but I may be wrong here, you can't close the pipe - this will flush the data. But head closes it. So you'll need a custom version of head that doesn't close it. Not very elegant. What are you trying to achieve with this? It sounds like an XY-Problem.
    – dirkt
    Jul 20 at 13:11











  • @dirk1 In the end I want to have a persistent FIFO queue: write arbitrary times to a file, read arbitrary times from the same file, data that is read disappears from the queue. Named pipes seemed to me like a solution for that. But as pointed out in the question, it does not work like expected by me.
    – Min-Soo Pipefeet
    Jul 20 at 13:18










  • Pipes won't work for this. See e.g. here for alternatives.
    – dirkt
    Jul 20 at 14:29
















up vote
1
down vote

favorite












Prerequisites:



$ mkfifo pipe1



If several lines are put into pipe1



$ (echo line 1; echo line 2) > pipe1


all the lines can be read in another terminal by



$ cat pipe1
line 1
line 2


So far all right.



But



if instead of the latter cat several partial reads are executed



$ head -n 1 pipe1; head -n 1 pipe1
line 1


then only the first line of the data piped into pipe1 is returned (by the first head -n 1); the rest of the data seems to get lost, the second read access is hanging hence waiting for (new) data available in pipe1.



How



to set up a named pipe and pipe contents to it in such a way that several consecutive partial reads are possible?



In other words, how to get this:



$ head -n 1 pipe1; head -n 1 pipe1
line 1
line 2






share|improve this question





















  • IIRC, but I may be wrong here, you can't close the pipe - this will flush the data. But head closes it. So you'll need a custom version of head that doesn't close it. Not very elegant. What are you trying to achieve with this? It sounds like an XY-Problem.
    – dirkt
    Jul 20 at 13:11











  • @dirk1 In the end I want to have a persistent FIFO queue: write arbitrary times to a file, read arbitrary times from the same file, data that is read disappears from the queue. Named pipes seemed to me like a solution for that. But as pointed out in the question, it does not work like expected by me.
    – Min-Soo Pipefeet
    Jul 20 at 13:18










  • Pipes won't work for this. See e.g. here for alternatives.
    – dirkt
    Jul 20 at 14:29












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Prerequisites:



$ mkfifo pipe1



If several lines are put into pipe1



$ (echo line 1; echo line 2) > pipe1


all the lines can be read in another terminal by



$ cat pipe1
line 1
line 2


So far all right.



But



if instead of the latter cat several partial reads are executed



$ head -n 1 pipe1; head -n 1 pipe1
line 1


then only the first line of the data piped into pipe1 is returned (by the first head -n 1); the rest of the data seems to get lost, the second read access is hanging hence waiting for (new) data available in pipe1.



How



to set up a named pipe and pipe contents to it in such a way that several consecutive partial reads are possible?



In other words, how to get this:



$ head -n 1 pipe1; head -n 1 pipe1
line 1
line 2






share|improve this question













Prerequisites:



$ mkfifo pipe1



If several lines are put into pipe1



$ (echo line 1; echo line 2) > pipe1


all the lines can be read in another terminal by



$ cat pipe1
line 1
line 2


So far all right.



But



if instead of the latter cat several partial reads are executed



$ head -n 1 pipe1; head -n 1 pipe1
line 1


then only the first line of the data piped into pipe1 is returned (by the first head -n 1); the rest of the data seems to get lost, the second read access is hanging hence waiting for (new) data available in pipe1.



How



to set up a named pipe and pipe contents to it in such a way that several consecutive partial reads are possible?



In other words, how to get this:



$ head -n 1 pipe1; head -n 1 pipe1
line 1
line 2








share|improve this question












share|improve this question




share|improve this question








edited Jul 20 at 13:11
























asked Jul 20 at 13:07









Min-Soo Pipefeet

1064




1064











  • IIRC, but I may be wrong here, you can't close the pipe - this will flush the data. But head closes it. So you'll need a custom version of head that doesn't close it. Not very elegant. What are you trying to achieve with this? It sounds like an XY-Problem.
    – dirkt
    Jul 20 at 13:11











  • @dirk1 In the end I want to have a persistent FIFO queue: write arbitrary times to a file, read arbitrary times from the same file, data that is read disappears from the queue. Named pipes seemed to me like a solution for that. But as pointed out in the question, it does not work like expected by me.
    – Min-Soo Pipefeet
    Jul 20 at 13:18










  • Pipes won't work for this. See e.g. here for alternatives.
    – dirkt
    Jul 20 at 14:29
















  • IIRC, but I may be wrong here, you can't close the pipe - this will flush the data. But head closes it. So you'll need a custom version of head that doesn't close it. Not very elegant. What are you trying to achieve with this? It sounds like an XY-Problem.
    – dirkt
    Jul 20 at 13:11











  • @dirk1 In the end I want to have a persistent FIFO queue: write arbitrary times to a file, read arbitrary times from the same file, data that is read disappears from the queue. Named pipes seemed to me like a solution for that. But as pointed out in the question, it does not work like expected by me.
    – Min-Soo Pipefeet
    Jul 20 at 13:18










  • Pipes won't work for this. See e.g. here for alternatives.
    – dirkt
    Jul 20 at 14:29















IIRC, but I may be wrong here, you can't close the pipe - this will flush the data. But head closes it. So you'll need a custom version of head that doesn't close it. Not very elegant. What are you trying to achieve with this? It sounds like an XY-Problem.
– dirkt
Jul 20 at 13:11





IIRC, but I may be wrong here, you can't close the pipe - this will flush the data. But head closes it. So you'll need a custom version of head that doesn't close it. Not very elegant. What are you trying to achieve with this? It sounds like an XY-Problem.
– dirkt
Jul 20 at 13:11













@dirk1 In the end I want to have a persistent FIFO queue: write arbitrary times to a file, read arbitrary times from the same file, data that is read disappears from the queue. Named pipes seemed to me like a solution for that. But as pointed out in the question, it does not work like expected by me.
– Min-Soo Pipefeet
Jul 20 at 13:18




@dirk1 In the end I want to have a persistent FIFO queue: write arbitrary times to a file, read arbitrary times from the same file, data that is read disappears from the queue. Named pipes seemed to me like a solution for that. But as pointed out in the question, it does not work like expected by me.
– Min-Soo Pipefeet
Jul 20 at 13:18












Pipes won't work for this. See e.g. here for alternatives.
– dirkt
Jul 20 at 14:29




Pipes won't work for this. See e.g. here for alternatives.
– dirkt
Jul 20 at 14:29















active

oldest

votes











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%2f457444%2fretaining-data-in-a-named-pipe-after-an-incomplete-read-access%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457444%2fretaining-data-in-a-named-pipe-after-an-incomplete-read-access%23new-answer', 'question_page');

);

Post as a guest