- 论坛徽章:
- 0
|
APUE356页上面有个程序
char buf[500000];
int main(void){
int ntowrite, nwrite;
char *ptr;
ntowrite=read(STDIN_FILENO,buf,sizeof(buf));
fprintf(stderr,"read %d bytes\n", ntowrite);
set_fl(STD_FILENO,O_NONBLOCK);
ptr=buf;
while(ntowrite>0){
errno=0;
nwrite=write(STDOUT_FILENO,ptr,ntowrite);
fprintf(stderr,"nwrite=%d,errno=%d\n",nwrite,errno);
if(nwrite>0){
ptr+=nwrite;
ntowrite-=nwrite;
}
}
clr_fl(STD_FILENO,O_NONBLOCK);
exit(0);
}
书上说当标准输出为普通文件时,write可以只执行一次
而当标准输出为终端时,会进行数千次write调用,但是只有10-20次才会输出数据 其余都是出错
write有时会返回小于500000的一个数字 有时会出错返回 结果如下:
nwrite=216041,errno=0;
nwrite=-1,errno=11;
... 重复上述错误1497次
nwrite=16015,errno=0;
nwrite=-1,errno=11;
... 重复上述错误1856次
...
nwrite=7949,errno=0
我的问题就是为什么write一个普通文件和终端时会不一样呢?为什么在write一个终端时,每次返回的数值都不同甚至在大多数时候都是出错呢?
|
|