Linux进程间通信之无名管道(三)

1.无名管道概述

管道(pipe)又称无名管道。 无名管道是一种特殊类型的文件,在应用层体现为两个打开的文件描述符。

在这里插入图片描述

管道是最古老的UNIX IPC方式,其特点是: 1、半双工,数据在同一时刻只能在一个方向上流动。 2、数据只能从管道的一端写入,从另一端读出。 3、写入管道中的数据遵循先入先出的规则。 4、管道所传送的数据是无格式的,这要求管道的读出方与写入方必须事先约定好数据的格式,如多少字节算一个消息等。 5、管道不是普通的文件,不属于某个文件系统,其只存在于内存中。 6、管道在内存中对应一个缓冲区。不同的系统其大小不一定相同。 7、从管道读数据是一次性操作,数据一旦被读走,它就从管道中被抛弃,释放空间以便写更多的数据。 8、管道没有名字,只能在具有公共祖先的进程之间使用。

2.无名管道创建和使用

	int pipe(int filedes[2]);

功能:
经由参数filedes返回两个文件描述符

参数:
filedes为int型数组的首地址,其存放了管道的文件描述符fd[0]、fd[1]。
filedes[0]为读而打开,filedes[1]为写而打开管道,filedes[0]的输出是filedes[1]的输入。

返回值:
成功:返回0
失败: 返回-1
在这里插入图片描述

3.无名管道读写的特点

从管道中读数据的特点 1、默认用read函数从管道中读数据是阻塞的。 2、调用write函数向管道里写数据,当缓冲区已满时write也会阻塞。 3、通信过程中,读端口全部关闭后,写进程向管道内写数据时,写进程会(收到SIGPIPE信号)退出。 从管道中读数据的特点 编程时可通过fcntl函数设置文件的阻塞特性。 设置为阻塞: fcntl(fd, FSETFL, 0); 设置为非阻塞: fcntl(fd, FSETFL, O_NONBLOCK);

4.参考代码

//=============================================================================
// File Name    : process_vfork.c
// Author       : FengQQ
//
// Description  : 创建无名管道
// Annotation   : 
//
// Created by FengQQ. 2020-9-28
//=============================================================================
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
	int fd[2];
	pid_t pid;
	
	pipe(fd);
	pid = fork();
	if(pid < 0)
	{
		printf("fork create failed...\r\n");
	}
	else if(pid == 0)
	{
		sleep(3);
		write(fd[1],"hello",5);				//fd[1] 写端
	}
	else
	{
		close(fd[1]);
		while(1)
		{
			char buf[256] = "";
			sleep(1);
			read(fd[0],buf,sizeof(buf));	//fd[0] 读端
			printf("[%s]\r\n",buf);
		}
	}
	
	return 0;
}

5.建立管道和创建父子进程间的通信

采用半双工方式通信或全双工方式通信。

5.1.参考代码

//=============================================================================
// File Name    : process_vfork.c
// Author       : FengQQ
//
// Description  : 建立管道和创建父子进程间的通信
// Annotation   : 半双工通信
//
// Created by FengQQ. 2020-9-28
//=============================================================================
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>


int main(int argc,char *argv[])
{
	int fd[2];
	pid_t pid;
	int ret;
	
	ret = pipe(fd);		//创建无名管道
	if(ret == -1)
	{
		perror("pipe.\n");
		exit(1);
	}
	
	pid = fork();
	if(pid == -1)
	{
		perror("fork.\n");
		exit(1);
	}
	else if(pid == 0)
	{
		char buff[256];
		close(fd[1]);
		read(fd[0],buff,256);
		printf("Form parent say: %s\n",buff);
		close(fd[0]);
	}
	else
	{
		char *say = "Hello child.";
		close(fd[0]);
		write(fd[1],say,strlen(say)+1);
		close(fd[1]);
		int status;
		wait(&status);
	}
	
	return 0;
	
}

5.2.参考代码

//=============================================================================
// File Name    : process_pipe_full_duplex.c
// Author       : FengQQ
//
// Description  : 建立管道和创建父子进程间的通信
// Annotation   : 全双工通信
//
// Created by FengQQ. 2020-9-28
//=============================================================================
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>


int main(int argc,char *argv[])
{
    char *parent_talk[]= {"Hello","Can you tell me what time is it?","Ok,I must go,Bye",NULL};
    char *child_talk[]= {"Hi","No problem","See you,Bye",NULL};
    int fd1[2],fd2[2];
    pid_t pid;
    int ret;
    ret= pipe(fd1);
    if(ret== -1)
    {
        printf("pipe1 create failed...\r\n");
        exit(1);
    }
    ret= pipe(fd2);
    if(ret < 0)
    {
        printf("pipe2 create failed...\r\n");
        exit(1);
    }
    pid= fork();
    if(pid < 0)
    {
        printf("fork create failed...\r\n");
        exit(1);
    }
    else if(pid == 0)
    {
        char buff[256];
        close(fd1[1]);
        close(fd2[0]);
        int i= 0;
        char *talk= child_talk[i];
        while(talk!= NULL)
        {
            read(fd1[0],buff,256);
            printf("Parent say: %s\n",buff);
            write(fd2[1],talk,strlen(talk)+ 1);
            talk= child_talk[++i];
        }
        close(fd1[0]);
        close(fd2[1]);
    }
    else 
    {
        char buff[256];
        close(fd1[0]);
        close(fd2[1]);
        int i= 0;
        char *talk= parent_talk[i];
        while(talk!= NULL)
        {
            write(fd1[1],talk,strlen(talk)+ 1);
            read(fd2[0],buff,256);
            printf("Child say: %s\n",buff);
            talk= parent_talk[++i];
        }
        close(fd1[1]);
        close(fd2[0]);
        int status;
        wait(&status);
    }
    return 0;
	
}

Linux进程间通信之有名管道(四)
链接: link.(https://blog.csdn.net/qq_39721016/article/details/119280878?spm=1001.2014.3001.5501)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值