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