mer*_*011 8 linux caching system-calls go

Foo.txt
Foo Bar Bar Foo

考虑以下简短程序.

package main

import "syscall"
import "fmt"


func main() {
    fd, err := syscall.Open("Foo.txt", syscall.O_RDONLY, 0)
    if err != nil {
        fmt.Println("Failed on open: ", err)
    }
    data := make([]byte, 100)
    _, err = syscall.Read(fd, data)
    if err != nil {
        fmt.Println("Failed on read: ", err)
    }
    syscall.Close(fd)
}

当我们运行上面的程序时,我们没有错误,这是正确的行为.

syscall.Open
fd, err := syscall.Open("Foo.txt", syscall.O_RDONLY | syscall.O_SYNC | syscall.O_DIRECT, 0)

当我再次运行程序时,我得到以下(不合需要的)输出.

Failed on read:  invalid argument
syscall.O_SYNCsyscall.O_DIRECTopen
syscallososos
Ubuntu 14.04ext4

更新:我尝试在下面的代码中使用@Nick Craig-Wood的软件包.

package main

import "io"
import "github.com/ncw/directio" 
import "os"
import "fmt"


func main() {
    in, err := directio.OpenFile("Foo.txt", os.O_RDONLY, 0666)
    if err != nil {
        fmt.Println("Error on open: ", err)
    }

    block := directio.AlignedBlock(directio.BlockSize)
    _, err = io.ReadFull(in, block)
    if err != nil {
        fmt.Println("Error on read: ", err)
    }
}

输出如下

Error on read:  unexpected EOF