Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 不要读它应该读的东西_C_Linux_Process_Pipe - Fatal编程技术网

C 不要读它应该读的东西

C 不要读它应该读的东西,c,linux,process,pipe,C,Linux,Process,Pipe,所以我有两个进程,一个客户端获得两个操作数,一个操作+或-,将它们发送到第二个进程,服务器进行计算,然后将结果发送回客户端。这是客户: #define FIFO_NAME1 "fifo1" #define FIFO_NAME2 "fifo2" int main() { if (access(FIFO_NAME1, 0) == 0){ unlink(FIFO_NAME1); } int fd1 = -1, fd2 = -1; int res

所以我有两个进程,一个客户端获得两个操作数,一个操作+或-,将它们发送到第二个进程,服务器进行计算,然后将结果发送回客户端。这是客户:

#define FIFO_NAME1 "fifo1"
#define FIFO_NAME2 "fifo2"

int main()
{   
    if (access(FIFO_NAME1, 0) == 0){
        unlink(FIFO_NAME1);
    }

    int fd1 = -1, fd2 = -1;
    int result, send[3];
    char sign;

    if(mkfifo(FIFO_NAME1, 0600) != 0){
        perror("Err creating FIFO");
        return 1;
    }

    fd1 = open(FIFO_NAME1, O_WRONLY);
    if(fd1 == -1) {
        perror("Could not open FIFO");
        return 1;   
    }
    fd2 = open(FIFO_NAME1, O_RDONLY);
    if(fd2 == -1) {
        perror("Could not open FIFO");
        return 1;   
    }

    printf("Write expression: (x sign y): \n");
    scanf("%d %c %d", &send[0], &sign, &send[1]);

    if (sign == '+')
        send[2] = 1;
    else if (sign == '-')
        send[2] = 2;

    //cheking if reading was good
    for(int i = 0; i < 3; i++)
        printf("%d ", send[i]);
    printf("\n");

    write(fd1, send, sizeof(int) * 3);

    read(fd2, &result, sizeof(int));

    printf("result: %d\n", result);

    close(fd1);
    close(fd2);
    unlink(FIFO_NAME1);

    return 0;
}
这是服务器:

#define FIFO_NAME1 "fifo1"
#define FIFO_NAME2 "fifo2"

int main()
{
    if (access(FIFO_NAME2, 0) == 0){
        unlink(FIFO_NAME2);
    }

    int fd1 = -1, fd2 = -1;
    int result, recive[3];

    if(mkfifo(FIFO_NAME2, 0600) != 0){
        perror("Err creating FIFO");
        return 1;
    }

    fd1 = open(FIFO_NAME1, O_RDONLY);
    if(fd1 == -1) {
        perror("Could not open FIFO1");
        return 1;
    }
    fd2 = open(FIFO_NAME1, O_WRONLY);
    if(fd2 == -1) {
        perror("Could not open FIFO2");
        return 1;
    }

    read(fd1, recive, sizeof(int) * 3);
    //checking if reading form pipe is good
    printf("recives %d sign:%d %d\n", recive[0], recive[2], recive[1]);

    if(recive[2] == 1)
        result = recive[0] + recive[1];
    else if (recive[1] == 2)
        result = recive[0] - recive[1];

    //cheking result before writing to pipe
    printf("%d\n", result);

    write(fd2, &result, sizeof(int));

    close(fd1);
    close(fd2);
    unlink(FIFO_NAME2);

    return 0;
} <br>

此代码有3个结果。首先,它是有效的,这是最不常见的情况。第二个问题是,从服务器读取的数据不正确,我在服务器中进行了数据检查。第一个操作数读取正确,但符号和第二个操作数是垃圾值。第三种情况是正确读取操作数和符号,但结果仍然是垃圾。我不知道为什么会发生第三种情况。所以数据是正确读取的,我的代码进行计算看起来非常直接和简单,但不起作用。在发送回服务器之前,我在服务器中进行了一次结果检查,然后再次检查,结果发现它有垃圾值。我在这里错过了什么

问题可能是,所有通信都是通过FIFO1完成的,因此操作数和结果会混淆

在客户端中,您打开FIFO1进行写入和读取,然后向其写入和读取:

  fd1 = open(FIFO_NAME1, O_WRONLY);
    if(fd1 == -1) {
        perror("Could not open FIFO");
        return 1;   
    }
    fd2 = open(FIFO_NAME1, O_RDONLY); //should be FIFO_NAME2
    if(fd2 == -1) {
        perror("Could not open FIFO");
        return 1;   
    }
  ...
 write(fd1, send, sizeof(int) * 3);

 read(fd2, &result, sizeof(int));
在服务器中,它是相同的:

 fd1 = open(FIFO_NAME1, O_RDONLY);
    if(fd1 == -1) {
        perror("Could not open FIFO1");
        return 1;
    }
    fd2 = open(FIFO_NAME1, O_WRONLY); //should be FIFO_NAME2
    if(fd2 == -1) {
        perror("Could not open FIFO2");
        return 1;
    }

因此,FIFO1最终有两个侦听器:管道提供的通信通道是字节流:没有消息边界的概念。这意味着,您不能假设一次读取将从一次写入中接收所有数据。代码应检查read的返回值,以查看接收了多少数据,并继续读取,直到所有数据都已接收。检查函数返回值始终是最佳做法。这可能是问题的根本原因,也可能不是问题的根本原因,但它应该得到修复并重新测试。是的,我复制粘贴了fd2的open和if,忘记更改为FIFO_NAME2。谢谢