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

  1. Path: sparky!uunet!wupost!waikato.ac.nz!comp.vuw.ac.nz!actrix!Bruce.Hoult
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: How to avoid calling destructor twice after fork()?
  4. Message-ID: <1992Jul30.120150.1507@actrix.gen.nz>
  5. From: Bruce.Hoult@bbs.actrix.gen.nz
  6. Date: Thu, 30 Jul 1992 12:01:50 GMT
  7. Sender: Bruce.Hoult@actrix.gen.nz (Bruce Hoult)
  8. References: <1992Jul29.180655.4716@cis.ohio-state.edu>
  9. Organization: Actrix Information Exchange
  10. Lines: 115
  11.  
  12. In article <1992Jul29.180655.4716@cis.ohio-state.edu> gyu@cis.ohio-state.edu (george yu) writes:
  13. > I am using fork() system call in my C++ program. The problem is that each
  14. > calling of fork() creates a copy of calling process. The problem is any
  15. > instances created and not destroyed before fork() calling will be 
  16. > destroyed twice. The following figure shows the problem.
  17. > class MY_CLASS;
  18. > main()
  19. > {    
  20. >    MY_CLASS my_class;
  21. >    if(fork()==0)
  22. >     {
  23. >        // process 1
  24. >     }
  25. >    else
  26. >     {
  27. >        // process 2
  28. >     }
  29. >    
  30. >    // everything beyond this point will be executed twice, including
  31. >    // the destructor of my_class.
  32. > }
  33. > Any suggestions?
  34.  
  35.  
  36. What's the problem?  After the fork you've got two seperate objects in
  37. different processes and running the destructor on both of them is the
  38. correct thing to do -- the only possible problem is that the fork()
  39. call effectively does a default operator= to create the second copy
  40. instead of using any operator= that you've defined.
  41.  
  42. If you really do have a problem with this then either ensure that the
  43. object is destroyed before the fork() ...
  44.  
  45.     {
  46.        MY_CLASS my_class;
  47.        // use it
  48.     }
  49.     if(fork()==0){
  50.        // ...
  51.  
  52. ... or else don't declare it until you get into the true and/or false
  53. branches of the fork().
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. -- 
  120. Bruce.Hoult@bbs.actrix.gen.nz   Twisted pair: +64 4 477 2116
  121. BIX: brucehoult                 Last Resort:  PO Box 4145 Wellington, NZ
  122. "Cray's producing a 200 MIPS personal computer with 64MB RAM and a 1 GB
  123. hard disk that fits in your pocket!"   "Great!  Is it PC compatable?"
  124.