- 论坛徽章:
- 0
|
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <unistd.h>
- int
- main(void)
- {
- fd_set rfds;
- struct timeval tv;
- int retval;
- /* Watch stdin (fd 0) to see when it has input. */
- FD_ZERO(&rfds);
- FD_SET(0, &rfds);
- /* Wait up to five seconds. */
- tv.tv_sec = 5;
- tv.tv_usec = 0;
- retval = select(1, &rfds, NULL, NULL, &tv);
- /* Don't rely on the value of tv now! */
- if (retval == -1)
- perror("select()");
- else if (retval)
- printf("Data is available now.\n");
- /* FD_ISSET(0, &rfds) will be true. */
- else
- printf("No data within five seconds.\n");
- exit(EXIT_SUCCESS);
- }
复制代码 疑惑:
FD_SET(0, &rfds); //这里已经在集合rfds中设置文件描述符'0',
然后调用select之后,通过FD_ISSET(0, &rfds)来检测文件描述符'0'是不是集合rfds中的一个元素,来判断是否发生了I/O操作。
但是‘0’不是已经设置过了吗?不是本来就已经在集合rfds中了吗?
对select的原理不是很清楚。。。 |
|