Writing files using O_DIRECT in C


 
Thread Tools Search this Thread
Top Forums Programming Writing files using O_DIRECT in C
# 1  
Old 02-28-2010
Writing files using O_DIRECT in C

I am trying to write .pgm images using the O_DIRECT flag in open().
I have a char* buffer which has the image data.

I know that I have to align the buffers and have done that using posix_memalign() yet only a part of the image gets written.

Has someone used O_DIRECT for writing files successfully? Can you help me on how to proceed with this?

Thanks
# 2  
Old 02-28-2010
Can you show us you code?
# 3  
Old 03-01-2010
Here is the function. It does not write the entire image.
Code:
// imgdata is the image data to be written into a pgm image file

int writePgm( char* Filename, unsigned char* imgdata, int width, int height )
{
   void *buf;
   int ret=0;

   unsigned long long int bytes_written=0;
   int ps=getpagesize();
   posix_memalign(&buf, ps, height*width*2);  //each pixel is equvivalent to 2 bytes
   memcpy(buf ,imgdata, height*width*2);

   FILE* stream;
   int fd = open(Filename, O_CREAT | O_WRONLY |O_DIRECT, 00666);
   stream = fdopen(fd, "wb");

   if( stream == NULL)
   {
     perror( "Can't open image file" );
     return 1;
   }
   
   fprintf( stream, "P5\n%u %u 255\n", width, height );
   
   fwrite( buf, ps, width*height/ps, stream );
   
   fclose( stream );
   close(fd);
   return 0;
}

# 4  
Old 03-01-2010
When using O_DIRECT on Linux, every block written needs to be properly aligned and the correct size, including the very last one, which is a real difficulty when creating files that don't perfectly match that size.

You're probably having problems with the last block not getting written, and also because you're using fwrite(), which does buffered output. But the buffer is not likely to be properly aligned and could also be smaller than a page size.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

What is the difference between o_direct and DAX with ext4 filesystem?

I'm trying to understand the difference between o_direct flag of open system call and dax (direct access) with ext4 filesystem. According to my understanding both bypass page cache. But I'm still unclear about the crucial difference between these 2 techniques. If there is a huge difference... (1 Reply)
Discussion started by: BHASKAR JUPUDI
1 Replies

2. Shell Programming and Scripting

Writing to Memory Instead of Flat Files

i've always been of the mind that having to write to disk is very sloppy and dirty. i personally hate this. so i wonder, is there a way to have a shell script/program write to memory and access whatever it has in memory whenever it wants. pretty much treat memory like hard disk. can... (2 Replies)
Discussion started by: SkySmart
2 Replies

3. Shell Programming and Scripting

Problem writing to different files

Hello: I have the following code: ---------------------------------- open (OUTPUT_FILE, ">>/usr/users/rovolis/PREPAID/CC/TCG/PP.$cyear$cmonth$cday.txt")||die "$!"; 82 open (OUTPUT_FILE2, ">>/usr/users/rovolis/PREPAID/CC/TCG/PR.$cyear$cmonth$cday.txt")||die "$!"; 83 # ... (0 Replies)
Discussion started by: chriss_58
0 Replies

4. Shell Programming and Scripting

Testing for O_DIRECT support

Is there some was to test if opening with the O_DIRECT flag will work (without writing a C program)? (2 Replies)
Discussion started by: brsett
2 Replies

5. UNIX for Dummies Questions & Answers

Writing a text to many files

Is there a command in shell-script that I can use that copies a string or writes it to many files? Say I have files a, b and c and I want to copy or write the text "Hallo, I am a newbie!" to all these files? I know I can do echo "Hallo, I am a newbie!" > a but this only copies it to one file. How... (2 Replies)
Discussion started by: #moveon
2 Replies

6. Shell Programming and Scripting

Writing files without temporary files

Hey Guys, I was wondering if someone would give me a hand with an issue I'm having, let me explain the situation: I have a file that is constantly being written to and read from with updated lines: # cat activity.file activity1 activity2 activity3 activity4 activity5 This file... (2 Replies)
Discussion started by: bashshadow1979
2 Replies

7. UNIX for Dummies Questions & Answers

Writing large files to tape

I have a zipped file that is ~ 10GB. I tried tarring it off to a tape, but I receive: tar: <filename> too large to archive. Use E function modifier. The file is stored on a UFS mount, so I was unable to use ufsdump. What other options do I have? (I don't have a local file system large... (3 Replies)
Discussion started by: FredSmith
3 Replies

8. UNIX for Dummies Questions & Answers

new to writing script files

thanks guys i managed to answer (0 Replies)
Discussion started by: bebop1111116
0 Replies

9. UNIX for Dummies Questions & Answers

Reading and Writing Files ?

Hello, Our group is just starting to get into UNIX here. We are former REXX users on a VM Mainframe. Can someone point me to the documentation to read a file and write a file in a Unix Shell Script or does this have to be done in another language? Thank you in advance... Dave (3 Replies)
Discussion started by: tracydp
3 Replies

10. UNIX for Dummies Questions & Answers

What files are writing to a directory

Is there a way to tell what files/scripts are writing/wrote to a given directory? (3 Replies)
Discussion started by: hattorihanzo
3 Replies
Login or Register to Ask a Question