c-通过fifo队列linux进行通信



这是我们的任务:编写三个通过fifo队列进行通信的程序。第一个程序发送偶数,第二个程序发送奇数,第三个程序接收把数字加在一起。所有流程都应在屏幕我试图解决它,但我的代码无法正常工作,可能是3程序中的问题,请帮助。第一个prog应该发送偶数,第二个应该发送奇数,第三个应该接收并将这些数字相加(sum(。第一个和第二个程序似乎执行了它们的任务,但第三个程序没有,并且冻结了我的代码:

//1程序

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

void prog1(int descriptor,char *fifo)
{
int a=0;
int i=0;
while (i!=10)
{
a+=2;
descriptor = open(fifo, O_WRONLY);
write(descriptor,&a, sizeof(a)+1);
puts("");
printf(" Even number : %d",a);
close(descriptor);
i++;
}
}
int main()
{
int descriptor;
char * fifo = "/tmp/myfifo_file";
mkfifo(fifo, 0666);
prog1(descriptor,fifo);
return 0;
}

//2程序

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
void prog2(int descriptor,char *fifo)
{
int b=1;
int i=0;
while (i!=10)
{

descriptor = open(fifo, O_WRONLY);
write(descriptor, &b, sizeof(b)+1);
puts("");
printf("Odd number : %d",b);
b+=2;
close(descriptor);
i++;
}
}
int main()
{
int descriptor;
char * fifo = "/tmp/myfifo_file";
mkfifo(fifo, 0666);
prog2(descriptor,fifo);
return 0;
}

//3程序

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

void prog3(int descriptor,char *fifo)
{
int *a;
int *b;
int i=0;
while (i!=10)
{

descriptor = open(fifo, O_RDONLY);
read(descriptor,a, sizeof(a));
close(descriptor);
descriptor = open(fifo, O_RDONLY);
read(descriptor,b, sizeof(b));
close(descriptor);
printf("Sum : %d",(*a)+(*b));
i++;
}
}
int main()
{
int descriptor;
char * fifo = "/tmp/myfifo_file";
mkfifo(fifo, 0666);
prog3(descriptor,fifo);
return 0;
}

除了使用正确地址和要读取的数据大小的错误外,主要错误是不应在循环中重复打开和关闭FIFO。

打开FIFO进行读取或写入将被阻塞,直到另一端也被打开。因此,prog1和prog2都将等待,直到prog3打开FIFO进行读取,而prog3将等待,直至prog1和prog2中的至少一个打开FIFO进行写入。

关闭写入端的FIFO将导致读取端出现EOF情况。关闭读取端的FIFO将导致写入端出现"管道破裂"错误。

这些程序也缺乏错误处理。mkfifoopenreadwriteclose可能返回错误,readwrite也可能返回的字节数小于您想要读取或写入的字节数。在您的示例中,少量数据可能不会发生这种情况,但当您尝试传输大量数据时,您很可能会注意到这一点。

以下是您程序的改进版本:

prog1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

void prog1(char *fifo)
{
int descriptor;
int a=0;
int i=0;
descriptor = open(fifo, O_WRONLY);
while (i!=10)
{
a+=2;
write(descriptor,&a, sizeof(a));
printf("Even number %d : %dn", i, a);
i++;
}
close(descriptor);
}
int main(void)
{
char * fifo = "/tmp/myfifo_file";
mkfifo(fifo, 0666);
prog1(fifo);
return 0;
}

prog2

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
void prog2(char *fifo)
{
int descriptor;
int b=1;
int i=0;
descriptor = open(fifo, O_WRONLY);
while (i!=10)
{
write(descriptor, &b, sizeof(b));
printf("Odd number %d: %dn", i, b);
b+=2;
i++;
}
close(descriptor);
}
int main(void)
{
char * fifo = "/tmp/myfifo_file";
mkfifo(fifo, 0666);
prog2(fifo);
return 0;
}

prog3

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

void prog3(char *fifo)
{
int descriptor;
int a;
int b;
int i=0;
descriptor = open(fifo, O_RDONLY);
while (i!=10)
{
read(descriptor,&a, sizeof(a));
read(descriptor,&b, sizeof(b));
printf("Sum %d: %d + %d = %dn", i, a, b, a+b);
i++;
}
close(descriptor);
}
int main(void)
{
char * fifo = "/tmp/myfifo_file";
mkfifo(fifo, 0666);
prog3(fifo);
return 0;
}

当运行程序时,我得到这个(变化的(输出:

$ ./prog1&./prog2&
[1] 8410
[2] 8411
$ ./prog3         
Sum 0: 1 + 2 = 3
Odd number 0: 1
Even number 0 : 2
Odd number 1: 3
Sum 1: 3 + 4 = 7
Even number 1 : 4
Odd number 2: 5
Sum 2: 5 + 6 = 11
Odd number 3: 7
Even number 2 : 6
Odd number 4: 9
Even number 3 : 8
Sum 3: 7 + 9 = 16
Odd number 5: 11
Even number 4 : 10
Odd number 6: 13
Sum 4: 8 + 11 = 19
Even number 5 : 12
Odd number 7: 15
Sum 5: 10 + 13 = 23
Even number 6 : 14
Odd number 8: 17
Sum 6: 12 + 15 = 27
Odd number 9: 19
Even number 7 : 16
Sum 7: 14 + 17 = 31
Even number 8 : 18
Sum 8: 16 + 19 = 35
Even number 9 : 20
Sum 9: 18 + 20 = 38
[2]  + 8411 done       ./prog2
[1]  + 8410 done       ./prog1

最新更新