#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
/*返回:就绪的文件描述符数量,在超时的情况下返回 0,在产生错误时返回 -1 */
void FD_CLR(int fd, fd_set *set);
int FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);
/*返回:如果在 fdset 中设置 fd,则返回非零值,否则返回 0*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
int main(void)
{
fd_set read_set;
fd_set write_set;
int i;
FD_ZERO (&read_set);
FD_ZERO (&write_set);
FD_SET (0, &read_set);
FD_SET (1, &write_set);
FD_SET (2, &read_set);
FD_SET (3, &write_set);
printf ("read_set:\n");
for (i = 0; i < 4; i++) {
printf (" bit %d is %s\n", i, (FD_ISSET (i, &read_set) ? "set" : "clear"));
}
printf ("write_set:\n");
for (i = 0; i < 4; i++) {
printf (" bit %d is %s\n", i, (FD_ISSET (i, &write_set) ? "set" : "clear"));
}
return (0);
}
beyes@linux-beyes:~/C/base> ./select.exe
read_set:
bit 0 is set
bit 1 is clear
bit 2 is set
bit 3 is clear
write_set:
bit 0 is clear
bit 1 is set
bit 2 is clear
bit 3 is set
struct timeval {
time_t tv_sec; /* 时间, 以秒为单位 */
suseconds_t tv_usec; /* 时间,以微妙为单位 */
};
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
char buffer[128];
int result, nread;
fd_set inputs, testfds;
struct timeval timeout;
FD_ZERO (&inputs); /*每一位都初始化为 0*/
FD_SET (0, &inputs); /* 监视0描述符 */
while (1) {
testfds = inputs;
timeout.tv_sec = 2;
timeout.tv_usec = 500000; /* 2.5 秒超时等待 */
result = select(FD_SETSIZE, &testfds, (fd_set *)NULL, (fd_set *)NULL,
&timeout);
switch(result) {
case 0:
printf("timeout\\n");
break;
case -1:
perror("select");
exit(1);
default:
if (FD_ISSET(0, &testfds)) { /* 测试描述符是否就绪(标准输入是否有输入并可读) */
ioctl(0, FIONREAD, &nread);
if (nread == 0) {
printf("keyboard done\\n"); /* 标准输入无数据(按下 ctrl + d 组合键) */
exit(0);
}
nread = read(0, buffer, nread); /* 处理读取内容 */
buffer[nread] = 0;
printf("read %d from keyboard: %s", nread, buffer);
}
break;
}
}
beyes@linux-beyes:~/C/base> ./select2.exe
oo
read 3 from keyboard: oo
timeout
kk
read 3 from keyboard: kk
dkdkdk
read 7 from keyboard: dkdkdk
lk
read 3 from keyboard: lk
keyboard done
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
void display_time(const char *string)
{
int seconds;
seconds = time((time_t *)NULL);
printf("%s, %d\\n", string, seconds);
}
int main()
{
fd_set readfds;
struct timeval timeout;
int ret;
/*监视文件描述符0(标准输入,键盘输入)是否有数据输入*/
FD_ZERO(&readfds);
FD_SET(0, &readfds);
/*设置超时时间10秒*/
timeout.tv_sec = 10;
timeout.tv_usec = 0;
while (1) {
display_time("before select");
ret = select(1, &readfds, NULL, NULL, &timeout);
display_time("after select");
switch (ret) {
case 0:
printf("No data in ten seconds.\\n");
exit(0);
break;
case -1:
perror("select");
exit(1);
break;
default:
getchar();
printf("Data is availabel now. \\n");
}
}
return (0);
}
beyes@linux-beyes:~/C/base> ./test_select.exe
before select, 1251106276
ddd
after select, 1251106283
Data is availabel now.
before select, 1251106283
after select, 1251106286
No data in ten seconds.
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |