- 论坛徽章:
- 5
|
msg:Success代表perror函数读到结尾时显示的格式
你确定FD_ISSET(fds[0],&inset1)这个返回值只有第一次为真??- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <time.h>
- #include <errno.h>
- #include <string.h>
- int main(void)
- {
- int fds[2];
- char buf[7];
- int i,rc,maxfd;
- fd_set inset1,inset2;
- struct timeval tv;
- /*首先按一定的权限打开hello1文件*/
- if((fds[0] = open ("hello1", O_RDWR|O_CREAT,0666))<0)
- perror("open hello1");
- /*再按一定的权限打开hello2文件*/
- if((fds[1] = open ("hello2", O_RDWR|O_CREAT,0666))<0)
- perror("open hello2");
- if((rc = write(fds[0],"Hello!\n",7)))
- printf("rc=%d\n",rc);
- lseek(fds[0],0,SEEK_SET);
- /*取出两个文件描述符中的较大者*/
- maxfd = fds[0]>fds[1] ? fds[0] : fds[1];
- /*初始化读集合inset1,并在读集合中加入相应的描述集*/
- FD_ZERO(&inset1);
- FD_SET(fds[0],&inset1);
- /*初始化写集合inset2,并在写集合中加入相应的描述集*/
- FD_ZERO(&inset2);
- FD_SET(fds[1],&inset2);
- tv.tv_sec=1;
- tv.tv_usec=0;
- /*循环测试该文件描述符是否准备就绪,并调用select函数对相关文件描述符做对应操作*/
- while(FD_ISSET(fds[0],&inset1)||FD_ISSET(fds[1],&inset2)){
- if(select(maxfd+1,&inset1,&inset2,NULL,&tv)<0)
- perror("select");
- else{
- if(FD_ISSET(fds[0],&inset1)){
- rc = read(fds[0],buf,7);
- if(rc>0){
- buf[rc]='\0';
- printf("read: %s\n",buf);
- }else
- {
- //errno=1;
- printf("Here:%d:%s\n",errno,strerror(errno));
- perror("read");
- }
- }
- if(FD_ISSET(fds[1],&inset2)){
- rc = write(fds[1],buf,7);
- if(rc>0){
- buf[rc]='\0';
- printf("rc=%d,write: %s\n",rc,buf);
- }else
- perror("write");
- sleep(10);
- }
- }
- }
- return 0;
- }
复制代码 看看输出时Here那一段
|
|