c - Redirecting Output -


When I do not understand anything, then I was playing with redirection.

I have a basic multi-client server program that connects a user or writes a message for standard output when disconnecting.

If I run 2 clients in other terminals and shut down, the output will be something like this:

  Waiting for connection .... A user   

If I redirect the output via a file

 , a user connected to a user connected to a user disconnected   

./ Server & gt; logfile.txt

When I open the logfile, I see something like this:

  Waiting for connection. ... A user connected to a user is waiting for connection disconnected .... Disconnect connected to a user   

My question is: How do I get the same output Can i do

My suspicion is that there is such a code in your server program:

  printf "is waiting for connection ... \ n"); While ((handle = new_connection ())! = 0) {if ((pid = fork ()) <0 fprintf (stderr, "..."); else if (pid == 0) {printf ("Connect a user \ n"); manage_connection (handle); Printf ("One user disconnected \ n"); Exit (0); }}   

When you go with the output to the terminal, the output buffer buffer occurs, so the waiting message is displayed immediately.

When the output is out in a file, the output is completely buffer; The buffer fills, or the obvious fflush () occurs, or a fclose () , or the process normally exits, the file is not sent until This means that there is a waiting message in its buffer in the hair process, and a copy of the message will be printed for each child's process.

The only way to fix this is to modify the server program. You can use fflush () after printing the 'Wait' message (and before the loop), or you can explicitly set output buffering on _IOLBF Are there.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -