本文介绍了使用select()和read();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿所有.. 我在我的程序中遇到了一些问题,我在c中写了linux。我用使用select()来监控用户是否按下了一个键,然后用read()读取键 。它在我的IBM笔记本电脑上工作得很好,但是一旦我将程序移动到我的戴尔笔记本电脑上,好像它甚至不能识别选择功能。 那是,它根本不使用分配的超时。当我在任何一台计算机上编译时,我都没有收到任何错误 。我在两台笔记本电脑上运行debian /测试和 i使用CVS进行版本控制..我试图在IBM上编译它并且 在dell上运行它..但似乎没什么用。 - 马丁 代码是这样的: #include< sys / types.h> #include< sys / stat.h> #include< asm / io.h> #include< fcntl.h> #include< termios.h> #include< stdio.h> #include< string.h> #include< stdlib.h> #include< unistd.h> #include< limits.h> #include< time.h> #include< termio.h> #include< sys / ioctl.h> struct termio OldTerm; struct termio NewTerm; struct timeval tv1; void SetRaw(int); void TermRestore(int); int main(void ){ int fd = 0,r es = 0,i = 0; char keystroke = 0; fd_set rfds1; SetRaw(fd); FD_ZERO(& rfds1); FD_SET(fileno(stdin),& rfds1); while(1){ tv1.tv_sec = 0; tv1.tv_usec = 1; / *打印菜单* / res = select(fd + 1,& rfds1,NULL,NULL,& tv1); if(res){ read( fd,& keystroke,1); if(keystroke ==''1'){ } if(keystroke ==''2''){ } if(keystroke == ''3''){ } if(keystroke ==''0''){ printf(" s? slutter vi!\ n"); TermRestore(fd); 退出(1); } } 系统(清除); } TermRestore(fd); 返回0; } 无效SetRaw(int fd) { / * *获取终端模式,并将它们保存在OldTerm结构中。 * / (void)ioctl( fd,TCGETA,& OldTerm); / * *将模式设置为我们想要的模式。 * / NewTerm.c_lflag& =〜(ICANON | ECHO | ECHOE | ECHOK | ECHONL); NewTerm.c_oflag | =(OPOST | ONLCR | TAB3); NewTerm.c_oflag& =〜(OCRNL | ONOCR | ONLRET); NewTerm.c_cc [VMIN] = 1; NewTerm.c_cc [VTIME] = 0; (无效)ioctl(fd,TCSETAW,& NewTerm); printf(" fd is%d \ n",fd); } 无效TermRestore(int fd) { / * *恢复保存的模式。 * / (void)ioctl(fd,TCSETAW,& OldTerm); } 解决方案 On Thu ,2004年5月13日11:40:55 +0200,Martin Holm Pedersen写道: 您好这可能不是comp.lang.c的主题,但是对于 comp.unix.programmer。交叉链接和后续设置。 嘿所有.. 我的程序有点问题,我在c中为linux写的。我使用select()来监视用户是否按下了一个键并使用read()读取了该键。它在我的IBM笔记本电脑上运行良好,但是一旦我将程序移动到我的戴尔笔记本电脑上,它似乎甚至都不能识别选择功能。也就是说,它没有使用完全分配超时。当我在任何一台计算机上编译时,我都没有收到任何错误。我在两台笔记本电脑上运行debian /测试和我使用CVS进行版本控制..我试图在IBM上编译它并在戴尔上运行它..但似乎没有任何工作.. - 马丁 代码是这样的: #include< sys / types.h> # include< sys / stat.h> #include< asm / io.h> #include< fcntl.h> #include< termios.h> #include< stdio.h> #include< string.h> #include< stdlib.h> #include< unistd.h> # include< limits.h> #include< time.h> #include< termio.h> #include< sys / ioctl.h> struct termio OldTerm; struct termio NewTerm; struct timeval tv1; void SetRaw(int); void TermRestore(int); int main(void){ int fd = 0,res = 0,i = 0; char keystroke = 0; fd_set rfds1; SetRaw(fd); FD_ZERO(& rfds1); FD_SET(fileno(stdin),& rfds1); while(1){ tv1.tv_sec = 0; tv1.tv_usec = 1; / *打印菜单* / res =选择(fd + 1,& rfds1,NULL,NULL和& tv1); if(res){ read( fd,& keystroke,1); if(keystroke ==''1'){ } if(keystroke = ='''''){ } 如果(击键==''3''){ } if(keystroke ==''0''){ printf(" s? slutter vi!\\\"); TermRestore(fd); exit(1); } } system(clear); TermRestore(fd); 返回0; } void SetRaw(int fd) { / * *获取终端模式,并将它们保存在OldTerm结构中。 * / (void)ioctl(fd,TCGETA,& OldTerm); 查看退货价格 / * *将模式设置为我们想要的模式。 * / NewTerm.c_lflag& =〜(ICANON | ECHO | ECHOE | ECHOK | ECHONL); NewTerm.c_oflag | =(OPOST | ONLCR | TAB3); NewTerm.c_oflag& = 〜(OCRNL | ONOCR | ONLRET); NewTerm.c_cc [VMIN] = 1; NewTerm.c_cc [VTIME] = 0; (void)ioctl(fd,TCSETAW,& NewTerm); 查看返回值 printf(fd is%d \ nn,fd); } void TermRestore(int fd) { / * *恢复保存的模式。 * / (void)ioctl(fd,TCSETAW,& OldTerm ); 查看返回值 } - NPV 大字打印,小字打印 Tom Waits - 向上走吧 Martin Holm Pedersen< mh **** @ kom.auc.dk>写道: struct termio OldTerm; struct termio NewTerm; .... 无效SetRaw(int fd) { / * *获取终端模式,并将它们保存在OldTerm结构中。 * / (void)ioctl(fd,TCGETA,& OldTerm); 你也想要将NewTerm初始化...... :-) / * *将模式设置为我们想要的方式他们。 * / NewTerm.c_lflag& =〜(ICANON | ECHO | ECHOE | ECHOK | ECHONL); NewTerm.c_oflag | =(OPOST | ONLCR | TAB3); NewTerm.c_cc [VMIN] = 1; NewTerm.c_cc [VTIME] = 0; (void) ioctl(fd,TCSETAW,& NewTerm); 如果它在这一点上做任何事情都是意外的! printf(fd is%d \ nn,fd); } void TermRestore(int fd) { / * *恢复已保存的模式。 * / ( void)ioctl(fd,TCSETAW,& OldTerm); } - Floyd L. Davidson< http://web.newsguy.com/floyd_davidson> Ukpeagvik(阿拉斯加州巴罗市) fl *** @ barrow。 com Floyd L. Davidson写道: Martin Holm Pedersen< mh **** @ kom .auc.dk>写道: struct termio OldTerm; struct termio NewTerm; ... void SetRaw(int fd ) { / * *获取终端模式,并将它们保存在OldTerm结构中。 * / (void)ioctl(fd, TCGETA,& OldTerm); 你也想要将NewTerm初始化......: - ) (void)ioctl(fd,TCGETA,& OldTerm); 用于获取现有设置并将其保存在OldTerm中? 我必须初始化NewTerm超过: struct termio NewTerm; ?? / * *将模式设置为我们想要的模式。 * / NewTerm.c_lflag& =〜(ICANON | ECHO | ECHOE | ECHOK | ECHONL); NewTerm.c_oflag | =(OPOST | ONLCR | TAB3); NewTerm.c_oflag& =〜(OCRNL | ONOCR | ONLRET); NewTerm.c_cc [VMIN] = 1; NewTerm.c_cc [VTIME] = 0; (void)ioctl(fd,TCSETAW,& NewTerm); printf(fd is%d \ nn,fd); } void TermRestore(int fd) { / * *恢复保存的模式。 * / (void)ioctl(fd, TCSETAW,& OldTerm); } - Floyd L. Davidson< http://web.newsguy.com/floyd_davidson> fl***@barrow.com Hey All..Im having a bit of a problem with my program that i wrote for linux in c. Iuse select() to monitor if the user has pressed a key and reads the keywith read(). It works fine om my IBM laptop but once i move the program tomy dell laptop it seems like it doesn''t even recognize the select-function.That is, it doesn''t use the timeout assigned at all. I don''t get any errorswhen i compile on either computer. I run debian/testing on both laptops andi use CVS for version-control.. I have tried to compile it on the IBM andrun it on the dell.. But nothing seems to work.. - Martin The code is something along the lines of: #include <sys/types.h>#include <sys/stat.h>#include <asm/io.h>#include <fcntl.h>#include <termios.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <limits.h>#include <time.h>#include <termio.h>#include <sys/ioctl.h> struct termio OldTerm;struct termio NewTerm;struct timeval tv1; void SetRaw(int);void TermRestore(int); int main(void) {int fd=0, res=0, i=0;char keystroke = 0;fd_set rfds1; SetRaw(fd);FD_ZERO(&rfds1);FD_SET(fileno(stdin), &rfds1);while(1){ tv1.tv_sec=0;tv1.tv_usec=1; /* PRINT THE MENU */res=select(fd+1, &rfds1,NULL,NULL,&tv1);if(res){read(fd, &keystroke, 1); if(keystroke==''1''){ } if(keystroke==''2''){ } if(keystroke==''3''){ } if(keystroke==''0''){printf("s? slutter vi!\n");TermRestore(fd);exit(1);}}system("clear");} TermRestore(fd);return 0;} void SetRaw(int fd){ /** Get terminal modes, and saves them in the OldTerm struct.*/(void)ioctl(fd, TCGETA, &OldTerm);/** Set the modes to the way we want them.*/NewTerm.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);NewTerm.c_oflag |= (OPOST|ONLCR|TAB3);NewTerm.c_oflag &= ~(OCRNL|ONOCR|ONLRET);NewTerm.c_cc[VMIN] = 1;NewTerm.c_cc[VTIME] = 0;(void)ioctl(fd, TCSETAW, &NewTerm);printf("fd is %d\n", fd);} void TermRestore(int fd){/** Restore saved modes.*/(void)ioctl(fd, TCSETAW, &OldTerm);} 解决方案 On Thu, 13 May 2004 11:40:55 +0200, Martin Holm Pedersen wrote: Hi This is probably not a topic for comp.lang.c but forcomp.unix.programmer. Crossposted and followups set. Hey All.. Im having a bit of a problem with my program that i wrote for linux in c. I use select() to monitor if the user has pressed a key and reads the key with read(). It works fine om my IBM laptop but once i move the program to my dell laptop it seems like it doesn''t even recognize the select-function. That is, it doesn''t use the timeout assigned at all. I don''t get any errors when i compile on either computer. I run debian/testing on both laptops and i use CVS for version-control.. I have tried to compile it on the IBM and run it on the dell.. But nothing seems to work.. - Martin The code is something along the lines of: #include <sys/types.h> #include <sys/stat.h> #include <asm/io.h> #include <fcntl.h> #include <termios.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <limits.h> #include <time.h> #include <termio.h> #include <sys/ioctl.h> struct termio OldTerm; struct termio NewTerm; struct timeval tv1; void SetRaw(int); void TermRestore(int); int main(void) { int fd=0, res=0, i=0; char keystroke = 0; fd_set rfds1; SetRaw(fd); FD_ZERO(&rfds1); FD_SET(fileno(stdin), &rfds1); while(1){ tv1.tv_sec=0; tv1.tv_usec=1; /* PRINT THE MENU */ res=select(fd+1, &rfds1,NULL,NULL,&tv1); if(res){ read(fd, &keystroke, 1); if(keystroke==''1''){ } if(keystroke==''2''){ } if(keystroke==''3''){ } if(keystroke==''0''){ printf("s? slutter vi!\n"); TermRestore(fd); exit(1); } } system("clear"); } TermRestore(fd); return 0; } void SetRaw(int fd) { /* * Get terminal modes, and saves them in the OldTerm struct. */ (void)ioctl(fd, TCGETA, &OldTerm);Check return value /* * Set the modes to the way we want them. */ NewTerm.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); NewTerm.c_oflag |= (OPOST|ONLCR|TAB3); NewTerm.c_oflag &= ~(OCRNL|ONOCR|ONLRET); NewTerm.c_cc[VMIN] = 1; NewTerm.c_cc[VTIME] = 0; (void)ioctl(fd, TCSETAW, &NewTerm);Check return value printf("fd is %d\n", fd); } void TermRestore(int fd) { /* * Restore saved modes. */ (void)ioctl(fd, TCSETAW, &OldTerm);Check return value } --NPV "the large print giveth, and the small print taketh away"Tom Waits - Step right up Martin Holm Pedersen <mh****@kom.auc.dk> wrote:struct termio OldTerm;struct termio NewTerm;....void SetRaw(int fd){ /* * Get terminal modes, and saves them in the OldTerm struct. */ (void)ioctl(fd, TCGETA, &OldTerm);You''ll want to initalize NewTerm also... :-) /* * Set the modes to the way we want them. */ NewTerm.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); NewTerm.c_oflag |= (OPOST|ONLCR|TAB3); NewTerm.c_oflag &= ~(OCRNL|ONOCR|ONLRET); NewTerm.c_cc[VMIN] = 1; NewTerm.c_cc[VTIME] = 0; (void)ioctl(fd, TCSETAW, &NewTerm);It''s an accident if it does anything at this point! printf("fd is %d\n", fd);}void TermRestore(int fd){ /* * Restore saved modes. */ (void)ioctl(fd, TCSETAW, &OldTerm);} --Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>Ukpeagvik (Barrow, Alaska) fl***@barrow.comFloyd L. Davidson wrote: Martin Holm Pedersen <mh****@kom.auc.dk> wrote:struct termio OldTerm;struct termio NewTerm; ...void SetRaw(int fd){ /* * Get terminal modes, and saves them in the OldTerm struct. */ (void)ioctl(fd, TCGETA, &OldTerm); You''ll want to initalize NewTerm also... :-) (void)ioctl(fd, TCGETA, &OldTerm);is for getting the existing settings and saving them in OldTerm? Do i have to initialize NewTerm more than:struct termio NewTerm; ?? /* * Set the modes to the way we want them. */ NewTerm.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); NewTerm.c_oflag |= (OPOST|ONLCR|TAB3); NewTerm.c_oflag &= ~(OCRNL|ONOCR|ONLRET); NewTerm.c_cc[VMIN] = 1; NewTerm.c_cc[VTIME] = 0; (void)ioctl(fd, TCSETAW, &NewTerm); It''s an accident if it does anything at this point! printf("fd is %d\n", fd);}void TermRestore(int fd){ /* * Restore saved modes. */ (void)ioctl(fd, TCSETAW, &OldTerm);} -- Floyd L. Davidson <http://web.newsguy.com/floyd_davidson> Ukpeagvik (Barrow, Alaska) fl***@barrow.com 这篇关于使用select()和read();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 19:58