What happens when you read a file while it is overwritten?How to make reading and writing the same file in the same pipeline always “fail”?Reading lines from a file with bash: for vs. whileMaking a process read a different file for the same filenameHow can I make a special file that executes code when read fromHow do you deliberately trigger a “text file busy” error?File contents created when openedRecovering a file that is overwritten with cat >Can you read an updated mtime, but when reading the contents they have yet to be updated?Cannot read regular file - IO operations blockWhat happens to file descriptors when the process is killed?What happens when I kill 'cp'? Is it safe and does it have any consequences?
How does one intimidate enemies without having the capacity for violence?
Approximately how much travel time was saved by the opening of the Suez Canal in 1869?
Is it possible to do 50 km distance without any previous training?
How is it possible to have an ability score that is less than 3?
Convert two switches to a dual stack, and add outlet - possible here?
How to draw a waving flag in TikZ
What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)
What is the word for reserving something for yourself before others do?
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Maximum likelihood parameters deviate from posterior distributions
How much of data wrangling is a data scientist's job?
Do I have a twin with permutated remainders?
What does the "remote control" for a QF-4 look like?
Do infinite dimensional systems make sense?
strTok function (thread safe, supports empty tokens, doesn't change string)
Theorems that impeded progress
Which country benefited the most from UN Security Council vetoes?
tikz convert color string to hex value
How can bays and straits be determined in a procedurally generated map?
Why is Minecraft giving an OpenGL error?
What would happen to a modern skyscraper if it rains micro blackholes?
I'm flying to France today and my passport expires in less than 2 months
What's that red-plus icon near a text?
Important Resources for Dark Age Civilizations?
What happens when you read a file while it is overwritten?
How to make reading and writing the same file in the same pipeline always “fail”?Reading lines from a file with bash: for vs. whileMaking a process read a different file for the same filenameHow can I make a special file that executes code when read fromHow do you deliberately trigger a “text file busy” error?File contents created when openedRecovering a file that is overwritten with cat >Can you read an updated mtime, but when reading the contents they have yet to be updated?Cannot read regular file - IO operations blockWhat happens to file descriptors when the process is killed?What happens when I kill 'cp'? Is it safe and does it have any consequences?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Suppose I read (cat) a file while another process is rewriting its contents. Is the output predictable? What would happen?
files
add a comment |
Suppose I read (cat) a file while another process is rewriting its contents. Is the output predictable? What would happen?
files
3
The behavior is undefined, you should never do this.
– daisy
Jun 26 '12 at 13:40
See this as well: How to make reading and writing the same file in the same pipeline always “fail”?.
– codeforester
yesterday
add a comment |
Suppose I read (cat) a file while another process is rewriting its contents. Is the output predictable? What would happen?
files
Suppose I read (cat) a file while another process is rewriting its contents. Is the output predictable? What would happen?
files
files
asked Jun 26 '12 at 13:38
LevenLeven
215148
215148
3
The behavior is undefined, you should never do this.
– daisy
Jun 26 '12 at 13:40
See this as well: How to make reading and writing the same file in the same pipeline always “fail”?.
– codeforester
yesterday
add a comment |
3
The behavior is undefined, you should never do this.
– daisy
Jun 26 '12 at 13:40
See this as well: How to make reading and writing the same file in the same pipeline always “fail”?.
– codeforester
yesterday
3
3
The behavior is undefined, you should never do this.
– daisy
Jun 26 '12 at 13:40
The behavior is undefined, you should never do this.
– daisy
Jun 26 '12 at 13:40
See this as well: How to make reading and writing the same file in the same pipeline always “fail”?.
– codeforester
yesterday
See this as well: How to make reading and writing the same file in the same pipeline always “fail”?.
– codeforester
yesterday
add a comment |
3 Answers
3
active
oldest
votes
That depends on what the writer does.
If the writer overwrites the existing file, then the reader will see the new content when the writer overtakes the reader, if ever. If the writer and the reader proceed at variable speeds, the reader may alternatively see old and new content.
If the writer truncates the file before it starts to write, the reader will run against the end of the file at that point.
If the writer creates a new file then moves the new file to the old name, the reader will keep reading from the old file. If an opened file is moved or removed, the processes that have the file opened keep reading from that same file. If the file is removed, it actually remains on the disk (but with no way to open it again) until the last process has closed it.
Unix systems tend not to have mandatory locks. If an application wants to ensure that its writer component and its reader component don't step on each other's toes, it's up to the developer to use proper locking. There are a few exceptions where a file that's open by the kernel may be protected from writing by user applications, for example a loop-mounted filesystem image or an executable that's being executed on some unix variants.
Gilles, does your explanation also apply inftp
/sftp
scenarios? Say a process starts reading a transmittedftp
file while another version of the same file is overwriting it due to a new transmission.
– iruvar
Aug 4 '14 at 20:27
@1_CR Yes. In that case, the writer and the reader are the ftpd processes.
– Gilles
Aug 4 '14 at 21:46
what's the meaning of overtake here?
– Victor Choy
Oct 10 '16 at 7:02
@VictorChoy Standard meaning: to start behind/after someone else and at some point move ahead of them.
– Gilles
Oct 10 '16 at 17:50
add a comment |
It's a classic race condition, so the outcome is unpredictable by definition.
Among others, it depends on
fopen(3)
oropen(2)
write modes,- how/if the writer is buffering its output,
- how the reader is reading the file,
- the speed difference between the reader and writer,
- the time difference between the read and writer's start.
- And of course, on modern multi-core machines, things are complicated even more by other factors lower down (e.g. process scheduling).
If you need to be able to read a file while it's being rewritten, then you can make the writer make a transient copy of the file, modify that, then copy it back to the original file. This the way rsync
does this, for instance. There are a number of ways to implement this, but no free lunch. Each method has its own shortcomings and repercussions.
1
This answer is far more comprehensive than mine. Deleting my answer.
– killermist
Jun 26 '12 at 14:22
add a comment |
Previous responders have more comprehensive explanations than this, but here's a trick that definitely works as well, pretty much doing exactly what he wants:
$ tail -f <filename>
Will show you the end of the file as it's being written. Handy if you want to pipe STDERR to a file but still see it in another terminal window, for example.
New contributor
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f41668%2fwhat-happens-when-you-read-a-file-while-it-is-overwritten%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
That depends on what the writer does.
If the writer overwrites the existing file, then the reader will see the new content when the writer overtakes the reader, if ever. If the writer and the reader proceed at variable speeds, the reader may alternatively see old and new content.
If the writer truncates the file before it starts to write, the reader will run against the end of the file at that point.
If the writer creates a new file then moves the new file to the old name, the reader will keep reading from the old file. If an opened file is moved or removed, the processes that have the file opened keep reading from that same file. If the file is removed, it actually remains on the disk (but with no way to open it again) until the last process has closed it.
Unix systems tend not to have mandatory locks. If an application wants to ensure that its writer component and its reader component don't step on each other's toes, it's up to the developer to use proper locking. There are a few exceptions where a file that's open by the kernel may be protected from writing by user applications, for example a loop-mounted filesystem image or an executable that's being executed on some unix variants.
Gilles, does your explanation also apply inftp
/sftp
scenarios? Say a process starts reading a transmittedftp
file while another version of the same file is overwriting it due to a new transmission.
– iruvar
Aug 4 '14 at 20:27
@1_CR Yes. In that case, the writer and the reader are the ftpd processes.
– Gilles
Aug 4 '14 at 21:46
what's the meaning of overtake here?
– Victor Choy
Oct 10 '16 at 7:02
@VictorChoy Standard meaning: to start behind/after someone else and at some point move ahead of them.
– Gilles
Oct 10 '16 at 17:50
add a comment |
That depends on what the writer does.
If the writer overwrites the existing file, then the reader will see the new content when the writer overtakes the reader, if ever. If the writer and the reader proceed at variable speeds, the reader may alternatively see old and new content.
If the writer truncates the file before it starts to write, the reader will run against the end of the file at that point.
If the writer creates a new file then moves the new file to the old name, the reader will keep reading from the old file. If an opened file is moved or removed, the processes that have the file opened keep reading from that same file. If the file is removed, it actually remains on the disk (but with no way to open it again) until the last process has closed it.
Unix systems tend not to have mandatory locks. If an application wants to ensure that its writer component and its reader component don't step on each other's toes, it's up to the developer to use proper locking. There are a few exceptions where a file that's open by the kernel may be protected from writing by user applications, for example a loop-mounted filesystem image or an executable that's being executed on some unix variants.
Gilles, does your explanation also apply inftp
/sftp
scenarios? Say a process starts reading a transmittedftp
file while another version of the same file is overwriting it due to a new transmission.
– iruvar
Aug 4 '14 at 20:27
@1_CR Yes. In that case, the writer and the reader are the ftpd processes.
– Gilles
Aug 4 '14 at 21:46
what's the meaning of overtake here?
– Victor Choy
Oct 10 '16 at 7:02
@VictorChoy Standard meaning: to start behind/after someone else and at some point move ahead of them.
– Gilles
Oct 10 '16 at 17:50
add a comment |
That depends on what the writer does.
If the writer overwrites the existing file, then the reader will see the new content when the writer overtakes the reader, if ever. If the writer and the reader proceed at variable speeds, the reader may alternatively see old and new content.
If the writer truncates the file before it starts to write, the reader will run against the end of the file at that point.
If the writer creates a new file then moves the new file to the old name, the reader will keep reading from the old file. If an opened file is moved or removed, the processes that have the file opened keep reading from that same file. If the file is removed, it actually remains on the disk (but with no way to open it again) until the last process has closed it.
Unix systems tend not to have mandatory locks. If an application wants to ensure that its writer component and its reader component don't step on each other's toes, it's up to the developer to use proper locking. There are a few exceptions where a file that's open by the kernel may be protected from writing by user applications, for example a loop-mounted filesystem image or an executable that's being executed on some unix variants.
That depends on what the writer does.
If the writer overwrites the existing file, then the reader will see the new content when the writer overtakes the reader, if ever. If the writer and the reader proceed at variable speeds, the reader may alternatively see old and new content.
If the writer truncates the file before it starts to write, the reader will run against the end of the file at that point.
If the writer creates a new file then moves the new file to the old name, the reader will keep reading from the old file. If an opened file is moved or removed, the processes that have the file opened keep reading from that same file. If the file is removed, it actually remains on the disk (but with no way to open it again) until the last process has closed it.
Unix systems tend not to have mandatory locks. If an application wants to ensure that its writer component and its reader component don't step on each other's toes, it's up to the developer to use proper locking. There are a few exceptions where a file that's open by the kernel may be protected from writing by user applications, for example a loop-mounted filesystem image or an executable that's being executed on some unix variants.
answered Jun 27 '12 at 0:40
GillesGilles
546k12911101624
546k12911101624
Gilles, does your explanation also apply inftp
/sftp
scenarios? Say a process starts reading a transmittedftp
file while another version of the same file is overwriting it due to a new transmission.
– iruvar
Aug 4 '14 at 20:27
@1_CR Yes. In that case, the writer and the reader are the ftpd processes.
– Gilles
Aug 4 '14 at 21:46
what's the meaning of overtake here?
– Victor Choy
Oct 10 '16 at 7:02
@VictorChoy Standard meaning: to start behind/after someone else and at some point move ahead of them.
– Gilles
Oct 10 '16 at 17:50
add a comment |
Gilles, does your explanation also apply inftp
/sftp
scenarios? Say a process starts reading a transmittedftp
file while another version of the same file is overwriting it due to a new transmission.
– iruvar
Aug 4 '14 at 20:27
@1_CR Yes. In that case, the writer and the reader are the ftpd processes.
– Gilles
Aug 4 '14 at 21:46
what's the meaning of overtake here?
– Victor Choy
Oct 10 '16 at 7:02
@VictorChoy Standard meaning: to start behind/after someone else and at some point move ahead of them.
– Gilles
Oct 10 '16 at 17:50
Gilles, does your explanation also apply in
ftp
/sftp
scenarios? Say a process starts reading a transmitted ftp
file while another version of the same file is overwriting it due to a new transmission.– iruvar
Aug 4 '14 at 20:27
Gilles, does your explanation also apply in
ftp
/sftp
scenarios? Say a process starts reading a transmitted ftp
file while another version of the same file is overwriting it due to a new transmission.– iruvar
Aug 4 '14 at 20:27
@1_CR Yes. In that case, the writer and the reader are the ftpd processes.
– Gilles
Aug 4 '14 at 21:46
@1_CR Yes. In that case, the writer and the reader are the ftpd processes.
– Gilles
Aug 4 '14 at 21:46
what's the meaning of overtake here?
– Victor Choy
Oct 10 '16 at 7:02
what's the meaning of overtake here?
– Victor Choy
Oct 10 '16 at 7:02
@VictorChoy Standard meaning: to start behind/after someone else and at some point move ahead of them.
– Gilles
Oct 10 '16 at 17:50
@VictorChoy Standard meaning: to start behind/after someone else and at some point move ahead of them.
– Gilles
Oct 10 '16 at 17:50
add a comment |
It's a classic race condition, so the outcome is unpredictable by definition.
Among others, it depends on
fopen(3)
oropen(2)
write modes,- how/if the writer is buffering its output,
- how the reader is reading the file,
- the speed difference between the reader and writer,
- the time difference between the read and writer's start.
- And of course, on modern multi-core machines, things are complicated even more by other factors lower down (e.g. process scheduling).
If you need to be able to read a file while it's being rewritten, then you can make the writer make a transient copy of the file, modify that, then copy it back to the original file. This the way rsync
does this, for instance. There are a number of ways to implement this, but no free lunch. Each method has its own shortcomings and repercussions.
1
This answer is far more comprehensive than mine. Deleting my answer.
– killermist
Jun 26 '12 at 14:22
add a comment |
It's a classic race condition, so the outcome is unpredictable by definition.
Among others, it depends on
fopen(3)
oropen(2)
write modes,- how/if the writer is buffering its output,
- how the reader is reading the file,
- the speed difference between the reader and writer,
- the time difference between the read and writer's start.
- And of course, on modern multi-core machines, things are complicated even more by other factors lower down (e.g. process scheduling).
If you need to be able to read a file while it's being rewritten, then you can make the writer make a transient copy of the file, modify that, then copy it back to the original file. This the way rsync
does this, for instance. There are a number of ways to implement this, but no free lunch. Each method has its own shortcomings and repercussions.
1
This answer is far more comprehensive than mine. Deleting my answer.
– killermist
Jun 26 '12 at 14:22
add a comment |
It's a classic race condition, so the outcome is unpredictable by definition.
Among others, it depends on
fopen(3)
oropen(2)
write modes,- how/if the writer is buffering its output,
- how the reader is reading the file,
- the speed difference between the reader and writer,
- the time difference between the read and writer's start.
- And of course, on modern multi-core machines, things are complicated even more by other factors lower down (e.g. process scheduling).
If you need to be able to read a file while it's being rewritten, then you can make the writer make a transient copy of the file, modify that, then copy it back to the original file. This the way rsync
does this, for instance. There are a number of ways to implement this, but no free lunch. Each method has its own shortcomings and repercussions.
It's a classic race condition, so the outcome is unpredictable by definition.
Among others, it depends on
fopen(3)
oropen(2)
write modes,- how/if the writer is buffering its output,
- how the reader is reading the file,
- the speed difference between the reader and writer,
- the time difference between the read and writer's start.
- And of course, on modern multi-core machines, things are complicated even more by other factors lower down (e.g. process scheduling).
If you need to be able to read a file while it's being rewritten, then you can make the writer make a transient copy of the file, modify that, then copy it back to the original file. This the way rsync
does this, for instance. There are a number of ways to implement this, but no free lunch. Each method has its own shortcomings and repercussions.
answered Jun 26 '12 at 14:21
AlexiosAlexios
14.7k15067
14.7k15067
1
This answer is far more comprehensive than mine. Deleting my answer.
– killermist
Jun 26 '12 at 14:22
add a comment |
1
This answer is far more comprehensive than mine. Deleting my answer.
– killermist
Jun 26 '12 at 14:22
1
1
This answer is far more comprehensive than mine. Deleting my answer.
– killermist
Jun 26 '12 at 14:22
This answer is far more comprehensive than mine. Deleting my answer.
– killermist
Jun 26 '12 at 14:22
add a comment |
Previous responders have more comprehensive explanations than this, but here's a trick that definitely works as well, pretty much doing exactly what he wants:
$ tail -f <filename>
Will show you the end of the file as it's being written. Handy if you want to pipe STDERR to a file but still see it in another terminal window, for example.
New contributor
add a comment |
Previous responders have more comprehensive explanations than this, but here's a trick that definitely works as well, pretty much doing exactly what he wants:
$ tail -f <filename>
Will show you the end of the file as it's being written. Handy if you want to pipe STDERR to a file but still see it in another terminal window, for example.
New contributor
add a comment |
Previous responders have more comprehensive explanations than this, but here's a trick that definitely works as well, pretty much doing exactly what he wants:
$ tail -f <filename>
Will show you the end of the file as it's being written. Handy if you want to pipe STDERR to a file but still see it in another terminal window, for example.
New contributor
Previous responders have more comprehensive explanations than this, but here's a trick that definitely works as well, pretty much doing exactly what he wants:
$ tail -f <filename>
Will show you the end of the file as it's being written. Handy if you want to pipe STDERR to a file but still see it in another terminal window, for example.
New contributor
New contributor
answered 2 days ago
Willoughby WillWilloughby Will
11
11
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f41668%2fwhat-happens-when-you-read-a-file-while-it-is-overwritten%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
The behavior is undefined, you should never do this.
– daisy
Jun 26 '12 at 13:40
See this as well: How to make reading and writing the same file in the same pipeline always “fail”?.
– codeforester
yesterday