linux 的 fifos 问题

发布于 2024-09-03 15:07:29 字数 1898 浏览 5 评论 0原文

我在调试时遇到问题,为什么 client.c 中的 read_from_fifo 函数中的 n_bytes 与写入 fifo 的值不对应。它应该只写入 25 个字节,但它会尝试读取更多字节(准确地说是 1836020505 个字节(!))。知道为什么会发生这种情况吗?

server.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include <sys/stat.h>

typedef enum { false, true } bool;

//first read the int with the number of bytes the data will have
//then read that number of bytes
bool read_from_fifo(int fd, char* var)
{
    int n_bytes;
    if (read(fd, &n_bytes, sizeof(int)))
    {
        printf("going to read %d bytes\n", n_bytes);
        if (read(fd, var, n_bytes))
            printf("read var\n");
        else {
            printf("error in read var. errno: %d\n", errno);
            exit(-1);
        }
    }

    return true;
}

int main()
{
    mkfifo("/tmp/foo", 0660);
    int fd = open("/tmp/foo", O_RDONLY);
    char var[100];
    read_from_fifo(fd, var);
    printf("var: %s\n", var);
    return 0;
}

client.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

typedef enum { false, true } bool;

//first write to fd a int with the number of bytes that will be written afterwards
bool write_to_fifo(int fd, char* data)
{
    int n_bytes = (strlen(data)) * sizeof(char);
    printf("going to write %d bytes\n", n_bytes);
    if (write(fd, &n_bytes, sizeof(int) != -1))
        if (write(fd, data, n_bytes) != -1)
            return true;
    return false;
}


int main()
{
    int fd = open("/tmp/foo", O_WRONLY);
    char data[] = "some random string abcdef";
    write_to_fifo(fd, data);
    return 0;
}

非常感谢您的帮助。提前致谢。

I am having problem debugging why n_bytes in read_from_fifo function in client.c doesn't correspond to the value written to the fifo. It should only write 25 bytes but it tries to read a lot more (1836020505 bytes (!) to be exact). Any idea why this is happening?

server.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include <sys/stat.h>

typedef enum { false, true } bool;

//first read the int with the number of bytes the data will have
//then read that number of bytes
bool read_from_fifo(int fd, char* var)
{
    int n_bytes;
    if (read(fd, &n_bytes, sizeof(int)))
    {
        printf("going to read %d bytes\n", n_bytes);
        if (read(fd, var, n_bytes))
            printf("read var\n");
        else {
            printf("error in read var. errno: %d\n", errno);
            exit(-1);
        }
    }

    return true;
}

int main()
{
    mkfifo("/tmp/foo", 0660);
    int fd = open("/tmp/foo", O_RDONLY);
    char var[100];
    read_from_fifo(fd, var);
    printf("var: %s\n", var);
    return 0;
}

client.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

typedef enum { false, true } bool;

//first write to fd a int with the number of bytes that will be written afterwards
bool write_to_fifo(int fd, char* data)
{
    int n_bytes = (strlen(data)) * sizeof(char);
    printf("going to write %d bytes\n", n_bytes);
    if (write(fd, &n_bytes, sizeof(int) != -1))
        if (write(fd, data, n_bytes) != -1)
            return true;
    return false;
}


int main()
{
    int fd = open("/tmp/foo", O_WRONLY);
    char data[] = "some random string abcdef";
    write_to_fifo(fd, data);
    return 0;
}

Help is greatly appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-09-10 15:07:29

read(2) 错误的返回值为 -1,而不是 0。因此,至少第一个 4 字节读取的 if 语句是错误的。

The return value for an error from read(2) is -1, not 0. So your if statement for the first 4-byte read, at least, is wrong.

坏尐絯℡ 2024-09-10 15:07:29

您是否验证 read_from_fifo() 函数打印的 nbytes 是否显示正确的值?
请注意,在 write(fd, data, n_bytes) 上,您没有写入字符串字符 '\0' 的末尾,并且每当您通过 read(fd, var, n_bytes) 读取它时,您都没有添加 '\0'到刚刚读取的字符串末尾,因此 printf("var: %s\n", var);可能会显示非 \0 结尾的字符串,从而导致不可预测的结果。

Did you verify if nbytes printed by read_from_fifo() function displayed the correct value ?
Just notice that on write(fd, data, n_bytes) you did not write the end of string char '\0', and whenever you read it by read(fd, var, n_bytes), you did not added a '\0' to the end of string just read, so the printf("var: %s\n", var); could display a not \0 ended string resulting unpredicted results.

つ可否回来 2024-09-10 15:07:29

我自己找到了解决方案。

不管你信不信,问题是“)”。 n_bytes 变量是正确的,问题是我没有将其写入 fifo。

这个 (write(fd, &n_bytes, sizeof(int) != -1))

应该是这个 (write(fd, &n_bytes, sizeof(int)) != - 1)

无论如何,感谢您的回答。

I have found the solution myself.

The problem is a ')' believe it or not. The n_bytes variable is correct, the problem is that I am not writing that to the fifo.

this (write(fd, &n_bytes, sizeof(int) != -1))

should be this (write(fd, &n_bytes, sizeof(int)) != -1)

Thanks anyway for your answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文