home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11815 < prev    next >
Encoding:
Text File  |  1992-07-30  |  2.4 KB  |  70 lines

  1. Xref: sparky comp.lang.c++:11815 comp.unix.programmer:4025
  2. Newsgroups: comp.lang.c++,comp.unix.programmer
  3. Path: sparky!uunet!sun-barr!ames!elroy.jpl.nasa.gov!usc!rpi!batcomputer!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  4. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  5. Subject: Re: How to avoid calling destructor twice after fork()?
  6. Message-ID: <9221212.11172@mulga.cs.mu.OZ.AU>
  7. Sender: news@cs.mu.OZ.AU
  8. Organization: Computer Science, University of Melbourne, Australia
  9. References: <1992Jul29.180655.4716@cis.ohio-state.edu>
  10. Date: Thu, 30 Jul 1992 02:44:35 GMT
  11. Lines: 57
  12.  
  13. gyu@cis.ohio-state.edu (george yu) writes:
  14.  
  15. >I am using fork() system call in my C++ program. The problem is that each
  16. >calling of fork() creates a copy of calling process. The problem is any
  17. >instances created and not destroyed before fork() calling will be 
  18. >destroyed twice. 
  19.  
  20. [I'm cross-posting to comp.unix.programmer; you might get a more informative
  21. answer there]
  22.  
  23. When you call fork(), it creates a copy of the data space of the program
  24. for the child. Thus parent and child each get their own copy of the stack,
  25. heap, and static data areas.
  26.  
  27. This means that if the destructor is simply doing memory management, for
  28. example, then the destructor *should* be called twice, since it needs to
  29. call operator delete for both copies of the heap.
  30.  
  31. If the destructor does some I/O (eg. closing a file), then the situation
  32. is more complicated. The simplest solution might be to bracket use of local
  33. variables using blocks, if this is possible:
  34.  
  35.     {
  36.       local vars...
  37.       some code...
  38.     }
  39.     pid=fork();
  40.     {
  41.       more local vars...S
  42.       more code...
  43.     }
  44.  
  45. P.S.
  46. This is related to the following problem with C programs that use fork:
  47.  
  48.     #include <stdio.h>
  49.     extern int fork(void);
  50.  
  51.     int main(void) {
  52.         printf("Hello");
  53.         fork();
  54.         printf("\n");
  55.         return 0;
  56.     }
  57.  
  58. You might expect this to print one "Hello" and two "\n"s.
  59. However because stdout is buffered, the "Hello" is not printed immediately.
  60. Thus the program forks, and then each copy contains "Hello" in the buffer
  61. for stdio. Now when each copy prints "\n", the buffer is flushed, so you
  62. get TWO "Hello"s as well as two "\n"s.
  63. (The solution here is to call fflush(stdout) before calling fork().)
  64.  
  65. -- 
  66. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  67. This .signature VIRUS is a self-referential statement that is true - but 
  68. you will only be able to consistently believe it if you copy it to your own
  69. .signature file!
  70.