Solved

write system call

Posted on 2000-04-29
6
201 Views
Last Modified: 2010-04-21
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?
0
Comment
Question by:ravindra042100
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2762178
What minor modifications do you want to do?
0
 
LVL 40

Accepted Solution

by:
jlevie earned 25 total points
ID: 2762230
Why would you think that it would be costly. All that needs to happen to truncate a file to nothing is simply for all disk blocks other than the first to be returned to the free list.

Whether a file gets modified in place or simply re-writtten is up to the application. If you want, you can open a file read/write and modify it's contents in place by reading data, modifying that data and writing it back out. That's commonly done when the data organization in the file is effectively fixed-length records. If the data isn't fixed-length, it's more work than it's worth, so you simply create a new file containing the modified data.
0
 

Author Comment

by:ravindra042100
ID: 2772804
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
ID: 2772842
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)
0
 

Expert Comment

by:cside
ID: 2773753
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.

0
 

Author Comment

by:ravindra042100
ID: 2776555
Thank you ...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now