管道

1)特点:
1.管道是单向的、先进先出的,它把一个进程的输出和另一个进程的输入连接在一起。
2.一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据
3.管道提供了简单的流控制机制,进程试图读空管道时,进程将阻塞。同样,管道已经满时,进程再试图向管道写入数据,进程将阻塞
4.管道包括无名管道和有名管道两种,前者用于父进程和子进程间的通信,后者可用于运行于同一系统中的任意两个进程间的通信。
2)程序实现
1.无名管道

无名管道创建:
int pipe(int filedis[2]);
当一个管道建立时,它会创建两个文件描述符:
filedis[0] 用于读管道,
filedis[1] 用于写管道

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

            /*创建子进程*/
    if((pid=fork())==0)  //子进程 OR 父进程?
    {
        printf("\n");
        close(pipe_fd[1]);
        sleep(2); /*为什么要睡眠*/
        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);
    }
    return 0;
}

2.有名管道
read文件

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

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

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

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

    /* 打开管道 */
    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(); /*暂停,等待信号*/
    unlink(FIFO); //删除文件
}

write文件

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

    /*打开管道*/
    fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);

    if(argc==1)
    {
        printf("Please send something\n");
        exit(-1);
    }

    strcpy(w_buf,argv[1]);

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

注意点:管道使用和文件操作的区别?

除了创建函数不同,其他的使用函数都相同,唯一不同的是文件中的数据读出来了还存在在文件中,但是FIFO中的数据在读出来之后就消失了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值