[MUD-Dev] Re: Technical C/C++ coding question

Katrina McClelan kitkat at the486.bradley.edu
Tue Jun 16 18:20:53 New Zealand Standard Time 1998

On Tue, 16 Jun 1998, Jon Leonard wrote:
> On Sun, Jun 14, 1998 at 07:57:38AM -0700, J C Lawrence wrote:
> > Katrina McClelan<kitkat at the486.bradley.edu> wrote:
> > 
> > > 	if(!fork()) { /* child copy starts here */
> > >         kill(getpid(),SIGSEGV); /* this'll dump core */ 
> > >         sleep(10); /* make sure it stops here */ 
> > >         /* dead by here */ 
> > >       }
> > > 	/* parent continues unaware */
> > 
> If I read the code right, the sleep is in the child, to keep the child
> from executing additional parent code.  I'd include an exit() after the
> sleep, so that if the kill doesn't work (SIGSEGV blocked?), the child
> doesn't do any damage.

This is probably good.... the code I submitted was not meant (in my mind) 
to be fail safe.  The exit makes sure the child does no damage and is thus
good.  It's not really elegant, but then how elegant can you be when
you're trying to dump core ;)?  I assume this is only a tool and not going
to be a part of the finished server?  Even if it is, Ben can figure out
how to make it "perfect" from there.  SIGSEGV can be blocked, but
shouldn't be.  Actually I think the sleep is unneeded since the child
kills itself which is executed linear, so kill won't return until the
signal is sent and received thus killing the process in kill().  The exit
should be used incase kill fails. 

> That said, I'd use abort() instead of kill, and not bother with the sleep.
> I'd keep the exit() because I'm paranoid, and some older OSes may return
> from abort.  If the appropriate signal is precise, the sleep is useless
> anyway.

This is a better way to go, since abort can't return accd to the man page.

> Failure to fork would be good to test for.  If you're willing to accept
> a failure mode of no core file, the above code is find.  

I was assuming Ben was looking for just a quick and easy way and that he'd
add his own error checking.

> I don't see any particular reason to do this.  It seems cleaner to me
> to have the parent do nothing but the fork.

agreed.  The parent should IMO not get bogged down with this.
Particularly since there is an abort() command that I had never actually
used.  Learn something new every day  :)


More information about the MUD-Dev mailing list