-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo.go
52 lines (42 loc) · 791 Bytes
/
fifo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bufio"
"fmt"
"log"
"os"
"syscall"
"time"
)
func main() {
fmt.Println("Starting named pipe test...")
// setup
pipe := "pipe.log"
os.Remove(pipe)
err := syscall.Mkfifo(pipe, 0666)
if err != nil {
log.Fatal("Couldn't make fifo, ", err)
}
file, err := os.OpenFile(pipe, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
log.Fatal("Couldn't open pipe, ", err)
}
// write to named pipe
go write(file)
// read from named pipe
reader := bufio.NewReader(file)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
log.Fatal("Couldn't read line, ", err)
}
fmt.Printf(string(line))
}
}
func write(file *os.File) {
i := 0
for {
file.WriteString(fmt.Sprintf("wrote %d times\n", i))
i++
time.Sleep(time.Second)
}
}