简体   繁体   English

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

[英]C - Create/Write/Read named pipe

I want to create a named pipe and then write to it and after that I want read it.我想创建一个命名管道,然后写入它,然后我想读取它。 Here is my code:这是我的代码:

#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;
}

After I write a text, nothing happen anymore.在我写完一篇文章后,什么都没有发生。 And there is no message.而且没有消息。 I have to quit the program with STRG C. Someone know what is wrong?我必须用 STRG C 退出程序。有人知道出了什么问题吗? I have to use the functions mkfifo, fopen, fprintf, fgets and fclose.我必须使用函数 mkfifo、fopen、fprintf、fgets 和 fclose。 So would be nice if I could keep them into the code.如果我可以将它们保留在代码中,那就太好了。

FIFO's don't work nice with just one thread.仅使用一个线程时,FIFO 不能很好地工作。 You'll get blocked on a reading open until a writing open is performed and vice versa, so you'll need to open in RDWR (non-portable) mode or with RDONLY in one thread and WRONLY in another or you'll get blocked.您将在读取打开时被阻塞,直到执行写入打开,反之亦然,因此您需要在 RDWR(不可移植)模式下打开,或者在一个线程中使用 RDONLY,在另一个线程中使用 WRONLY,否则您将被阻塞.

Eg:例如:

fp = fopen(FIFO, "r+");

then you'll need to write no more than the size of the FIFO buffer (which is ulimit -p * 512 ?) (or else you get blocked).那么你只需要写入不超过 FIFO 缓冲区的大小(即ulimit -p * 512 ?)(否则你会被阻塞)。 After that, you'll need to read no more than what you've written.之后,您只需阅读您所写的内容即可。

All in all, this should work (although it's not the usual way to use FIFOs):总而言之,这应该可行(尽管这不是使用 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM