473,782 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having trouble with setvbuf with large buffer size under linux

I wish to fread a few thousand bytes at a time in turn from several
very large files from a DVD data disk, under Redhat Fedora Linux.

When this is done the DVD drive wastes a lot of time and almost shakes
itself to pieces.

I tried using setvbuf, with large buffers, e.g. (example only, not
checked):

#include <stdio.h>
#include <string.h>
FILE *FP1,*FP2;
#define big (32*1024*1024)
char buf1(big),buf2( big);
char a(1024),b(1024) ;
int i;

if(FP1=fopen("/mnt/dvd/file1","rb") == NULL) {
printf("bad fopen\n");
exit(1);
}
if(setvbuf(FP1, buf1,_IOFBF,big ) {
printf("bad setvbuf\n");
exit(1);
}
if(FP2=fopen("/mnt/dvd/file2","rb") == NULL) {
printf("bad fopen\n");
exit(1);
}
if(setvbuf(FP2, buf2,_IOFBF,big ) {
printf("bad setvbuf\n");
exit(1);
}
for (i=0,i<1024*102 4*1024; i+=1024) {
if (fread(a,sizeof (char),1024,FP1 ) != 1024) {
printf("bad fread\n");
exit(1);
}
if (fread(b,sizeof (char),1024,FP2 ) != 1024) {
printf("bad fread\n");
exit(1);
}
... play with a and b ...
}

Why does the value of "big" have no effect on time or number of drive
shakes? What can I do instead? (e.g., are there free substitutes for
stdio that work?)

Obviously I could create
my_fopen, my_fopen64, my_fread, my_fseek, my_fseeko, my_fscanf,
my_ftell, my_getc, my_ftell...

which use the standard library calls to read large blocks sequentially
into my own buffers, but it would be silly to virtually recreate
stdio. It would also be nice to be able to efficiently use other
people's software, without substituting my routine names for all the
standard ones.
Nov 14 '05 #1
6 4725
grunes <gr****@yahoo.c om> wrote:
I wish to fread a few thousand bytes at a time in turn from several
very large files from a DVD data disk, under Redhat Fedora Linux. When this is done the DVD drive wastes a lot of time and almost shakes
itself to pieces. I tried using setvbuf, with large buffers, e.g. (example only, not
checked): #include <stdio.h>
#include <string.h>
FILE *FP1,*FP2;
#define big (32*1024*1024)
char buf1(big),buf2( big);
char a(1024),b(1024) ;
What's that? Didn't you mean e.g. "buf1[big]" etc.? And do you
realize that there's nnormally a size limit for automatic arrays?
As far as I remember you can't count on more than 65536 bytes.
So you might be better of malloc()ing the buffers.
int i; if(FP1=fopen("/mnt/dvd/file1","rb") == NULL) {
printf("bad fopen\n");
exit(1);
}
if(setvbuf(FP1, buf1,_IOFBF,big ) {
printf("bad setvbuf\n");
exit(1);
}
if(FP2=fopen("/mnt/dvd/file2","rb") == NULL) {
printf("bad fopen\n");
exit(1);
}
if(setvbuf(FP2, buf2,_IOFBF,big ) {
printf("bad setvbuf\n");
exit(1);
}
for (i=0,i<1024*102 4*1024; i+=1024) {
if (fread(a,sizeof (char),1024,FP1 ) != 1024) {
printf("bad fread\n");
exit(1);
}
if (fread(b,sizeof (char),1024,FP2 ) != 1024) {
printf("bad fread\n");
exit(1);
}
... play with a and b ...
} Why does the value of "big" have no effect on time or number of drive
shakes? What can I do instead? (e.g., are there free substitutes for
stdio that work?)


That's something that's not a C question - the C standard doesn't
talk about the "shaking of DVD drives" nor even, shockingly enough,
about DVD drives at all... Chances are that this is nothing you can
influence from your userland program at all (<OT> just think about
what influence e.g. the size of kernel buffers, OS timing issues,
the way the driver for the DVD drive is written etc.may have </OT>).
Perhaps you get some reasonable answers in
comp.os.linux.d evelopment.apps .
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2
Maybe I should give a working program, which I have in fact tested!
You should eject, then remount the disk just before running it, so
execution time is not lessened by disk buffer caching (otherwise the
2nd run would be much faster, regardless of software efficiency,
because the file would already be in memory).

/*Why doesn't size of "big" affect execution time much? */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

FILE *F1,*F2;
#define big (32*1024*1024) /* Buffer size */
/*#define big (1024) */ /* My other trial value*/
#define fsiz (128*1024*1024) /* File Size */
char *b1,*b2;
char a[1024],b[1024];
int i;

if ((b1=malloc(big ))==NULL) {
printf("bad malloc\n");
exit(1);
}

if ((b2=malloc(big ))==NULL) {
printf("bad malloc\n");
exit(1);
}

F1=fopen("/mnt/cdrom/file1","rb");
if (F1==NULL) {
printf("bad fopen\n");
exit(1);
}

if(setvbuf(F1,b 1,_IOFBF,big)) {
printf("bad setvbuf\n");
exit(1);
}
F2=fopen("/mnt/cdrom/file2","rb");
if (F2==NULL) {
printf("bad fopen\n");
exit(1);
}

if(setvbuf(F2,b 2,_IOFBF,big)) {
printf("bad setvbuf\n");
exit(1);
}

for (i=0; i<fsiz; i+=1024) {
if(fread(a,1,10 24,F1) != 1024) {
printf("bad fread\n");
exit(1);
}
if(fread(b,1,10 24,F1) != 1024) {
printf("bad fread\n");
exit(1);
}
}
return(0);
}
Nov 14 '05 #3
grunes <gr****@yahoo.c om> wrote:
Maybe I should give a working program, which I have in fact tested!
You should eject, then remount the disk just before running it, so
execution time is not lessened by disk buffer caching (otherwise the
2nd run would be much faster, regardless of software efficiency,
because the file would already be in memory).


Ok, you're program is written in C, and that's probably why you
think it is useful to post your question in comp.lang.c. But C
as a language makes no promises at all about how fast a program
is going to run, how short disk access times are or how much
"DVD shaking" is allowed. All C as a language is concerned with
is that it works - it doesn't distinguish how fast or slow.

You have tried a standard C conforming solution by playing around
with setvbuf(). But you have found that it doesn't help you. Now
you have to start looking behind the scenes and try to understand
what are the real factors, most probably related to things like
the OS you're using, the way drivers talk with the DVD drive and
dozens of other factors. But for getting help on these kinds of
topics you have to look somewhere else. Since you are running
some kind of Linux comp.os.linux.d evelopment.apps would seem to
be the first place to go to, or comp.os.linux.d evelopment.syst em,
if you find that you have to deal with real low level issues.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4
> What's that? Didn't you mean e.g. "buf1[big]" etc.? And do you
realize that there's nnormally a size limit for automatic arrays?
As far as I remember you can't count on more than 65536 bytes.
So you might be better of malloc()ing the buffers.
Oops. Both of those, as well as the inappropriate include files were
fixed in my second post, where I used code I had actually tried. Not
doing that the first time was a mistake.
Why does the value of "big" have no effect on time or number of drive
shakes? What can I do instead? (e.g., are there free substitutes for
stdio that work?)

That's something that's not a C question - the C standard doesn't
talk about the "shaking of DVD drives" nor even, shockingly enough,
about DVD drives at all... Chances are that this is nothing you can
influence from your userland program at all (<OT> just think about
what influence e.g. the size of kernel buffers, OS timing issues,
the way the driver for the DVD drive is written etc. may have </OT>).
Perhaps you get some reasonable answers in
comp.os.linux.d evelopment.apps .


I'm not talking ANSI standard C in general. I'm talking recent
versions of Redhat Fedora Linux, compiled by gcc using default options
(except that I use -O). Clearly setvbuf size specification simply does
not work in this environment. I don't actually need to know why
changing buffer size does not reduce the number of hardware disk
seeks. I just need to know that it does not, and that I need it to, as
does anyone who is trying to set buffer size to speed up access to
multiple files, or reduce wear and tear on the drive.

(The same problem might apply to data from hard disk drives, but it is
more important on CD and DVD drives, because of the slower seek, and
because the mechanisms used on on many CD and DVD drives are more
easily worn out than those used on modern hard drives.)
Nov 14 '05 #5
gr****@yahoo.co m (grunes) wrote:
Why does the value of "big" have no effect on time or number of drive
shakes? What can I do instead? (e.g., are there free substitutes for
stdio that work?)
That's something that's not a C question - the C standard doesn't
talk about the "shaking of DVD drives" nor even, shockingly enough,
about DVD drives at all...
Perhaps you get some reasonable answers in
comp.os.linux.d evelopment.apps .


I'm not talking ANSI standard C in general.


Then why do you ask in a newsgroup that _does_ talk ISO C?
I'm talking recent versions of Redhat Fedora Linux,


Then you really do need to visit a system-specific group. What, you
think we're experts on every single system there was ever a C compiler
for? This group is for C, not for C-and-some-guys-system-setup.

Richard
Nov 14 '05 #6
> But for getting help on these kinds of
topics you have to look somewhere else.


Point taken. Wrong newsgroup. Thanks.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
8130
by: Hemant Shah | last post by:
Folks, I am using DB2 UDB 8.2 on AIX 5.1. How large of a bufferpool can you create? I tried to create a 4GB bufferpool db2 complained that is cannot allocate enogth memory. I have 16GB on this system. # db2 create bufferpool cfgbuffpool immediate size 1048576 pagesize 4096 SQL20189W The buffer pool operation (CREATE/ALTER) will not take effect until
2
3069
by: Keith Doyle | last post by:
I'm curious if setvbuf behavior is known to be undefined with regards to effects it may have on the file pointer. I've found that the following code works different on AIX 4.3.3 xlc and FreeBSD gcc 2.95.2 19990314 than it does on SCO 5.0.4 cc or Linux gcc egcs-1.1.2 19991024. This is a bit curious as here's a gcc example that works one way and one that works the other, and I would think the relevant code is in libc/glibc. This is what...
6
6372
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes before the end of file, and going backwards is the way. In this case, am I guaranteed best results...
0
913
by: zhimin | last post by:
Hi, I'm writing a program to send large file(100m) through dotnet using TCPListener & TCPClient, I'm sending the file with a ask and response loop: 1. Client send a flag 1 to server indicate it has data send to server. 2. Client send the buffer block size. 3. Client send the actual buffer to the server. 4. Server send a flag 1 to client indicating that the buffer has been successfully receeived. 5. The next loop until all data of the...
1
4013
by: lwickland | last post by:
Summary: System.Net.ScatterGatherBuffers.MemoryChuck allocates inordinately large bytes when sending large post data. The following application consumes inordinate quantities of memory. My code does not explicitly allocate memory in a loop nor does it explicitly allocate large blocks of memory. Yet, the application’s memory footprint will grow as large as 370 MB. Rarely will it run to completion; usually, it throws an out of memory...
3
5743
by: Michael | last post by:
Hi all, I'm having trouble PInvoking a TCHAR within a struct. I'll paste the specific struct's API definition below. I've tried so many numerous variations. The main Win32 error I get is 0x3f0 / 515L which amounts to ERROR_NO_TOKEN. Every single instance of this in the past was due to mistakes I made while within PInvoked structs. Is anybody able to point me to documentation or just tell me outright how to
0
2588
by: joshblair | last post by:
Hello, I am trying to post XML documents to a third party using the HttpWebRequest. This URL uses HTTPS (SSL) but I don't have a client certificate to deal with. Apparently they are using WebMethods as the platform that receives these postings. I don't have any experience with that technology. The sample below is the from test app that I put together to post the XML (cXML Order Requests) documents. These documents, at least my
10
6947
by: santosh | last post by:
Which situations call for the use of setvbuf(), specifically when you supply the buffer yourself? What is the advantage of an user specified buffer over an automatically allocated one? Can we call setvbuf() on the predefined streams, immediatly upon start of main()? How often is this function used in typical C programs? Thanks.
2
4626
by: sitko | last post by:
Hi, I'm creating a quick utility which will copy one file into another, but I need it to have the ability to select out certain lines not to copy. I figured I'd use fread/fwrite but set the buffering size to a line with "setvbuf", here is the code I was going to use as a skeleton for this: #include <stddef.h> #include <stdio.h> #define FAIL 0 #define SUCCESS 1
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.