LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2010, 05:27 AM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
return value of write(2)


Hi all,

I am using write(2) to append records to a file, and there are several similar processes that append to the same file.

Code:
int fd = open( "my.txt", O_RDWR|O_CREAT|O_APPEND|O_SYNC, (mode_t)0664 );
...
size_t iW = write( fd, &myrecord, sizeof(myrecord) );
if (iW == sizeof(myrecord) {
  // I find peace
  fsync( fd );
}
else if (iW == 0) {
  // don't panic. decide what to do.
}
else if (iW == -1) {
  // don't panic. check errno to see what to do.
}
else {
  // don't panic. may be I should.
}
I'm wondering if it is possible that write(2) writes an incomplete record to the file before another write(2) occurs.

The man page says write(2) will return:
the number of bytes written; or
0, indicating nothing written; or
-1, indicating error.

Should I worry about write(2) writing an incomplete record?

also, do I need fsync(2) despite I have the O_SYNC flag set when the fd is open?

Thanks for teaching.
 
Old 05-07-2010, 05:55 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Jerry Mcguire View Post
...
Should I worry about write(2) writing an incomplete record?
...
What are you trying ultimately to do ? Think of, say, a database application not actually updating data on disk because of 'write' error.
 
Old 05-07-2010, 08:19 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
If the return value from write() is not -1, then the following applies.

If the return value is equal to the number of bytes you wished to write, then there is obviously no problem presented here.

If it is fewer than the number of bytes you wished to write, then the reason has nothing to do with whether a physical write has actually occurred. It happens because you've run out of space to write data to that file, and the returned number shows how many bytes you successfully wrote. An example is zero, which means the space was full before you even started to write.
 
Old 05-07-2010, 02:30 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Let me expand on that: this refers only to synchronous writes. If you're writing asynchronously, then you might get fewer bytes written. In that case, you need to loop back until all of them have been written.
 
Old 05-09-2010, 09:23 PM   #5
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Hi Sergei,

I am try to do the followings,
1) I have an app that receives data from TCP socket and writes to a file for bufferring. Data in this file is then processed by several computational processes to extract information. (i.e. One-Write-Many-Read).

The file organization is records of 2-byte-length-descriptor + the actual binary data. There will be 350MB in this file per day. The first app uses writev(2). The computational processes uses read(2) starting off reading the 2-byte len, and then read again in a loop for the rest of the binary data.


2) These extracted information are then written to a centralized log file, and this log file will be read by another process so that appropriate information will be delivered to various clients over TCP sockets. (i.e. Many-Write-Many-Read).

The file organization is variable-length records separated by newline character, so that fputs() and fgets() can be easily used, and this log can be `cat` for examination.


I do use file for inter-process communication a lot. I worry about write or writev not finishing a complete record and then another write kicks in. Any advice please. Thanks.

----
Hi Wje,

I do only synchronous write to ensure records written to file are permanent. If the target runs out of space, the problem is more than just an incomplete record. I can deal with that later. Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read/write,write/write lock with smbclient fails swatidas11 Linux - Networking 1 03-10-2010 12:27 PM
dpkg return error :post installation script return an error code (1) grimfold Debian 2 09-10-2009 01:55 PM
root unable to write on drive mounted with read/write perrmission aquash2000 Linux - Server 5 07-19-2008 12:20 PM
Return to Castle Wolfenstein GOTY: Cannot write to hunkusage.dat caseyweederman Linux - Games 2 12-13-2005 09:56 PM
Retain Server Connection Up, If Client Break, write system call return SIGPIPE anandss2004 Programming 2 10-24-2005 12:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration