Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I'm a writing a small webserver for personal use in c (not c++). I want to allow user input in the console like "shutdown server" or "restart server".

In order to allow this kind of input the server is running in a seperate thread (pthreads), so the console isn't blocked.

I also want this thread to print output in the console like "a new client connected" or "client requestet 'home.html'".

The problem is: If I'm typing something like "shutdown server" and at the same time the thread prints something like "a new client connected" everthing mixes up and I get something like "shuta new client connectedown server"

Is there an elegant way to print the output of the thread and at the same time allow the user to enter commands without both interfering?

Or is this a stupid idea to begin with? If yes: Is there a standard way to handle things like that (i.e. to control server).

share|improve this question
Have you looked into a curses interface? It'd give you the ability to have separate input and output regions of a terminal. – Sean McSomething Mar 5 '13 at 0:34
Thank you! I've considered that but then it seemed too much work to do something that simple. – mjb Mar 6 '13 at 17:48

2 Answers 2

up vote 1 down vote accepted

When you start your server from a command line, that console should be used as the running log (you should also have your logs written to a file for some permanence. There are two options for handling your scenario.

  1. Have your server temporarily stop outputting to the console when a user starts typing i.e. turn on an "interactive mode.
  2. When your server starts, have it open an "administrative port" that listens to commands sent to it from a different console. When you want to sent the shutdown command you'd use a separate program the server admin client to allow you to interface with the server via the command line (or a UI).
share|improve this answer
Thank you! Any idea on how to do the first one? Maybe a link to some example? – mjb Mar 4 '13 at 17:57
My c is pretty rusty I can give pseudo code though (also which platform are you targeting? – Michael Brown Mar 5 '13 at 14:17
Thank you, but I already managed to do it. Perfect idea! – mjb Mar 6 '13 at 17:47

There is no good way to do what you want to do.

As you have observed, if you allow the logger to continue its output while the command interpreter is echoing the user's typing, you get a mishmashed mess.

The flip side is this: if you block the logger when the user starts typing, you risk blocking an IMPORTANT message, one that requires IMMEDIATE action.

The best answer is to have two (2) "consoles", displayed simultaneously, so you don't have to block your logger.

If you can't do that, because (for example), you're on a raw serial port, then option two is to add priorities to your output messages. When the user starts typing, unimportant messages get held until he finishes typing. Important messages blast through, and then the command interpreter retypes his line so far. This will require some amount of buffer space to stack his messages while he's scratching his head.

PERSONALLY, if I was going to do something like this, I'd put a timeout on the held messages: if he doesn't finish typing his command in some amount of time, the previously-held unimportant messages get dumped through, and then the command line so far gets retyped.

Usually, it isn't worth the trouble to avoid the message mixing. If you have a verbose operator and a verbose message logger, you've got bigger problems than just mixed output streams.

share|improve this answer
If you can open up multiple shells, you can easily unmix them by redirecting stdout/stderr to a file in one shell and doing tail -f on that file in another. – Steven Burnap Mar 4 '13 at 20:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.