目录
write写操作阻塞后read读取4Kb才能再写入
-
写操作也是会阻塞的,当缓冲区满的时候,写操作就会阻塞
-
当缓冲中有足够的空间能容纳下本次的写是,本次的写就解除了阻塞。
(read读取4Kb才能解除)
证明—read读取4Kb才能解除
1.mkfifo my_fifo
在数据读出时,FIFO管道中同时清除数据,并且“先进先出"
2.read.c
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int i=1;
int fd = open("my_fifo", O_RDONLY);
while (1)
{
char buff[2048] = {0};
memset(buff, 0, sizeof(buff));
read(fd, buff, 1024);
printf("buff%d = %s\n", i++,buff);
sleep(2);
read(fd, buff, 1024);
printf("buff%d = %s\n", i++,buff);
}
close(fd);
return 0;
}
3.write.c
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(){
int fd = open("my_fifo", O_WRONLY);
char buff[32] = "h";
int count = 0;
while(1){
//在写若干次把管道文件写满后,
//write会阻塞
write(fd, buff, 1);
printf("count = %d\n", ++count);
}
close(fd);
return 0;
}
执行结果
1. 第一次write写操作阻塞在64K
buff3——>还在读
2. 读取4K后,第二次write写操作阻塞在64K+4K=68K
buff5——>已经写入4K
3. 读取4K后,第三次write写操作阻塞在68K+4K=72K
buff9——>已经写入4K