【问题标题】:Communicate over the fifo queue linux通过fifo队列通信linux
【发布时间】:2019-10-23 14:12:23
【问题描述】:

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

//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;
}

【问题讨论】:

  • 发生了什么错误?请提供有关代码实际执行的操作以及应该执行的操作的更多详细信息。
  • 第一个 prog 应该发送偶数,第二个奇数,第三个应该接收并将这些数字相加(总和)。第一个和第二个程序似乎执行了它们的任务,但第三个没有并冻结。
  • @HIPHOP 请edit您的问题并在此处添加所有要求的信息或说明,而不是编写 cmets。
  • 如果你想在 prog 1 和 2 中写入二进制整数值,你应该使用write(descriptor, &amp;b, sizeof(b));。您当前的程序会将b 中存储的数字解释为地址,并从该地址读取比int 的大小多一个字节。这是未定义的行为。您的程序将发送垃圾数​​据,甚至可能崩溃。代码中的一些错误应该会产生编译器警告。也许您必须启用警告。请修复所有警告。
  • 您在 prog3 中遇到了同样的错误。现在您在 prog3 中使用未初始化的指针 ab。不要使用sizeof(b)+1。这还将在保存变量b 的内存结束后读取一个字节。与我之前评论中的代码进行比较。 顺便说一句: 不能保证 prog3 将从 prog1 中读取一个值并从 prog2 中读取一个值。它也可能发生,例如prog1 先写入所有值,然后 prog2 写入所有值。

标签: c linux ubuntu unix


【解决方案1】:

除了使用正确的地址和要读取的数据大小的错误之外,主要的错误是不应该在循环中反复打开和关闭 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 : %d\n", 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: %d\n", 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 = %d\n", 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

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2013-04-02
    • 2020-06-30
    相关资源
    最近更新 更多