home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.programmer:5744 comp.unix.questions:14845
- Path: sparky!uunet!psinntp!ncrlnk!ncrsaud!kbahey
- From: kbahey@ncrsaud.SaudiArabia.NCR.COM (Khalid Bahey-elDin)
- Newsgroups: comp.unix.programmer,comp.unix.questions
- Subject: Programs that fork() don't redirect output in desired way?
- Message-ID: <941@ncrsaud.SaudiArabia.NCR.COM>
- Date: 20 Dec 92 08:16:35 GMT
- Reply-To: kbahey@ncrsaud.SaudiArabia.NCR.COM (Khalid Bahey-elDin)
- Distribution: world
- Organization: NCR Corp., Saudi Arabia
- Lines: 36
-
- 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
- Systems Services Division | Email: Khalid.Bahey-elDin@SaudiArabia.NCR.COM
- NCR Corp., Saudi Arabia | also: kbahey@ncrsaud.saudiarabia.ncr.com
- P.O.Box 13964, Jeddah 21414 | UUCP: ..!uunet!ncrlnk!ncrsaud!kbahey
-