使用mkfifo暂停

时间:2017-06-20 02:35:49

标签: c unix mkfifo

据我所知,根据https://linux.die.net/man/3/mkfifo

我有一个暗示,我必须有读者和作家文件,以便

利用管道文件。下面的源是writer文件,

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

int main(){

    int fd;
    char *myfifo = "./myfifo";

    mkfifo(myfifo, 0777);
    fd = open(myfifo, O_WRONLY);    


    int PID = fork();   

    if(PID == 0){

        execl("./reader.o", "reader", (char*)NULL);

    }

    write(fd, "Rock and roll baby\0", sizeof("Rock and roll baby"));
    close(fd);
    unlink(myfifo);

    return 0;

}

以下提供的来源是读者文件。

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

#define MAX_BUF 1024

int main(){

    int fd;

    char* myfifo = "./myfifo";
    char buf[MAX_BUF];

    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    write(STDOUT_FILENO, buf, MAX_BUF);

    close(fd);
    exit(EXIT_SUCCESS);

    return 0;

}

运行writer文件的可执行文件时,命令提示符进入

在打印换行符后暂停。我对这个问题的假设是因为

编写器文件中的open()无法检测到管道文件,

就是这种情况?

谢谢。

2 个答案:

答案 0 :(得分:1)

我建议您在fork之前创建FIFO,但只在fork之后打开FIFO。这避免了各种各样的问题。在大多数情况下,我使用write()将错误报告给标准错误;它虽然不如使用fprintf(stderr, …)那么方便。

请注意,编写器在消息的末尾写入一个空字节。读取器获取空字节,但在将结果字符数组(它不再是字符串;字符串末尾有一个终结空字节)写入标准输出之前,用新行覆盖它。如果代码使用<stdio.h>来写入数据(例如printf("%s\n", buf)),则不需要用换行符替换空字节。

writer.c

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

#ifndef READER
#define READER "./reader"
#endif

int main(void)
{
    char *myfifo = "./myfifo";

    if (mkfifo(myfifo, 0777) != 0)
    {
        write(STDERR_FILENO, "Failed to create FIFO\n",
                      sizeof("Failed to create FIFO\n") - 1);
    }

    int PID = fork();

    if (PID == 0)
    {
        execl(READER, "reader", (char *)NULL);
        write(STDERR_FILENO, "Failed to execute reader\n",
                      sizeof("Failed to execute reader\n") - 1);
        exit(EXIT_FAILURE);
    }
    if (PID < 0)
    {
        write(STDERR_FILENO, "Failed to fork\n",
                      sizeof("Failed to fork\n") - 1);
        exit(EXIT_FAILURE);
    }

    int fd = open(myfifo, O_WRONLY);
    if (fd < 0)
    {
        write(STDERR_FILENO, "Failed to open FIFO for writing\n",
                      sizeof("Failed to open FIFO for writing\n") - 1);
        unlink(myfifo);
        exit(EXIT_FAILURE);
    }

    write(fd, "Rock and roll baby", sizeof("Rock and roll baby"));
    close(fd);
    unlink(myfifo);
    int corpse;
    int status;
    while ((corpse = wait(&status)) > 0)
        printf("Child %d exited with status 0x%.4X\n", corpse, status);

    return 0;
}

reader.c

#include <fcntl.h>
#include <unistd.h>

#define MAX_BUF 1024

int main(void)
{
    char* myfifo = "./myfifo";
    int fd = open(myfifo, O_RDONLY);
    if (fd < 0)
        write(STDERR_FILENO, "Failed to open FIFO for reading\n",
                      sizeof("Failed to open FIFO for reading\n")-1);
    else
    {
        char buf[MAX_BUF];
        int nbytes = read(fd, buf, MAX_BUF);
        if (nbytes > 0)
        {
            buf[nbytes-1] = '\n';
            write(STDOUT_FILENO, buf, nbytes);
        }
        close(fd);
    }
    return 0;
}

示例输出

Rock and roll baby
Child 43734 exited with status 0x0000

答案 1 :(得分:0)

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

int main(){

    int fd;
    char *myfifo = "./myfifo";

    int PID = fork();   

    if(PID == 0){

        execl("./reader.o", "reader", (char*)NULL);

    }

    mkfifo(myfifo, 0777);
    fd = open(myfifo, O_WRONLY);    

    write(fd, "Rock and roll baby\0", sizeof("Rock and roll baby"));
    close(fd);
    unlink(myfifo);

    return 0;

}

将execl所在的代码体移到

之上

mkfifo(),

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

#define MAX_BUF 1024

int main(){

    sleep(3);

    int fd;

    char* myfifo = "./myfifo";
    char buf[MAX_BUF];

    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    write(STDOUT_FILENO, buf, MAX_BUF);

    close(fd);

    exit(EXIT_SUCCESS);

    return 0;

}

让读者睡眠()3秒钟,程序开始

工作;但是,有没有人知道这两个程序是否可以打开()管道文件

恰好在同一时间?

谢谢。