Steven Narmontas wrote:
> Occasionally, I have need to read the entire contents of a file (which
> can be of any length) just so I can write it out again. For example, I
> may want to create a copy of a file, or simply read a file and dump its
> contents to stdout. The following program, for example simply reads a
> file called tester.txt and prints it to stdout:
[snip]
> The technique is to read and write the data in BUFSIZE byte chunks.
>
> First, is this a reasonable approach?
Reasonable, but not optimal.
> Second, is there an exceptionally good number to use for BUFSIZE, for a
> particular installation of linux? I've noticed that the struct_stat
> structure,which is used by the stat() system call has a member called
> st_blksize. For files on my system, st_blksize is 4096 = = 4K. Is this
> the 'ideal' size of BUFSIZE (because maybe the OS reads data in those
> sized chunks.)?
For copying large amounts of data with read()/write(), you should
generally specify a value much larger than BUFSIZ. The fewer system
calls, the better. OTOH, you don't want to allocate so large a buffer
that you exhaust RAM and end up using swap.
I don't know what's optimal; try running some tests with differing
sizes. However, make sure that the data isn't cached beforehand (e.g.
by running something which will displace cached data).
> Is st_blksize similar in concept to the "cluster size" on MS-DOS
> disks?
Pretty much, although st_blksize isn't guaranteed to correspond to
anything in particular.
> Where does the 'inode' size fit into the picture? When I prepped my
> hard disk during the Slackware install, I set the inode size to 1K.
This is unrelated. The inode density determines the proportion of the
partition which is allocated to the inode table.
An inode density of 1K means 1024 inodes per Mb of disk space. Each
file (directory, symlink, ...) requires one inode. If you have a 1Gb
partition, and an inode density of 1K, then you can have at most 2^20
files on that partition.
--
Glynn Clements <[EMAIL PROTECTED]>