Skip to content

Instantly share code, notes, and snippets.

@motiejus
Last active June 26, 2018 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motiejus/02d8cc5c2e78433572d020cd9f708ef6 to your computer and use it in GitHub Desktop.
Save motiejus/02d8cc5c2e78433572d020cd9f708ef6 to your computer and use it in GitHub Desktop.
read a named socket
.PHONY: test
test: f read_c read_go
bash -c 'while true; do echo yaddda | head -2 >> f; echo written; done' & \
./read_c f; \
./read_go f; \
./read.py f; \
pkill -f "echo yaddda"
f:
mkfifo f
read_c: read.c
$(CC) -Wall -O2 $< -o $@
read_go: read.go
go build -o $@ $<
#include <stdio.h>
int main(int argc, char **argv) {
FILE *fp = fopen(argv[1], "r");
char str[0xff];
if(fp == NULL) {
perror("Error opening file");
return(-1);
}
if(fgets(str, 0xff, fp) != NULL) {
printf("from c: %s", str);
}
fclose(fp);
}
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
fn := os.Args[1]
f, err := os.Open(fn)
if err != nil {
log.Fatalf("failed to open %s: %s", fn, err)
}
read1(f)
}
func read1(f *os.File) {
rd := bufio.NewReader(f)
line, _, err := rd.ReadLine()
if err != nil {
log.Fatalf("failed to read line: %s", err)
}
fmt.Printf("from go: %s\n", line)
}
#!/usr/bin/env python3
import sys
line = None
with open(sys.argv[1], "r") as f:
line = f.readline()
print("from py: %s" % line)
@motiejus
Copy link
Author

motiejus ~/x $ make test
bash -c 'while true; do echo yaddda | head -2 >> f; echo written; done' & \
                ./read_c f; \
                ./read_go f; \
                ./read.py f; \
                pkill -f "echo yaddda"
from c: yaddda
written
from go: yaddda
written
from py: yaddda

written
:

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