linux 下打开管道错误

linux 下打开管道错误,第1张

mkfifo("MYFIFO", 0777)或者mkfifo("MYFIFO", S_IRUSR|S_IWUSR)

fd = open("MYFIFO", O_RDWR)

记住命名管道要打" "号!

除非是这样:

char fn[] = "MYFIFO"

mkfifo(fn, 0777)或者mkfifo(fn, S_IRUSR|S_IWUSR)

fd = open(fn, O_RDWR)

另外,在write时 open不能加O_NONBLOCK!O_NONBLOCK只用于read,不然会提示no such device or address

创建 fifo 时,写 0777 (8进制的 777)

你用了 nonblock 方式去打开fifo的写端,此时fifo的读端没有人open,所以一定会返回失败。必须先有人以读的方式打开fifo后,才能以nonblock方式打开写。或者去掉 O_NONBLOCK 参数。

这里有两点要注意,第一,如果没有读打开管道,那么写打开管道就阻塞,而且不能非阻塞地打开;第二,如果没有写打开管道,那么读打开管道也阻塞,但是可以在open中指定O_NONBLOCK来非阻塞打开。一般读写分别在两个进程中。但在一个进程中也可以分别打开读写,但要先以非阻塞方式打开读。你的代码阻塞在了读打开管道.

根据你的代码,我重新写了一个实现。先将用户输入保存再DATAFILE中,再经过FIFO将数据读取并写入SAVEFILE中

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <errno.h>

#define FIFO "/tmp/fifo"

#define DATAFILE  "./data_file"

#define SAVEFILE  "./save_file"

static int input_data(const char *filename)

static int process_fifo(const char *filename)

int main(int argc, char *argv[])

{

    input_data(DATAFILE)

    process_fifo(DATAFILE)

    return 0

}

#define BUFSIZE    1024

static int input_data(const char *filename)

{

    printf("input data:\n")

    char buf[BUFSIZE]

    int n=read(STDIN_FILENO,buf,BUFSIZE)

    if(n<0){

        perror("read from stdin error")

        exit(EXIT_FAILURE)

    }

    int fd=open(filename,O_CREAT|O_TRUNC|O_RDWR,0666)

    if(fd<0){

        perror("create data file error")

        exit(EXIT_FAILURE)

    }

    if(n!=write(fd,buf,n)){

        perror("write to data file error")

        exit(EXIT_FAILURE)

    }

    close(fd)

    return 0

}

static int process_fifo(const char *filename)

{

    int rfd=open(filename,O_RDONLY)

    if(rfd<0){

        perror("read from data file error")

        exit(EXIT_FAILURE)

    }

    unlink(FIFO)

    if(mkfifo(FIFO,0666)!=0){

        perror("failed to create FIFO")

        exit(EXIT_FAILURE)

    }

    int fifo_r,fifo_w

    fifo_r=open(FIFO,O_RDONLY|O_NONBLOCK)

    fifo_w=open(FIFO,O_WRONLY)

    if(fifo_r<0||fifo_w<0){

        perror("open FIFO error")

        exit(EXIT_FAILURE)

    }

    int n

    char buf[BUFSIZE]

    /* 从数据文件中读取数据 */

    n=read(rfd,buf,BUFSIZE)

    if(n<0){

        perror("read from data file error")

        exit(EXIT_FAILURE)

    }

    /* 写入管道 */

    if(n!=write(fifo_w,buf,n)){

        perror("write to FIFO error")

        exit(EXIT_FAILURE)

    }

    /* 从管道读取 */

    memset(buf,0,BUFSIZE)

    n=read(fifo_r,buf,BUFSIZE)

    if(n<0){

        perror("read from FIFO error")

        exit(EXIT_FAILURE)

    }

    /* 打开保存数据的文件 */

    int wfd=open(SAVEFILE,O_CREAT|O_TRUNC|O_WRONLY,0666)

    if(wfd<0){

        perror("create save file error")

        exit(EXIT_FAILURE)

    }

    if(n!=write(wfd,buf,n)){

        perror("write to FIFO error")

        exit(EXIT_FAILURE)

    }

    /* 结束 */

    close(rfd)

    close(wfd)

    close(fifo_r)

    close(fifo_w)

    return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8306925.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-15
下一篇2023-04-15

发表评论

登录后才能评论

评论列表(0条)

    保存