进程间通信-管道

管道

管道的由来:不同进程对于同一文件的读写时,进程一对文件读的时候,进程二需要等到进程一读完关闭文件,进程二再打开进行相应的操作;而管道却可以实现多个进程对同一文件边读边写;

无名管道PIPE特征
  1. 没有名字,无法使用open()(可以使用read\write)

  2. 只能用于亲缘进程(父子进程、兄弟进程、祖孙进程)间通信(由于不能open,所以只能继承传递)

  3. 半双工工作方式:可读可写但不能同时进行

  4. 写入操作不具有原子性,只能用于一对一简单通信(对写操作不做任何保护,数据会产生数据践踏)

  5. 不能使用lseek定位

函数-PIPE
#include <unistd.h>
/*
功能:创建无名管道
参数:至少为两个int型的数组
返回值:成功返回0 失败返回-1
*/
int pipe(int pipefd[2]);
案例
//父进程写,子进程读
int main()
{
    int fd[2];	//f[0]:读	f[1]:写
    //pipe函数一定要在子进程被创建之前调用,不然无法被子进程继承
    int ret = pipe(fd);
    
    if(ret < 0)
    {
        perror("pipe error");
        return -1;
    }
    
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork error");
        return -1;
    }
    //父进程
    if(pid > 0)
    {
        while(1)
        {
        	char buf[100];
        	bzero(buf,sizeof(buf));
        
        	fgets(buf, sizeof(buf), stdin);
        	write(fd[1], buf, strlen(buf));
        }
    }
    //子进程
    if(pid == 0)
    {
        while(1)
        {
        	char buf[100];
        	bzero(buf,sizeof(buf));
        
        	read(fd[0], buf, sizeof(buf));
        	printf("from parent:%s\n", buf);
        }
    }
    
    return 0;
}
//两个子进程写,一个父进程进行读(验证踩踏)
//或者两个读,一个写,则会出现只能一个读到,(这就是管道只能读一次)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    int fd[2];

    int ret = pipe(fd);

    if(ret < 0)
    {
        perror("pipe error");
        return -1;
    }

    pid_t pid1 = fork();

    if(pid1 < 0)
    {
        perror("fork error");
        return -1;
    }


    if(pid1 == 0)
    {
        char buf[100] = "abcdefghijk";

        write(fd[1], buf, strlen(buf));
        exit(0);
    }
    //父进程
    if(pid1 > 0)
    {
        pid_t pid2 = fork();

        if(pid2 == 0)
        {
            char buf[100] = "lmnopqrstuvwxyz";

            write(fd[1], buf, strlen(buf));
            exit(0);
        }
        sleep(2);
        char buf[100] = {0};
        bzero(buf,sizeof(buf));

        read(fd[0], buf, sizeof(buf));
        printf("%s\n", buf);
        exit(0);
    }

    pause();
    return 0;
}
有名管道FIFO特征
  1. 有名字,存储于普通文件(无关联进程也可以使用),可以使用open/read/write来打开/读/写

  2. 具有写入原子性,多进程同时书写不会互相践踏

  3. 存取方式遵循队列(先进先出)原则

  4. 不能用lseek定位

函数-FIFO
#include <sys/types.h>
#include <sys/stat.h>
/*
功能:创建有名管道FIFO
参数1:待创建的FIFO文件名
参数2:文件权限
返回值:成功返回0 失败返回-1
*/
int mkfifo(const char* pathname, mode_t mode);
案例
//两个文件进行单向通信
//文件1
int main(int argc, char* argv[])
{
    //判断文件是否存在,如果存在返回0
    if(access("fifo", F_OK))
    {
        //不存在则创建文件
        mkfifo("fifo", 0644);
    }
    
    int fd = open("fifo", O_WRONLY);
    
    char buf[100];
    while(1)
    {
        bzero(buf, sizeof(buf));
        fgets(buf, sizeof(buf), stdin);
        write(fd, buf, strlen(buf));
    }
}
//文件2
int main(int argc, char* argv[])
{
    //判断文件是否存在,如果存在返回0
    if(access("fifo", F_OK))
    {
        //不存在则创建文件
        mkfifo("fifo", 0644);
    }
    
    int fd = open("fifo", O_RDONLY);
    
    char buf[100];
    while(1)
    {
        bzero(buf, sizeof(buf));
        read(fd, buf, sizeof(buf));
        fputs(buf);
        
        //将以下代码代替上一句fputs就可实现多进程写入文件
        //另开一个窗口运行文件1就是一个新的进程
        //open("1.txt", O_RDWR | O_APPEND);
        //write(fd_log, buf, strlen(buf));
        //close(fd_log);
    }
}
总结

FIFO和PIPE最大的区别就是FIFO具有写入原子性的特点。(典型案例:Linux日志系统,多个进程向管道写入,再通过一个进程来读取管道内容,就可以解决多个进程向同一文件写入内容)管道文件的内容和水流一样,只能读取一次,读完之后就会消失

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值