3

I have a Linux process that needs to read from, say, the keyboard and mouse. It needs to do this asynchronously while also doing some main process.

The implementation I've come up with is to have the main process in one thread, and have two separate threads always read()ing from the keyboard and mouse. If Linux read() is blocking, will the keyboard and mouse threads become blocked while the main thread continues executing (what I want), or will the entire process become blocked?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
tomKPZ
  • 827
  • 1
  • 10
  • 17
  • 1
    There is no reason the other threads should block. Alternatively you might be able get away with just using `select` with timeout, occassionally poll your input while otherwise process whatever main does. – Duck May 04 '14 at 01:18
  • Threading is one way; [`select()`](http://linux.die.net/man/2/select) is another. – j_random_hacker May 04 '14 at 01:19
  • You should prefer [poll(2)](http://man7.org/linux/man-pages/man2/poll.2.html) to `select` (google for `C10K problem`), and you want to implement an [event loop](http://en.wikipedia.org/wiki/Event_loop) – Basile Starynkevitch May 04 '14 at 04:58
  • It is curious that you are `read(2)`-ing the mouse and the keyboard.... Generally, the X11 server is reading them. – Basile Starynkevitch May 04 '14 at 17:59
  • Prefer epoll() as it is meant to replace select and poll system calls. On the other part regarding blocking, a block on one thread will not block other threads of the same process. – Karthik Balaguru Oct 10 '14 at 05:33

2 Answers2

3

All threads in a process will not block as a result of one or more threads in the process becoming blocked. Each thread will be scheduled if it is able to run, within the rules of the scheduler in effect.

So your design is quite valid. Just use proper synchronization techniques if the data you read has to be consumed by another thread. Semaphores and message queues are handy for that purpose.

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
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.

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).

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.

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).

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.

You could do Posix asynchronous IO, see 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

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

You should read Advanced Linux Programming

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547