使用的IO模型若是没有设置就是默认是阻塞的
注意运行环境ubuntu16.04
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char buffer[4096] = {'\0'}; //因为使用电脑的缓存区大小是4096
ssize_t size = 0;
sleep(5); //没有设置是不是阻塞的时候默认的是阻塞的形式,
/* 当程序睡眠5秒钟醒过来的时候还是会等待用户的标准输入的 */
size = read(STDIN_FILENO, buffer, sizeof(buffer));
//若是没有标准输入就让程序休眠等待,知道等到标准输入
if(size < 0)
{
perror("read error!");
exit(1);
}
else if(size == 0)
{
printf("read finished!\n");
}
else
{
if(write(STDOUT_FILENO, buffer, size) != size)
{
perror("write error!");
exit(1);
}
}
return 0;
}
测试结果:
程序编译好之后运行,程序会等待五秒之后,将输入的结果输出出来,若是不进行输入则会阻塞等待
andrew@andrew-Thurley:~/work/iotec$ bin/noblock
ddddd
ddddd
andrew@andrew-Thurley:~/work/iotec$