home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- 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
- From: pjl@sparc3.cs.uiuc.edu (Paul Lucas)
- Subject: Re: How to avoid calling destructor twice after fork()?
- Message-ID: <1992Jul29.210710.25075@sunb10.cs.uiuc.edu>
- Sender: news@sunb10.cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- References: <1992Jul29.180655.4716@cis.ohio-state.edu>
- Distribution: usa
- Date: Wed, 29 Jul 1992 21:07:10 GMT
- Lines: 35
-
- In <1992Jul29.180655.4716@cis.ohio-state.edu> gyu@cis.ohio-state.edu (george yu) writes:
-
- >I am using fork() system call in my C++ program. The problem is that each
- >calling of fork() creates a copy of calling process. The problem is any
- >instances created and not destroyed before fork() calling will be
- >destroyed twice. The following figure shows the problem.
-
- >class MY_CLASS;
- >main()
- >{
- > MY_CLASS my_class;
-
- > if(fork()==0)
- > {
- > // process 1
- > }
- > else
- > {
- > // process 2
- > }
- >
- > // everything beyond this point will be executed twice, including
- > // the destructor of my_class.
- >}
-
- >Any suggestions?
-
- *****> fork() duplicates the entire process, data and all; so there
- are two copies of all objects created by new. Call the
- destructor _once_per_process_ is what you want which is exactly
- what it does now.
- --
- - Paul J. Lucas University of Illinois
- AT&T Bell Laboratories at Urbana-Champaign
- Naperville, IL pjl@cs.uiuc.edu
-