你如何在 Linux 上用 C 进行非阻塞控制台 I/O? [英] How do you do non-blocking console I/O on Linux in C?

查看:30
本文介绍了你如何在 Linux 上用 C 进行非阻塞控制台 I/O?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何在 Linux/OS X 上用 C 语言执行非阻塞控制台 IO?

How do you do nonblocking console IO on Linux/OS X in C?

推荐答案

你没有,真的.TTY(控制台)是一个非常有限的设备,您几乎不进行非阻塞 I/O.当您看到一些看起来像非阻塞 I/O 的东西时,例如在 Curses/ncurses 应用程序中,您所做的称为<​​em>原始 I/O.在原始 I/O 中,没有字符解释,没有擦除处理等.相反,您需要编写自己的代码来检查数据,同时做其他事情.

You don't, really. The TTY (console) is a pretty limited device, and you pretty much don't do non-blocking I/O. What you do when you see something that looks like non-blocking I/O, say in a curses/ncurses application, is called raw I/O. In raw I/O, there's no interpretation of the characters, no erase processing etc. Instead, you need to write your own code that checks for data while doing other things.

在现代 C 程序中,您可以通过将控制台 I/O 放入一个线程或轻量级进程来以另一种方式简化这一过程.然后 I/O 可以以通常的阻塞方式继续进行,但数据可以插入到队列中以在另一个线程上进行处理.

In modern C programs, you can simplify this another way, by putting the console I/O into a thread or lightweight process. Then the I/O can go on in the usual blocking fashion, but the data can be inserted into a queue to be processed on another thread.

这是一个 curses 教程,其中涵盖了更多内容.

Here's a curses tutorial that covers it more.

这篇关于你如何在 Linux 上用 C 进行非阻塞控制台 I/O?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆