Direct I/O with C++ on SGI IRIX

From Higher Intellect Wiki
Jump to: navigation, search

I am using IRIX Release 6.5 UNIX/SGI and would like to read and write files in direct/binary mode. When I compile the C++ program, at the end of this posting, with g++ and run it the following ensues. Successfully read file: Invalid argument

I do not get the "Invalid argument" message if I replace iFile with fileno(stdin) in the read() call or if I set iFile=fileno(stdin) just prior to the read() call. Thus it appears that the invalid argument has something to do with using a handle associated with a file opened in O_DIRECT mode. Am I right in this regard? If so, can anyone offer any solutions?

---------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <sys/fcntl.h>

main()
{
        int iFile;
        char buffer[10];
        unsigned int  bytes2read=2;

        if ((iFile = open("/usr/people/peter/junk", O_RDWR | O_DIRECT))
== -1)
        {
                perror("Error opening file");
                exit(1);
        }

        if (read(iFile, &(buffer[0]), bytes2read)< bytes2read)
                perror("Error reading file");
        else perror("Successfully read file");
        close (iFile);
}

Response

$ man 2 open
[...]
     O_DIRECT
            If set, all reads and writes on the resulting file descriptor will
            be performed directly to or from the user program buffer, provided
            appropriate size and alignment restrictions are met.  Refer to the
            F_SETFL and F_DIOINFO commands in the fcntl(2) manual entry for
            information about how to determine the alignment constraints.
            O_DIRECT is a Silicon Graphics extension and is only supported on
            local EFS and XFS file systems, and remote BDS file systems.

So the problem is that you aren't meeting the size and alignment restrictions for disk files -- which would require a buffer aligned upon a block boundary. Your 'buffer' is aligned at an arbitrary word boundary, and you are reading less than a multiple of a block.

The boundary alignment restrictions for character devices is one character.



Share your opinion