Programs that fork() don't redirect output in desired way?

Programs that fork() don't redirect output in desired way?

Post by Khalid Bahey-elD » Mon, 21 Dec 1992 17:16:35



The following is the same on AT&T UNIX V.3 and V.4.

The scenario is that a program (a background communication interface) is
invoked, and it fork()s 2 children. The parent monitors both children, and
one child deals with inbound traffic, the other with outbound traffic.

In this program, we have a lot of printf()s used for debugging and tracing, and
are only active when we compile with a -DDEBUG compiler flag.

We let the program run in the background, and redirect stdout, and stderr to a
holding debug file, via the shell command:

prog > debug.out 2>&1

Because "prog" breaks itself into three running processes, we have three
different processes redirecting their output to the debug file.

The problem is that the file does not contain things in chronological order.
What happens is that it will contain output from the parent, then output from
child1, then output from child2, all interspersed.

I assume this is because of system buffering for printf()s.

Of course I can rewrite the printf()s so as they would write to a special log
file, but this would be a considerable effort, plus it will deprive us of the
flexibility of displaying output directly to the terminal when we want to.

What is an easy way to get the output of all three processes correctly in
correct chronological order?

Thanks in advance.
--
Khalid M. Bahey-elDin       | Voice: +966-2-651-2727 | Fax: +966-2-651-8804


P.O.Box 13964, Jeddah 21414 | UUCP:  ..!uunet!ncrlnk!ncrsaud!kbahey

 
 
 

Programs that fork() don't redirect output in desired way?

Post by Leslie Mikese » Tue, 22 Dec 1992 14:26:32



Quote:>Because "prog" breaks itself into three running processes, we have three
>different processes redirecting their output to the debug file.
>The problem is that the file does not contain things in chronological order.
>What happens is that it will contain output from the parent, then output from
>child1, then output from child2, all interspersed.
>I assume this is because of system buffering for printf()s.

The quick fix is to add an fflush() after each printf(), or you could
use setvbuf() to force unbuffered or line buffered output.  Fflush()
will probably work better to keep related output together.

Les Mikesell


 
 
 

Programs that fork() don't redirect output in desired way?

Post by Dwight Erne » Wed, 23 Dec 1992 01:37:18



>The following is the same on AT&T UNIX V.3 and V.4.
>The scenario is that a program (a background communication interface) is
>invoked, and it fork()s 2 children. The parent monitors both children, and
>one child deals with inbound traffic, the other with outbound traffic.
>In this program, we have a lot of printf()s used for debugging and tracing, and
>are only active when we compile with a -DDEBUG compiler flag.
>We let the program run in the background, and redirect stdout, and stderr to a
>holding debug file, via the shell command:
>prog > debug.out 2>&1
>Because "prog" breaks itself into three running processes, we have three
>different processes redirecting their output to the debug file.
>The problem is that the file does not contain things in chronological order.
>What happens is that it will contain output from the parent, then output from
>child1, then output from child2, all interspersed.
>I assume this is because of system buffering for printf()s.
>Of course I can rewrite the printf()s so as they would write to a special log
>file, but this would be a considerable effort, plus it will deprive us of the
>flexibility of displaying output directly to the terminal when we want to.
>What is an easy way to get the output of all three processes correctly in
>correct chronological order?

There's no clean way to completely avoid race conditions like you
describe without a coding effort, which you say you're trying to
avoid.

A better algorithm, though, if you wanted to recode, would be to have
the parent open the debug file instead of using redirection, and then
use some sort of locking mechanism to allow one instance of the program
to write to the output file at a time.

A cheap and easy way to at least mitigate the effect would be to call
fflush() after each printf.  RTFM.  It won't entirely do away with the
problem, but it should help.
--
--Dwight A. Ernest   R&D Manager (USA)     I speak only for myself.
  Hyphen, Inc./181 Ballardvale St./Wilmington, MA 01887/+1 508 988 0880 x125

 
 
 

Programs that fork() don't redirect output in desired way?

Post by Udo Mu » Tue, 22 Dec 1992 20:44:58


[...]
: The problem is that the file does not contain things in chronological order.
: What happens is that it will contain output from the parent, then output from
: child1, then output from child2, all interspersed.
:
: I assume this is because of system buffering for printf()s.
:
: Of course I can rewrite the printf()s so as they would write to a special log
: file, but this would be a considerable effort, plus it will deprive us of the
: flexibility of displaying output directly to the terminal when we want to.
:
: What is an easy way to get the output of all three processes correctly in
: correct chronological order?

Add a fflush(stdout) after each printf().
--
Udo Munk, Oberstr. 21, 4040 Neuss 1, Germany

uucp: +49 2131 278869 V22/32/42bis 8N1 login: uucp pwd: public MYNAME=bbsuser
request file /usr/spool/uucppublic/info for instructions

 
 
 

1. redirecting output for fork & curses Q's

HELLO ,thank you for reading ,and i hope you might help ,so
 with the help of fork()&system("cmdtool") i'd like to redirect,if 'redirect'
is the correct expression, the output of another child to that cmdtool,
even better i'd like to some way fork to another window, that is
have two outputs for the same program ,or more than 2 o/p,
i mean , is it possible via information ,e.g. pid, about a process to send
to it and/or recieve from it ,if so i'd really appriciete any help .
 Also i would like to get started with curses ,which i think is for at least
controlling the position of the cursor and the terminal(terminal ??),
and for more than just the curser ,so i wonder if any body could provide a
kick-start code ,because i think i'm having problems with telling the
compiler where to find libs,headers and makefiles,i found out i really dont
know anything about makefile, so i wonder if anybody could straighten things
out for me ,
 lots and lots of thank u for u.

2. why is child killed with signal 13

3. redirect a program's syslog output

4. HELP Shadow 3.3.2 installation

5. Redirecting output of fork

6. smail on Linux & Eudora

7. Redirecting 'at' job output

8. X-Server

9. How to redirect 'killed' output

10. redirect ``time'' output

11. 'at' command and redirecting the cron output

12. How to 'flush' output of 'C' cgi-program

13. fork() - program output?