linux中read阻塞原理,阻塞Linux中的多個線程上的read()和write()

0

You could use several threads, as answered by Amardeep, then you need synchronization techniques (perhaps Pthread condition variables and mutexes). Then read a tutorial about pthreads.

您可以使用幾個線程,然后需要同步技術(可能是Pthread條件變量和互斥對象)。然后閱讀有關pthreads的教程。

Notice that for real keyboard input in a terminal you may want to use libraries like ncurses or readline (they permit more editing than the classical getline(3) or fgets approach; read the tty demystified page).

注意,對於終端中的真正的鍵盤輸入,您可能希望使用ncurses或readline之類的庫(它們允許比傳統的getline(3)或fgets方法進行更多的編輯;閱讀“tty demystified”頁面。

You could also have an event loop around a multiplexing syscall like poll(2). See this example. Then you could (or not) have a single thread.

您還可以對多路復用系統(如poll(2))進行事件循環。看到這個例子。然后你可以(或者不可以)有一個線程。

You could also want a graphical user interface (i.e. windows, buttons, etc...), then you want to use a library like Qt or Gtk or libsdl etc which provides a powerful event loop. (Don't dare doing low-level X11 programming directly, life is too short for that).

您還可以需要一個圖形用戶界面(例如windows、按鈕等),然后需要使用Qt、Gtk或libsdl等庫,這些庫提供了一個強大的事件循環。(不要直接做底層的X11編程,生命太短暫了)。

You may want to do non-blocking IO (which is handy in event loops), set up with fcntl(2) F_SETFL the O_NONBLOCK flag.

您可能希望執行非阻塞IO(這在事件循環中很方便),使用fcntl(2) F_SETFL設置O_NONBLOCK標志。

You could do Posix asynchronous IO, see aio(7).

您可以執行Posix異步IO,參見aio(7)。

You may want to be notified that IO is possible thru the SIGIO signal(7) (but use signals with care, often a signal handler just sets up a volatile sig_atomic_t variable tested elsewhere!), use fcntl with F_SETOWN (on the result of getpid(2)) and O_ASYNC

您可能希望通過SIGIO信號(7)通知IO是可能的(但是要小心使用信號,通常一個信號處理程序只設置一個在其他地方測試過的volatile sig_atomic_t變量!),使用F_SETOWN的fcntl(基於getpid(2)和O_ASYNC的結果)

You could also make your application a specialized Web server, by using an HTTP server library like libonion

您還可以通過使用像libonion這樣的HTTP服務器庫使您的應用程序成為一個專門的Web服務器。

您應該閱讀高級Linux編程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值