管道


1. 管道 是单向的,先进先出的。它把一个进程的输出和另一个进程的输入连接在一起。
一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据。
数据被一个进程读出后,将被从管道中删除,其他读进程将不能再读到这些数据。

管道提供了简单的流控制机制:
a. 进程试图读空管道时,进程将阻塞。
b. 管道满时,进程再试图向管道写入数据时,进程也将阻塞。

2. 管道的分类

管道分为 无名管道 和 有名管道:
无名管道 用于父进程和子进程之间的通信。
有名管道 可用于运行于统一系统中的任意两个进程之间的通信。

3. 无名管道的创建

无名管道由 pipe()函数创建:
int pipe(int filedis[2]);

当一个管道建立时,它会创建两个文件描述符:
filedis[0] --- 用于读管道。
filedis[1] --- 用于写管道。

4. 关闭管道
只需要将这两个文件描述符关闭即可,可以使用普通的close函数逐个关闭。

例 pipe_create.c

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
        int pipe_fd[2];     

        //创建管道 
        if(pipe(pipe_fd) < 0){
                printf("pipe create error\n");
                return -1;
        } else{
                printf("pipe create success\n");
        }   

        //关闭管道 
        close(pipe_fd[0]);
        close(pipe_fd[1]);
}

5. 管道读写
管道用于不同进程间通信,通常先创建一个管道,再通过fork()创建一个子进程,该子进程会继承父进程所创建的管道。

注意: 必须在系统调用fork()之前 调用pipe(),否则子进程将不会继承文件描述符。

例pipe_rw.c

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

int main()
{
        int pipe_fd[2];
        pid_t pid;
        char buf_r[100];
        char* p_wbuf;
        int r_num;

        memset(buf_r, 0, sizeof(buf_r));

        //创建管道
        if(pipe(pipe_fd) < 0){
                printf("pipe create error\n");
                return -1;
        }

        //创建子进程
        if((pid = fork()) == 0){  //子进程
                printf("\n");
                close(pipe_fd[1]); //关闭写管道描述符
                sleep(2); //睡眠2s, 等待父进程写管道

                if((r_num = read(pipe_fd[0], buf_r, 100)) > 0){
                        printf("%d numbers read from the pipe is %s\n", r_num, buf_r);
                }
                close(pipe_fd[0]); //关闭读管道描述符
                exit(0); //退出进程
        } else if(pid > 0){  //父进程
                close(pipe_fd[0]);  //关闭读管道描述符
                if((write(pipe_fd[1], "Hello", 5)) != -1)
                        printf("parent write1 Hello!\n");
                if((write(pipe_fd[1], "Pipe", 5)) != -1)
                        printf("parent write2 Pipe!\n");
                close(pipe_fd[1]); //关闭写管道描述符

                sleep(3);
                waitpid(pid, NULL, 0); //等待子进程退出
                exit(0);
        }
}

6. 命名管道 (FIFO)

无名管道只能实现父子进程通信,通过命名管道,可以实现不相关进程之间数据交换。
实质上命名管道就是一个文件。

a. FIFO创建
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char* pathname, mode_t mode)

pathname --- FIFO文件名
mode --- 属性

一旦创建了一个FIFO,就可用open打开它,一般的文件访问函数(close, read, write等)都可以使用FIFO。

b. FIFO操作
打开FIFO时,非阻塞标志(O_NONBLOCK)对读写产生的影响:
1. 没使用 O_NONBLOCK 时: 访问要求无法满足时,进程将阻塞。如试图读取空的FIFO,将导致进程阻塞。
2. 使用 O_NONBLOCK 时: 访问要求无法满足时不阻塞,立刻出错返回,errno = ENXIO.

例:fifo_read.c

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

#define FIFO "/tmp/myfifo"

main(int argc, char** argv)
{
        char buf_r[100];
        int fd;
        int nread;

        //创建管道FIFO
        if((mkfifo(FIFO, O_CREAT | O_EXCL) < 0 ) && (errno != EEXIST))
                printf("cannot create fifoserver\n");

        printf("Preparing for reading bytes...\n");

        //打开管道FIFO,只读,非阻塞方式
        fd = open(FIFO, O_RDONLY | O_NONBLOCK, 0);

        if(fd == -1){
                perror("open");
                exit(1);
        }

        while(1){
                memset(buf_r, 0, sizeof(buf_r));

                if((nread = read(fd, buf_r, 100)) == -1){
                        if(errno == EAGAIN)
                                printf("no data yet\n");
                }
                printf("read %s from FIFO\n", buf_r);
                sleep(1);
        }
        pause(); //暂停,等待信号
}

 fifo_write.c

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

#define FIFO_SERVER "/tmp/myfifo"

main(int argc, char** argv)
{
        int fd;
        char w_buf[100];
        int nwrite;

        //打开管道, 与fifo_read 的文件名一样,这样才能通信
        fd = open(FIFO_SERVER, O_WRONLY|O_NONBLOCK, 0);

        if(argc == 1){
                printf("Please send something\n");
                exit(-1);
        }
        //拷贝 命令行敲入的字符串 到w_buf[].
        strcpy(w_buf, argv[1]);

        //向管道写入 w_buf 中的数据
        if((nwrite = write(fd, w_buf, 100)) == -1){
                if(errno == EAGAIN)
                        printf("The FIFI has been read yet. Please try later\n");
        } else
                printf("write %s to the FIFO\n", w_buf);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值