What happens fwrite/fread at the same time?


 
Thread Tools Search this Thread
Top Forums Programming What happens fwrite/fread at the same time?
# 1  
Old 02-15-2011
What happens fwrite/fread at the same time?

Hello,

I have a question about what happens when I copy the file which is being written by another process on Solaris 9/SPARC, UFS file system.

in particular, I want to know what happens while some process is reading the file using fread or mmap, another process try to write something on the exactly same place where is read by other process.

in the situation described above, does fwrite causes error? or both system call suceeds?

If you know there is some document or specification defines about confliction of read/write/mmap, please tell me.

(sorry if my English or manner is not good, I've never been post a question in these kind of forums)

Thanks.
# 2  
Old 02-15-2011
It explodes in a giant firey ball probably visible from space.

Well... not really. This is what we call a 'race condition'. It's not an error condition, but you're not likely to get what you wanted either. If the write happens first, the reading process sees what you wrote. If the write happens last, the reading process doesn't see the changes. If you're writing in chunks, you might even get something inbetween. So it doesn't blow up but it's not predictable, either, which is almost as bad!

To make this situation predictable one should use file area locks, or mutexes, and so forth.
# 3  
Old 02-15-2011
In the case of fread()/fwrite() it's even worse than that.

Both fread() and fwrite() by default buffer IO within the process's address space, so the OS may not even know that the application has made either call.

If you want to do concurrent access like that, you should probably use lower-level calls like read() and write(). Even then, you need to be concerned with applications not "seeing" things like changes in file size, for example.

And you do NOT want to be trying this on an NFS file system.
# 4  
Old 02-15-2011
> Corona688
> achenle
Thank you for answering my question.

Quote:
This is what we call a 'race condition'. It's not an error condition, but you're not likely to get what you wanted either.
is that mean, in the other words, when fread/fwrite called at the same time on same chunk of the file, both function call doesn't return an error but the result of fread would be unpredictable...is that right?? it means result of fwrite is not affected by the fread function call?

Quote:
To make this situation predictable one should use file area locks, or mutexes, and so forth.
Okay. I willSmilie

Quote:
Both fread() and fwrite() by default buffer IO within the process's address space, so the OS may not even know that the application has made either call.
So actual device I/O may not occur at the same time as fwrite/fread function is called, isn't it? Can I trust operating system to sync the data passed to fwrite on the disk certainly in spite of the fact that fread is called simultaneously? (Of course, unless somebody pull out the plug of the computer before operating system sync the data in the buffer...)

now I think that I almost understand what happening behind the fread/fwrite anyway.

Thanks.
# 5  
Old 02-16-2011
Quote:
Originally Posted by wipe3out
> Corona688
> achenle
Thank you for answering my question.

is that mean, in the other words, when fread/fwrite called at the same time on same chunk of the file, both function call doesn't return an error but the result of fread would be unpredictable...is that right??
Exactly. Unless you use some form of locking, the order is unpredictable.
Quote:
So actual device I/O may not occur at the same time as fwrite/fread function is called, isn't it? Can I trust operating system to sync the data passed to fwrite on the disk certainly in spite of the fact that fread is called simultaneously?
You can't trust fwrite() to sync at all, unless you fflush() after.
Quote:
now I think that I almost understand what happening behind the fread/fwrite anyway.
What fwrite does is call write(), if it has enough data, otherwise it just stores it to write later.


write() and read() are the actual system calls. They're not library functions, there's no deeper inside your program to go. At the point they get called, your program literally just stops until the kernel's done its job. So it's the kernel which decides what order they run in, and it won't enforce any particular order unless you've told it to.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Malloc problem with fread() to read file to structure in C

Hello, I am trying to read a text file into linked list, but always got the first and last records wrong. 1) The problem looks related to the initialization of the node temp with malloc(), but could not figure it out. No error/warning at compiling, though. 2) The output file is empty,... (10 Replies)
Discussion started by: yifangt
10 Replies

2. Programming

erase fread 's internal buffer ?

When fread is used twice, the second time it seems to overwrite the first time's output. Is there any way to clear fread's internal buffer after each use ? char *FILEREAD(const char *FILENAME) { static char READBUFFER = ""; READBUFFER = '\0'; // try to solve the problem but this will not... (2 Replies)
Discussion started by: limmer
2 Replies

3. Programming

segmentation fault in fwrite function

Hi, my code is written in proC and it is in UNIX(AIX).I have written a small code for writing data into a binary file,but while writing my program is giving core dump. Here Is my code---- fpWriteFile = fopen(WriteFileName,"wb+"); CHAR *recvgen; recvgen = (char... (7 Replies)
Discussion started by: ajaysahoo
7 Replies

4. Programming

fread: segementation fault(coredump) w/o stdlib.h

Hello All, I tried to test a sample fread example to read a complete file and the code is #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; long lSize; char * buffer; size_t result; pFile = fopen ( "test.xml" , "rb" ); if (pFile==NULL) {fputs ("File... (11 Replies)
Discussion started by: quintet
11 Replies

5. Programming

fwrite in Linux and UNIX

Hi I have an fwrite function in my C++ application. It is able to create the files in HP-UX. cross is one structure rec2 is another structure within cross. fwrite_return = fwrite( &cross.rec2, sizeof(cross.rec2), ... (14 Replies)
Discussion started by: rkraj
14 Replies

6. Programming

problem in reading file using fread

Hi All, These are the two ways i tried to read file but i getting work with second one not with the first. char buf; // Defining space for buf ctrlfnum = fopen(filename_arr.control_fname,"r"); 1) n = fread(buf,sizeof(buf),1,ctrlfnum); ============== (not works) 2) n =... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

7. Programming

fwrite throws segmentation fault

Code : function sSaveTFFile ....................... iRetCode = link (caCurrentFilename, caBackupFilename); if (iRetCode == -1) { ERR_MSG2(LOG_ALERT, "Can't move %s to %s", caCurrentFilename, caBackupFilename); return(FAILURE); } iRetCode = unlink... (6 Replies)
Discussion started by: fermisoft
6 Replies

8. Programming

Help -fwrite consuming lot of memory !!!

Hi , I am running a C/C++ program on a solaris 5.8 machine. This parituclar application has a module which saves data to a file. The module uses fwrite() function to save data. The fwrite function write about 500 MB of data to a file. The problem which I am facing is, the memory consumtion... (2 Replies)
Discussion started by: ajphaj
2 Replies

9. Programming

fwrite takes extremely long time

After my previous thread, I think I found out what causes the long delays. I run this program on several Linux computers, and the sometimes (after the file with the arrays becomes big) the fwrite takes between 100 ms to 900 ms. This is very bad for me, as I want a timer to halt each 30 ms.... ... (5 Replies)
Discussion started by: inna
5 Replies

10. Programming

Using fread if the buffer size is not known

Hi... I am trying to read a binary data that have different types of messages of different lengths. I am using fread() but this functions needs the size and count to read the buffer from the file. I think this may cause that the buffer overlaps other messages. Is there an alternative to read... (1 Reply)
Discussion started by: jlrodz
1 Replies
Login or Register to Ask a Question

Featured Tech Videos