Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我只想在 Ubuntu 上用 C 实现这个算法:
等待一段时间以接收来自键盘的输入,因此,通过获得可能的输入或随着时间的推移,程序应该继续。
我没有任何线索这样做!提前谢谢你。
有关alarm()和signal()函数的信息,请参见手册。您可以轻松地使任何代码超时,而无需使用任何线程或进程。
常见的方法是使用select()or poll():
select()
poll()
struct pollfd fd = {STDIN_FILENO, POLLIN}; switch(poll(&fd, 1, 1)){ case -1: die("poll failed"); break; case 0: //timed out... break; default: //read from stdin }