Linux进程间通信之有名管道和无名管道

(无名)管道pipe有以下几个注意的:

1.管道只能两个进程单向通信,数据只能从一个进程流向另一个进程(其中管道一端用写数据,另一端读)即半双工管道;

2.只能用于有亲缘关系的进程间;

3.是通过创建管道时,系统设置的文件描述符进行的。管道就是一个特殊的文件,这个文件只存在于内存中。在创建管道时

系统会为管道分配一个页面作为数据缓冲区,进行管道通信的两个进程通过读写这个缓冲区来进行通信;

4.它传递的是无格式的字节流;

5.写入的数据每次都添加在管道缓冲区的末尾,读数据的时候是从缓冲区的头部读出数据。

 

下面的代码是借用管道使得父子间进程简单的能通讯的代码:

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<string.h>

int main ()
{
    char *parent_talk[] = {"Hello!", "What's the time?", "Ok", "Thank You!", NULL};
    char *child_talk[] = {"Hi!", "Oh,wait,,,","It's 19:56","My pleasure~", NULL};

    int pipefd1[2];
    int pipefd2[2];
    int ret1 = pipe(pipefd1);//用pipe()创建管道,其中,pipefd1[0]用于读,pipefd1[1]用于写
    int ret2 = pipe(pipefd2);

    if(ret1 == -1)
    {
       perror("pipe");
       return -1;
    }
    if(ret2 == -1)
    {
       perror("pipe.");
       return -1;
    }

    pid_t pid = fork();//通过fork()函数创建父子进程
   
    if(pid == 0)//子进程
    {
       close(pipefd1[1]);//关闭写管道,此时需要子进程从读管道读取数据
       close(pipefd2[0]);//关闭读管道,此时需要子进程向写管道写入数据

       char buf[256];
       int  j = 0;
       char *child = child_talk[j];

       while(child != NULL)
       {
           read(pipefd1[0], buf, 256 );
           printf("Parent : > %s\n",buf);
           write(pipefd2[1], child, strlen(child)+1);
           child = child_talk[++j];
       }
       close(pipefd1[0]);//关闭读管道,此时子进程已经读完数据
       close(pipefd2[1]);//关闭写管道,此时子进程已经写完数据
    }
    else if(pid > 0)//父进程
    {  
       close(pipefd1[0]);//关闭读管道,此时需要父进程向写管道写入数据
       close(pipefd2[1]);//关闭写管道,此时需要父进程从读管道读取数据
       char buf[256];
       int i = 0;
       char *parent = parent_talk[i];
       while(parent != NULL)
       {
           write(pipefd1[1], parent, strlen(parent)+1);
           read(pipefd2[0], buf, 256);
           printf("Child : > %s\n", buf);
           parent = parent_talk[++i];
       }
       close(pipefd1[1]);//关闭写管道,此时父进程已经向写管道写完数据
       close(pipefd2[0]);//关闭读管道,此时父进程已经从读管道读完数据
       int status;
       wait(&status);//等待子进程结束
    }
    else
    {
       perror("fork.");
    }

    return 0;
}
//你会发现子进程的读写顺序和父进程的相反
/*********************************************/

 //下面这个比较简单就只是打印一句话
/*
int main ()
{
    int pipefd[2];
    int ret = pipe(pipefd);
    if(ret == -1)
    {
       perror("pipe.");
       return -1;
    }
    pid_t pid = fork();

    if(pid == 0)
    {
       char buf[256];
       close(pipefd[1]);
       read(pipefd[0], buf, 256);
       printf("Parent Say:> %s\n", buf);
       close(pipefd[0]);
    }
    else if(pid > 0)
    {
       char *say = {"Good Good Study Day Day Up!"};
       close(pipefd[0]);
       write(pipefd[1], say, strlen(say)+1);
       close(pipefd[1]);
       int status;
       wait(&status);
    }
    else 
    {
       perror("fork.");
    } 

    return 0;
}
*/

运行结果:

有名管道(FIFO):

最大的优点:可以在无亲缘关系的两个进程间通信;

缺点:需要打开open()  、关闭close() 管道文件;

具体操作过程看代码和注释:

//unitil.h文件
#pragma once

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>

#define BUFFER_SIZE 256

****************************************

//以下是ser.c 
#include"utili.h"

int main()
{
    int ret = mkfifo("write_fifo", 0755);//创建写管道
    if(ret == -1)
    {
        perror("mkfifo.");
        return -1;
    }

    int write_fd = open("write_fifo", O_WRONLY);  //打开写管道,返回一个write_fd(文件描述符),这个文件描述符就相当于一个指向该管道文件的指针(可以这样理解)

    if(write_fd == -1)
    {
        perror("open write file.");
        return -1;
    }

    int read_fd = open("read_fifo", O_RDONLY);//打开读管道,原理和写管道差不多
    if(read_fd == -1)
    {
        perror("open read file");
        return -1;
    }

    char sendbuf[BUFFER_SIZE];
    char recvbuf[BUFFER_SIZE];
    while(1)
    {
        printf("Ser:>");
        scanf("%s",sendbuf);//将读入的数据放进sendbuf这个数组中
        write(write_fd, sendbuf, strlen(sendbuf)+1);//将sendbuf里面strlen(sendbuf)+1长度的数据写进write_fd所指的写管道里

        read(read_fd, recvbuf, BUFFER_SIZE);//从read_fd所指的读管道里,读取BUFFER_SIZE个字节的数据放到recvbuf里
        printf("Cli:>%s\n",recvbuf);
    }

    close(write_fd);//关闭写管道
    close(read_fd);//关闭读管道
    return 0;
}

********************************************************
以下是cli.c
#include"utili.h"

//cli
int main()
{
    int read_fd = open("write_fifo", O_RDONLY); //pipefd[1]它要读的是ser的写文件
    if(read_fd == -1)
    {
        perror("open read file.");
        return -1;
    }

    int ret = mkfifo("read_fifo", 0755);
    if(ret == -1)
    {
        perror("mkfifo.");
        return -1;
    }

    int write_fd = open("read_fifo", O_WRONLY); //它要写的是ser的读文件
    if(write_fd == -1)
    {
        perror("open write file.");
        return -1;
    }

    char sendbuf[BUFFER_SIZE];
    char recvbuf[BUFFER_SIZE];
    while(1)
    { 
        read(read_fd, recvbuf, BUFFER_SIZE);
        printf("Ser:>%s\n",recvbuf);
        printf("Cli:>");
        scanf("%s",sendbuf);
        write(write_fd, sendbuf, strlen(sendbuf)+1);
    }

    close(write_fd);
    close(read_fd);
    return 0;
}

运行试试:

现在貌似只能一人说一句,然后等对方有回应了,你才能说下一句。?(以空格为单位,像我一次打了Nice to meet you!这句话时它就给你分开来回应...)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值