我一直在尝试实现fifo写入和读取,场景如此,writer1向fifo写入4个字节,reader1读取2个字节,reader2读取接下来的2个字节,下面是我做了什么,从FIFO读取/写入
writer.c
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
FILE *file;
unsigned char message[] = {0x66,0x66,0x67,0x67};
file = fopen("fifo1","wb");
fwrite(&message, 1,4,file);
}
reader.c
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
FILE *file;
unsigned char buff[2];
file = fopen("fifo1","rb");
fread(&buff, 1,2,file);
printf("%c\n",buff[0]);printf("%c\n",buff[1]);
}
然后我遵守他们与跑reader1第一终端和reader2第二终端和作家在th e第三终端。
我以为我会在其中一个读取器中获得前两个字节(ff),而在另一个读取器中获得后两个字节(gg),但它没有像我想的那样工作,有人可以让我知道我做错了什么,请注意,我不在乎谁读了前两个字节或后两个字节,这里重要的是两个读取器一次读取2个字节。我使用Ubuntu,GCC mkfifo创建了fifo。
写入和读取与二进制确定 – Ivan
真棒的人,它工作:) – Ivan