양방향 FIFO를 구현하고 싶습니다. 아래 코드는 작동하지만 양방향 FIFO를 사용하지 않습니다. 인터넷을 통해 수색했지만 좋은 예는 발견하지 못했습니다 ...양방향 FIFO
어떻게하면됩니까?
감사합니다,
WRITER.c :
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MAXLINE 4096
#define READ 0
#define WRITE 1
int main (int argc, char** argv)
{
int a, b, fd;
do {
fd=open("/tmp/myfifo",O_WRONLY);
if (fd==-1) sleep(1);
} while (fd==-1);
while (1) {
scanf("%d", &a);
scanf("%d", &b);
write(fd,&a,sizeof(int));
write(fd,&b,sizeof(int));
if (a == 0 && b == 0)
{
break;
}
}
close(fd);
return 0;
}
READER.c :
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#define MAXLINE 4096
#define READ 0
#define WRITE 1
int main(void)
{
int n1, n2;
int fd;
mkfifo("/tmp/myfifo",0660);
fd=open("/tmp/myfifo",O_RDONLY);
while(read(fd, &n1, sizeof(int)))
{
read(fd, &n2, sizeof(int));
if (n1 == 0 && n2 == 0)
{
break;
}
printf("soma: %d\n",n1+n2);
printf("diferenca: %d\n", n1-n2);
printf("divisao: %f\n", n1/(double)n2);
printf("multiplicacao: %d\n", n1*n2);
}
close(fd);
return 0;
}