C 在后台使用命名管道的程序不会';我关不好

C 在后台使用命名管道的程序不会';我关不好,c,bash,pipe,named-pipes,named,C,Bash,Pipe,Named Pipes,Named,我试图编写程序,其中一部分是通信使用命名管道(fifo)。当我在bash脚本的无限循环中运行writer和reader程序(writer在后台)时,有一段时间writer程序并没有被收到的坏结果正确关闭 我也尝试在我的程序中使用一个简单的代码: 作家c #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> int main() {

我试图编写程序,其中一部分是通信使用命名管道(fifo)。当我在bash脚本的无限循环中运行writer和reader程序(writer在后台)时,有一段时间writer程序并没有被收到的坏结果正确关闭

我也尝试在我的程序中使用一个简单的代码:

作家c

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";

    /* create the FIFO (named pipe) */
    mkfifo(myfifo, 0666);

    /* write "Hi" to the FIFO */
    fd = open(myfifo, O_WRONLY);
    write(fd, "Hi", sizeof("Hi"));
    close(fd);

    /* remove the FIFO */
    unlink(myfifo);

    return 0;
}
你能帮我做好这件事吗?或者你有没有别的办法


“问候”

在我看来很活泼。当读卡器尝试
打开()
它时,我看不出你在哪里确保已经创建了fifo。我理解你的意思,但每次这样做时,它都能正常工作吗?这只是我的all程序的一部分,我需要在这个程序中始终保持良好的管道通信。也许你对如何运行这个程序有另一个想法?例如,不是在后台,而是以其他方式每次都能正常工作。第一步是不要丢弃调用的函数的返回值。当读卡器尝试
打开()
它时,我看不出你在哪里确保已经创建了fifo。我理解你的意思,但每次这样做时,它都能正常工作吗?这只是我的all程序的一部分,我需要在这个程序中始终保持良好的管道通信。也许你对如何运行这个程序有另一个想法?例如,不是在后台,而是以其他方式每次都能正常工作。第一步是不要丢弃所调用函数的返回值。
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_BUF 1024

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";
    char buf[MAX_BUF];

    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    printf("Received: %s\n", buf);
    close(fd);

    return 0;
}
#!/bin/bash

while true; do
    ./writer &
    ./reader
done