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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cis.ohio-state.edu!eggplant.cis.ohio-state.edu!gyu
  3. From: gyu@cis.ohio-state.edu (george yu)
  4. Subject: How to avoid calling destructor twice after fork()?
  5. Message-ID: <1992Jul29.180655.4716@cis.ohio-state.edu>
  6. Originator: gyu@eggplant.cis.ohio-state.edu
  7. Sender: news@cis.ohio-state.edu (NETnews        )
  8. Organization: The Ohio State University, Department of Computer and Information Science
  9. Date: Wed, 29 Jul 1992 18:06:55 GMT
  10. Lines: 28
  11.  
  12. I am using fork() system call in my C++ program. The problem is that each
  13. calling of fork() creates a copy of calling process. The problem is any
  14. instances created and not destroyed before fork() calling will be 
  15. destroyed twice. The following figure shows the problem.
  16.  
  17. class MY_CLASS;
  18. main()
  19. {    
  20.    MY_CLASS my_class;
  21.  
  22.    if(fork()==0)
  23.     {
  24.        // process 1
  25.     }
  26.    else
  27.     {
  28.        // process 2
  29.     }
  30.    
  31.    // everything beyond this point will be executed twice, including
  32.    // the destructor of my_class.
  33. }
  34.  
  35. Any suggestions?
  36.  
  37. Thanks in advance.
  38.  
  39. George Yu
  40.