Link to home
Start Free TrialLog in
Avatar of ravindra042100
ravindra042100

asked on

write system call

Why does the write system-call trunc if called on an already existing file? Isn't it too costly to do so as most applns would just want to do minor modifications?
Avatar of ozo
ozo
Flag of United States of America image

What minor modifications do you want to do?
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ravindra042100
ravindra042100

ASKER

Just consider this example.

TO do:
------
I open a file of size,say 10Mb, replace a char and close it. (Not fixed length records ...)

      Now, for this to be done, all the blocks of this huge file have to be freed. When the data is written back, the blocks will need to be re-aquired needing modifications of free-list, the block map, inode ... Isn't this costly?
It could be better if just the required block is read in, modified and written back.
For replacing a single character, you can do this.
But if you change record lengths, the entire end of the file may have to be shifted.
(which is why databases built to deal with lots of insertions and deletions build indexes of pointers into their files)
The write call shouldn't be doing any truncation; that is handled by the call made to open. If the O_TRUNC mode is specified with O_RDWR or O_WRITE then that is what will happen. Not specifying it will allow you to seek to a position in the file and overwrite the data.

Thank you ...