3 无名管道2

本文探讨了无名管道的缓存大小问题,当缓存满时会导致写阻塞。通过一个实例展示了如何计算无名管道的缓存大小,并强调了其只能在有亲缘关系的进程(父子进程)间通信的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 无名管道的缓存是由大小的,缓存满了,会出现写阻塞。

2. 实例1,计算缓存大小:

#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
  int fd[2];
  int ret;
  int i=0;
  char writebuf[]="hello linux";
  char readbuf[128]={0};
  ret=pipe(fd);
  if(ret <0)
  {
	printf("creat pipe failure\n");
	return -1;
  }
  printf("creat pipe sucess fd[0]=%d,fd[1]=%d\n",fd[0],fd[1]);
  while( i < 5456)
  {
   write(fd[1],writebuf,sizeof(writebuf));
   i++;
  }
  
  printf("write pipe end\n");

  close(fd[0]);
  close(fd[1]);
  return 0;
}

执行结果:

alex@alex-virtual-machine:/extra/process/003$ gcc pipe_4.c
alex@alex-virtual-machine:/extra/process/003$ ./a.out
creat pipe sucess fd[0]=3,fd[1]=4
write pipe end

3.实例2:

通过无名管道,父进程先执行,子进程后执行。

先pipe再fork,子进程继承父进程的文件描述符,所以可以父子进程间通信。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值