C - 创建/写入/读取命名管道

Non*_*ono 3 c fopen file pipe fifo

我想创建一个命名管道,然后写入它,然后我想读取它。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#define FIFO "fifo0001"
int main(intargc, char *argv[]){
    char input[256];
    FILE *fp;
    char str[50];
    printf("Please write some text:\n");
    scanf("%s", input);
    unlink(FIFO); /* Because it already exists, unlink it before */
    umask(0);
    if(mkfifo(FIFO, 0666) == -1){
        printf("Something went wrong");
        return EXIT_FAILURE;
    }
    if((fp = fopen(FIFO, "a")) == NULL){
        printf("Something went wrong");
        return EXIT_FAILURE;
    }
    fprintf(fp, "%s", input);
    if(fgets(str, 50, fp) != NULL){
        puts(str);
    }
    fclose(fp);
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

在我写完一篇文章后,什么都没有发生。而且没有消息。我必须用 STRG C 退出程序。有人知道出了什么问题吗?我必须使用函数 mkfifo、fopen、fprintf、fgets 和 fclose。如果我可以将它们保留在代码中,那就太好了。

PSk*_*cik 5

仅使用一个线程时,FIFO 不能很好地工作。您将在读取打开时被阻塞,直到执行写入打开,反之亦然,因此您需要在 RDWR(非便携式)模式下打开,或者在一个线程中使用 RDONLY,在另一个线程中使用 WRONLY,否则您将被阻塞.

例如:

fp = fopen(FIFO, "r+");
Run Code Online (Sandbox Code Playgroud)

那么您只需要写入不超过 FIFO 缓冲区的大小(即ulimit -p* 512 ?)(否则您将被阻塞)。之后,您只需阅读您所写的内容即可。

总而言之,这应该可行(尽管这不是使用 FIFO 的常用方法):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#define FIFO "fifo0001"

int main(int argc, char *argv[]){
    char input[256] = "hw";
    FILE *fp;
    char str[50];
    printf("Please write some text:\n");
    scanf("%s", input); //!!!

    size_t input_len = strlen(input);

    unlink(FIFO); /* Because it already exists, unlink it before */
    umask(0);
    if(mkfifo(FIFO, 0666) == -1){
        printf("Something went wrong");
        return EXIT_FAILURE;
    }
    if((fp = fopen(FIFO, "r+")) == NULL){
        printf("Something went wrong");
        return EXIT_FAILURE;
    }
    fprintf(fp, "%s", input);
    if(fgets(str, input_len+1, fp) != NULL){
        puts(str);
    }
    fclose(fp);
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)