系统调用分成低速系统调用和其他系统调用两类。低速系统调用是可能会使进程永远阻塞的一类调用调用,他们包含:
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
char buf[50000];
int main(void){
int ntow,nw;
char *ptr;
ntow = read(STDIN_FILENO,buf,sizeof(buf));
fprintf(stderr, "read %d bytes.\n",ntow);
int flags = fcntl(STDOUT_FILENO, F_GETFL,0);
fcntl(STDOUT_FILENO,F_SETFL,flags|O_NONBLOCK);
ptr = buf;
while(ntow > 0){
errno = 0;
nw = write(STDOUT_FILENO,ptr,ntow);
fprintf(stderr,"nwrite = %d, errno = %d\n",nw, errno);
if(nw > 0){
ptr += nw;
ntow -= nw;
}
}
fcntl(STDOUT_FILENO,F_SETFL,flags|~O_NONBLOCK);
}
运行结果:原文:http://blog.csdn.net/todd911/article/details/18599403