Reputation: 97
My purpose is to decrease the file-write operations to the kernel therefore looking for a caching/buffering mechanism on POSIX. I believe standard-C library setbuf does that but is there a similar call in POSIX?
E.g. I'd like to set a buffer size of 1Kbytes and do not want my program to initiate the actual write operation to the disk before buffer size is exceeded.
fdpipe= open(PIPE_NAME,O_RDONLY);
......
fd = open(filename,O_CREAT|O_WRONLY|O_TRUNC|O_LARGEFILE,S_IREAD|S_IWRITE);
....
while((len = read(fdpipe,buffer,sizeof(buffer))) > 0) {
....
ret = write(fd,buffer,length = strlen(buffer));
}
Upvotes: 2
Views: 1008
Reputation: 275
You just keep the data in your own program buffer. Do not call write() systems call until, your own buffer reaches 1K. Then, call write() and flush(). Is this not good for you?
Upvotes: 0
Reputation: 42594
Why not simply use the stdio facility? You can use fdopen
to create a FILE*
that will read/write to a given file descriptor, and then set the buffer size you want with setvbuf
.
fdpipe = open(PIPE_NAME,O_RDONLY);
......
fd = open(filename,O_CREAT|O_WRONLY|O_TRUNC|O_LARGEFILE,S_IREAD|S_IWRITE);
FILE* outf = fdopen(fd, "wb");
char obuffer[1024];
setvbuf(outf, obuffer, _IOFBF, sizeof(obuffer));
....
while((len = read(fdpipe,buffer,sizeof(buffer))) > 0) {
....
ret = fwrite(buffer,1,length = strlen(buffer),outf);
}
Upvotes: 3