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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!ux1.cso.uiuc.edu!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc3.cs.uiuc.edu!pjl
  3. From: pjl@sparc3.cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: How to avoid calling destructor twice after fork()?
  5. Message-ID: <1992Jul29.210710.25075@sunb10.cs.uiuc.edu>
  6. Sender: news@sunb10.cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. References: <1992Jul29.180655.4716@cis.ohio-state.edu>
  9. Distribution: usa
  10. Date: Wed, 29 Jul 1992 21:07:10 GMT
  11. Lines: 35
  12.  
  13. In <1992Jul29.180655.4716@cis.ohio-state.edu> 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. The following figure shows the problem.
  19.  
  20. >class MY_CLASS;
  21. >main()
  22. >{    
  23. >   MY_CLASS my_class;
  24.  
  25. >   if(fork()==0)
  26. >    {
  27. >       // process 1
  28. >    }
  29. >   else
  30. >    {
  31. >       // process 2
  32. >    }
  33. >   
  34. >   // everything beyond this point will be executed twice, including
  35. >   // the destructor of my_class.
  36. >}
  37.  
  38. >Any suggestions?
  39.  
  40. *****>    fork() duplicates the entire process, data and all; so there
  41.     are two copies of all objects created by new.  Call the
  42.     destructor _once_per_process_ is what you want which is exactly
  43.     what it does now.
  44. -- 
  45.     - Paul J. Lucas                University of Illinois    
  46.       AT&T Bell Laboratories        at Urbana-Champaign
  47.       Naperville, IL            pjl@cs.uiuc.edu
  48.