Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Quote:
Has someone used O_DIRECT for writing files successfully?
|
I haven't, because there isn't a good reason to.
However, the following code illustrates its use. Note that both the buffer start and end should be block aligned.
Code:
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#define BLOCKSIZE 512
char image[] =
{
'P', '5', ' ', '2', '4', ' ', '7', ' ', '1', '5', '\n',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0,11,11,11,11, 0, 0,15,15,15,15, 0,
0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0,11, 0, 0, 0, 0, 0,15, 0, 0,15, 0,
0, 3, 3, 3, 0, 0, 0, 7, 7, 7, 0, 0, 0,11,11,11, 0, 0, 0,15,15,15,15, 0,
0, 3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0,11, 0, 0, 0, 0, 0,15, 0, 0, 0, 0,
0, 3, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0,11,11,11,11, 0, 0,15, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
};
int main()
{
void *buffer;
posix_memalign(&buffer, BLOCKSIZE, BLOCKSIZE);
memcpy(buffer, image, sizeof(image));
int f = open("feep.pgm", O_CREAT|O_TRUNC|O_WRONLY|O_DIRECT, S_IRWXU);
write(f, buffer, BLOCKSIZE);
close(f);
free(buffer);
return 0;
}
Last edited by neonsignal; 02-28-2010 at 08:55 PM.
|