home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / programm / 5744 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  2.1 KB

  1. Xref: sparky comp.unix.programmer:5744 comp.unix.questions:14845
  2. Path: sparky!uunet!psinntp!ncrlnk!ncrsaud!kbahey
  3. From: kbahey@ncrsaud.SaudiArabia.NCR.COM (Khalid Bahey-elDin)
  4. Newsgroups: comp.unix.programmer,comp.unix.questions
  5. Subject: Programs that fork() don't redirect output in desired way?
  6. Message-ID: <941@ncrsaud.SaudiArabia.NCR.COM>
  7. Date: 20 Dec 92 08:16:35 GMT
  8. Reply-To: kbahey@ncrsaud.SaudiArabia.NCR.COM (Khalid Bahey-elDin)
  9. Distribution: world
  10. Organization: NCR Corp., Saudi Arabia
  11. Lines: 36
  12.  
  13. The following is the same on AT&T UNIX V.3 and V.4.
  14.  
  15. The scenario is that a program (a background communication interface) is
  16. invoked, and it fork()s 2 children. The parent monitors both children, and
  17. one child deals with inbound traffic, the other with outbound traffic.
  18.  
  19. In this program, we have a lot of printf()s used for debugging and tracing, and
  20. are only active when we compile with a -DDEBUG compiler flag.
  21.  
  22. We let the program run in the background, and redirect stdout, and stderr to a
  23. holding debug file, via the shell command:
  24.  
  25. prog > debug.out 2>&1 
  26.  
  27. Because "prog" breaks itself into three running processes, we have three
  28. different processes redirecting their output to the debug file.
  29.  
  30. The problem is that the file does not contain things in chronological order.
  31. What happens is that it will contain output from the parent, then output from
  32. child1, then output from child2, all interspersed.
  33.  
  34. I assume this is because of system buffering for printf()s.
  35.  
  36. Of course I can rewrite the printf()s so as they would write to a special log
  37. file, but this would be a considerable effort, plus it will deprive us of the
  38. flexibility of displaying output directly to the terminal when we want to.
  39.  
  40. What is an easy way to get the output of all three processes correctly in
  41. correct chronological order?
  42.  
  43. Thanks in advance.
  44. -- 
  45. Khalid M. Bahey-elDin       | Voice: +966-2-651-2727 | Fax: +966-2-651-8804
  46. Systems Services Division   | Email: Khalid.Bahey-elDin@SaudiArabia.NCR.COM
  47. NCR Corp., Saudi Arabia     | also:  kbahey@ncrsaud.saudiarabia.ncr.com
  48. P.O.Box 13964, Jeddah 21414 | UUCP:  ..!uunet!ncrlnk!ncrsaud!kbahey
  49.