FIFOs implementation

前端 未结 5 1167
悲哀的现实
悲哀的现实 2020-12-17 04:35

Consider the following code:

writer.c

mkfifo(\"/tmp/myfifo\", 0660);

int fd = open(\"/tmp/myfifo\", O_WRONLY);

char *foo, *bar;

...

write(fd, foo         


        
5条回答
  •  旧巷少年郎
    2020-12-17 04:46

    You'll have to define some kind wire protocol or serialization/deserialization format so that your reader knows how to interpret the data that it's reading from the fifo. Using a separator is the simplest way to go about this but you'll run into problems if your separator ever appears as part of the data output of your writer.

    Slightly farther along the complexity scale, your protocol might define both a separator and a way of indicating the length of each "piece" or "message" of data that you're sending.

    Finally, this problem is more thoroughly solved by writing serialized messages which your writer will then deserialize after receiving. You may be interested in using something like Protocol Buffers or Thrift to achieve this (with the added bonus that you can implement your reader or writer in a number of different programming languages without modifying your protocol).

提交回复
热议问题