What probability distribution models this race condition?

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











up vote
5
down vote

favorite
1












Consider the following command:
bash -c "echo x; cat 1" | tee 1.



My understanding is that it would fork to a new shell, write x to stdout, write file 1 not found to stderr, exit and return control to the parent process, and write x to stdout and to 1. Hence, I would expect the final output to be x, and the file 1 contains exactly the string x.



However, this is not the case. In actuality, the file 1 usually contains at least two instances of x, and sometimes thousands of lines of xs. On a batch test of running the command ten thousand times, the mean number of xs written to the file was 52.3, and the median was 1. What mechanic is causing this? What probability distribution models this behavior? I suspect that it is conditionally geometric and otherwise uniform.










share|improve this question

















  • 3




    It has to do with the timing of the execution of the left and right hand side of the pipeline. Both are started simultaneously, or close to it. If tee has opened the file for writing before cat opens it for reading, you may get many x-es in the file. In that case, the "loop" would end whenever cat reads faster than tee writes, reaching the end of the file.
    – Kusalananda
    Aug 10 at 18:42











  • From limited testing here, the average number of xs written to the file was of 4.35. I guess it will depends a lot on machine load.
    – Renan
    Aug 11 at 0:18














up vote
5
down vote

favorite
1












Consider the following command:
bash -c "echo x; cat 1" | tee 1.



My understanding is that it would fork to a new shell, write x to stdout, write file 1 not found to stderr, exit and return control to the parent process, and write x to stdout and to 1. Hence, I would expect the final output to be x, and the file 1 contains exactly the string x.



However, this is not the case. In actuality, the file 1 usually contains at least two instances of x, and sometimes thousands of lines of xs. On a batch test of running the command ten thousand times, the mean number of xs written to the file was 52.3, and the median was 1. What mechanic is causing this? What probability distribution models this behavior? I suspect that it is conditionally geometric and otherwise uniform.










share|improve this question

















  • 3




    It has to do with the timing of the execution of the left and right hand side of the pipeline. Both are started simultaneously, or close to it. If tee has opened the file for writing before cat opens it for reading, you may get many x-es in the file. In that case, the "loop" would end whenever cat reads faster than tee writes, reaching the end of the file.
    – Kusalananda
    Aug 10 at 18:42











  • From limited testing here, the average number of xs written to the file was of 4.35. I guess it will depends a lot on machine load.
    – Renan
    Aug 11 at 0:18












up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





Consider the following command:
bash -c "echo x; cat 1" | tee 1.



My understanding is that it would fork to a new shell, write x to stdout, write file 1 not found to stderr, exit and return control to the parent process, and write x to stdout and to 1. Hence, I would expect the final output to be x, and the file 1 contains exactly the string x.



However, this is not the case. In actuality, the file 1 usually contains at least two instances of x, and sometimes thousands of lines of xs. On a batch test of running the command ten thousand times, the mean number of xs written to the file was 52.3, and the median was 1. What mechanic is causing this? What probability distribution models this behavior? I suspect that it is conditionally geometric and otherwise uniform.










share|improve this question













Consider the following command:
bash -c "echo x; cat 1" | tee 1.



My understanding is that it would fork to a new shell, write x to stdout, write file 1 not found to stderr, exit and return control to the parent process, and write x to stdout and to 1. Hence, I would expect the final output to be x, and the file 1 contains exactly the string x.



However, this is not the case. In actuality, the file 1 usually contains at least two instances of x, and sometimes thousands of lines of xs. On a batch test of running the command ten thousand times, the mean number of xs written to the file was 52.3, and the median was 1. What mechanic is causing this? What probability distribution models this behavior? I suspect that it is conditionally geometric and otherwise uniform.







shell files pipe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 10 at 18:39









Will Sherwood

1261




1261







  • 3




    It has to do with the timing of the execution of the left and right hand side of the pipeline. Both are started simultaneously, or close to it. If tee has opened the file for writing before cat opens it for reading, you may get many x-es in the file. In that case, the "loop" would end whenever cat reads faster than tee writes, reaching the end of the file.
    – Kusalananda
    Aug 10 at 18:42











  • From limited testing here, the average number of xs written to the file was of 4.35. I guess it will depends a lot on machine load.
    – Renan
    Aug 11 at 0:18












  • 3




    It has to do with the timing of the execution of the left and right hand side of the pipeline. Both are started simultaneously, or close to it. If tee has opened the file for writing before cat opens it for reading, you may get many x-es in the file. In that case, the "loop" would end whenever cat reads faster than tee writes, reaching the end of the file.
    – Kusalananda
    Aug 10 at 18:42











  • From limited testing here, the average number of xs written to the file was of 4.35. I guess it will depends a lot on machine load.
    – Renan
    Aug 11 at 0:18







3




3




It has to do with the timing of the execution of the left and right hand side of the pipeline. Both are started simultaneously, or close to it. If tee has opened the file for writing before cat opens it for reading, you may get many x-es in the file. In that case, the "loop" would end whenever cat reads faster than tee writes, reaching the end of the file.
– Kusalananda
Aug 10 at 18:42





It has to do with the timing of the execution of the left and right hand side of the pipeline. Both are started simultaneously, or close to it. If tee has opened the file for writing before cat opens it for reading, you may get many x-es in the file. In that case, the "loop" would end whenever cat reads faster than tee writes, reaching the end of the file.
– Kusalananda
Aug 10 at 18:42













From limited testing here, the average number of xs written to the file was of 4.35. I guess it will depends a lot on machine load.
– Renan
Aug 11 at 0:18




From limited testing here, the average number of xs written to the file was of 4.35. I guess it will depends a lot on machine load.
– Renan
Aug 11 at 0:18















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%2f461878%2fwhat-probability-distribution-models-this-race-condition%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%2f461878%2fwhat-probability-distribution-models-this-race-condition%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