FIFOs implementation
Posted
by nunos
on Stack Overflow
See other posts from Stack Overflow
or by nunos
Published on 2010-05-20T01:47:56Z
Indexed on
2010/05/20
2:00 UTC
Read the original article
Hit count: 350
Consider the following code:
writer.c
mkfifo("/tmp/myfifo", 0660);
int fd = open("/tmp/myfifo", O_WRONLY);
char *foo, *bar;
...
write(fd, foo, strlen(foo)*sizeof(char));
write(fd, bar, strlen(bar)*sizeof(char));
reader.c
int fd = open("/tmp/myfifo", O_RDONLY);
char buf[100];
read(fd, buf, ??);
My question is:
Since it's not know before hand how many bytes will foo and bar have, how can I know how many bytes to read from reader.c?
Because if I, for example, read 10 bytes in reader and foo and bar are together less than 10 bytes, I will have them both in the same variable and that I do not want.
Ideally I would have one read function for every variable, but again I don't know before hand how many bytes will the data have.
I thought about adding another write instruction in writer.c between the write for foo and bar with a separator and then I would have no problem decoding it from reader.c. Is this the way to go about it?
Thanks.
© Stack Overflow or respective owner