Skip to content

Instantly share code, notes, and snippets.

@jimjh
Created November 27, 2012 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimjh/4152221 to your computer and use it in GitHub Desktop.
Save jimjh/4152221 to your computer and use it in GitHub Desktop.
Experiments with parallel read/write in Go without file.Sync or file.Close
package main
// parallel_rw.go experiments with parallel read/write from a file without
// syncing (aka flushing) or closing.
import (
"encoding/binary"
"fmt"
// "math/rand"
"os"
"runtime"
// "time"
)
var MAX uint32 = 1000
func main() {
runtime.GOMAXPROCS(4)
file, err := os.Create("log")
if err != nil {
panic(err)
}
file.Close()
done := make(chan interface{})
go read(done)
go write()
<-done
}
// read reads 0..MAX integers from the log file.
func read(done chan<- interface{}) {
file, err := os.OpenFile("log", os.O_RDONLY, 0666)
if nil != err {
panic(err)
}
var i uint32
for i = 0; i < MAX; i = i {
var n uint32
err := binary.Read(file, binary.LittleEndian, &n)
if nil != err {
fmt.Println("\t\t\t\t\t\tError: ", err.Error())
continue
}
fmt.Println("Read ", n)
if i != n {
fmt.Printf("Corrupted data: expected %d, was %d\n", i, n)
os.Exit(1)
}
i++
}
done <- nil
}
// write writes 0..MAX integers into the log file.
func write() {
file, err := os.OpenFile("log", os.O_WRONLY, 0666)
if nil != err {
panic(err)
}
var i uint32
for i = 0; i < MAX; i++ {
if err := binary.Write(file, binary.LittleEndian, i); nil != err {
panic(err)
}
// time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
fmt.Println("\t\t\tWrite ", i)
}
}
@jimjh
Copy link
Author

jimjh commented Nov 27, 2012

Execute this with:

$> rm -f log; go run parallel_rw.go

Writes and Reads occur in parallel, but if Read is called without any new data, io.EOF is returned. If no data is written for a long time, it becomes a spin loop.

@jimjh
Copy link
Author

jimjh commented Nov 27, 2012

With MAX set to 10, the output of a particular run looks like:

                        Error:  EOF
            Write  0
Read  0
Read  1
            Write  1
                        Error:  EOF
            Write  2
Read  2
            Write  3
Read  3
            Write  4
Read  4
            Write  5
Read  5
            Write  6
Read  6
            Write  7
Read  7
            Write  8
Read  8
            Write  9
Read  9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment