main() { int fd, num; fd_set rfds; struct timeval tv;
fd = open("/dev/globalvar", O_RDWR, S_IRUSR | S_IWUSR); if (fd != - 1) { while (1) { //查看globalvar是否有输入 FD_ZERO(&rfds); FD_SET(fd, &rfds); //设置超时时间为5s tv.tv_sec = 5; tv.tv_usec = 0; select(fd + 1, &rfds, NULL, NULL, &tv);
//数据是否可获得? if (FD_ISSET(fd, &rfds)) { read(fd, &num, sizeof(int)); printf("The globalvar is %d\n", num);
//输入为0,退出 if (num == 0) { close(fd); break; } } else printf("No data within 5 seconds.\n"); } } else { printf("device open failure\n"); } }
|