FIFOs implementation

前端 未结 5 1162
悲哀的现实
悲哀的现实 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:56

    A separator is indeed one way to do this - and conveniently enough, C strings come with such a separator - the nul-terminator at the end of the string.

    If you change your write() calls so that they also write out the nul-terminator (note that sizeof(char) is defined to be 1, so it can be left out):

    write(fd, foo, strlen(foo) + 1);
    write(fd, bar, strlen(bar) + 1);
    

    You can then pick apart the strings after you read them in (you will still need to read them into one buffer and then break them apart, unless you read them a character at a time).

提交回复
热议问题