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編程