66 void activate_nonblock(int fd)//设置非堵塞io,fd是io文件描述符
67 {
68 int ret;
69 int flags = fcntl(fd, F_GETFL);//F_GETFL获取标志位flag
70 if (flags == -1)
71 ERR_EXIT("fcntl");
72
73 flags |= O_NONBLOCK; //为flag添加非堵塞属性
74 ret = fcntl(fd, F_SETFL, flags); //设置fd的标志位为新的flag
75 if (ret == -1) //返回值为 -1为异常
76 ERR_EXIT("fcntl");
77 }
83 void deactivate_nonblock(int fd) //设置io为堵塞
84 {
85 int ret;
86 int flags = fcntl(fd, F_GETFL);
87 if (flags == -1)
88 ERR_EXIT("fcntl");
89
90 flags &= ~O_NONBLOCK; //减去flag的非阻塞属性
91 ret = fcntl(fd, F_SETFL, flags);
92 if (ret == -1)
93 ERR_EXIT("fcntl");
94 }