W
W
weranda2018-10-15 08:05:37
Operating Systems
weranda, 2018-10-15 08:05:37

Is it possible to simultaneously write data to one file?

Hello
, I've been thinking about this. Let's say I posted a function to write the data entered from the form to a file. Two users at the same time, to within milliseconds, did the same action - they pressed the submit data button. What will happen - will the data be written to the file or not, and why?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Andrey, 2018-10-15
@weranda

if the work with files is organized correctly, then one will wait a little while the other writes.
if everything is completely sad, then the principle "who is the last is the dad" will work.

Y
Yaroslav Pronin, 2018-10-15
@proninyaroslav

In race conditions, the best option is the database, the file requires clear access regulations.

J
jcmvbkbc, 2018-10-15
@jcmvbkbc

Two users at the same time, to within milliseconds, did the same action - they pressed the submit data button. What will happen - will the data be written to the file or not, and why?

It depends on how the writing to the file is organized, on the OS and on the file system. For example, the POSIX standard guarantees that if a write is performed by a single write system call and the file was opened in "append" mode, then the write should look atomic. Those. data written by a successfully completed system call will be written to the file as a contiguous section. However, write is free to write less data than requested (which it will report with a return code). In the same way, for writing by multiple threads or processes to one shared file, the atomicity of writing and shifting the current position of the file is guaranteed. For other cases (non-POSIX, not a single system call, non-O_APPEND not via a shared file descriptor) there are no such guarantees.
For Windows - xs, are there any guarantees and which ones, too lazy to look for.

K
kagary 06, 2018-10-15
@kagary06

Based on the question, it is difficult to understand how your application is planned and how error handling is built, but in any case, you need to somehow mark which user made the addition of information.
If you have a server, then you can accumulate data in the buffer before writing. If a desktop application - store files in a separate user directory, next to the program.
In any case, files are not the best way to store information, since they do not have a strict structure (as well as indexing, meta-information and the ability to access information using queries).
If you do not use databases, then usually data storage tasks are followed by data search and modification tasks, and you yourself will need to implement the algorithms necessary for this. And this requires a fairly good knowledge of the principles of storing data in files (Flat-file Database, File Storage, etc.)
In addition, it will be necessary to implement a mechanism for file ownership (locks, limits on time and access rights) to avoid " race conditions" (parallel work of several users with one file).

S
Stanislav Bodrov, 2018-10-16
@jenki

Two users at the same time, to within milliseconds, did the same action - they pressed the submit data button. What will happen - will the data be written to the file or not, and why?
Nichrome will not come out. The fundamental limitation of physics is Heisenberg's uncertainty principle for action (the product of energy and time).
In addition, at the disk level, writing is slow, slow, and sequential relative to the processor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question