Games Coders Play
March 19, 2004 7:44 PM   Subscribe

Coders to the rescue! See, I write a silly advice column. It's not syndicated or anything, as I'm still working the bugs out of the voice before I pitch it to anyone...but that's not the point. I've been outgeeked by a question...and need some real coders to help me craft a response.

As part of a question, where the querent has claimed "ll33t haxor skills", he send a perl script designed to be a zombie killer. (It helps to know that Winifred's estate uses trained zombies...but anyway...)

Here is his script:
#!/usr/bin/perl -lw

use strict; # necromancer
my $zombie,$|++,print $/;

$SIG{TERM} = sub {
hit $zombie;
destroy $zombie and return unless $zombie->walks;
print qq|"BRAINS!" (|,$zombie->health,' left)';
};

for (1..6) {
next if fork;
$zombie = raise Zombie;
sleep 1 and regen $zombie while 666;
}

package Zombie;
sub raise {print "$$ rises" and bless {hits=>int(rand 5)+2},shift}
sub hit {shift->{hits}--}
sub walks {shift->{hits} > 1}
sub health {$_=shift->{hits};$_ > 1 ?"$_ hits":"$_ hit"}
sub regen {shift->{hits}+=3 if int(rand 2)}
sub destroy{print qq|"brainsss..." zombie destroyed| and exit}


It blows up in NT...haven't tried it in linux or unix yet. I was trying to craft a cute C++ response...what with the forking and the child processes spawing zombie processes...but I suck at C++ and everything I've tried actually terminates the child process without creating zombies. I need code to create zombies. (Not real zombie processes...but in response to the perl zombie killer.) Anyone got any ideas?
posted by dejah420 to Computers & Internet (7 answers total)
 
I don't understand your question. You don't want to make real zombie processes, okay. You just want to do something like what this perl code does?

You can use fork and an unending while loop just like he does. Is that what you're doing?

And I don't understand how that code is a "zombie killer." It just spawns a few "zombies" for the user to kill by banging ctl-c a lot...
posted by whatnotever at 8:11 PM on March 19, 2004


Oh, not ctl-c, because it exits. You actually have to send the TERM signal some other way. But my confusion about what you want to do still stands. It sounds more like he's posed the question of how to kill the zombies that are created by the perl script?
posted by whatnotever at 8:22 PM on March 19, 2004


This doesn't directly answer your question, but it blows up under NT because Perl on Win32 doesn't implement POSIX signal handlers, which is what $SIG{TERM} = coderef is, and because fork() isn't right on Win32.

In any case, these processes aren't zombies. They're processes with SIGTERM signal handlers. The real effect will be running processes (not actual zombies, which are something else entirely) which have to be killed repeatedly or sent SIGKILL before they terminate.

As far as I know, there isn't a way to do what this script does under Win32 because the game is played using an IPC mechanism that Windows doesn't have.
posted by majick at 9:51 PM on March 19, 2004


To expand a bit: Zombies are processes that have exited, but have not been reaped yet. Reaping happens when either the parent process calls wait (or wait4 or waitpid) on the process, or the parent dies (in which case the process gets reparented to init and init does the reaping). You cannot kill a zombie process in other way than forcing the parent process to wait for it or killing the parent process; They are no longer running and cannot run signal handlers anway.

Incidentally, wasn't NT supposed to have a POSIX compatibility layer (that was more trouble than it was worth so everybody turned it off, but still)?
posted by fvw at 12:25 AM on March 20, 2004


Yes, there's a POSIX layer (and an OS/2 1.2 layer as well), but it's not accessible from Win32 and Perl doesn't use it, mostly because it's hideously broken. I think it's based -- obviously with heavy modification -- on some ancient Xenix code Microsoft owns, and Xenix sucks.

For the purposes of the rest of this answer, I'm not going to use the word "zombie" because there's lots of confusion about it.

In any case, a Unix C version of this game, code that produces child processes which take a number of SIGTERM signals to kill, is almost as trivial. The "hard" bit is the signal handler. The rest is more or less the same: A for loop that fork()s 6 times and then goes into an infinite loop, sleeping and regenerating the child process' hit points.

If you really wanted to do a C++ equivalent, you'd make a class with the raise/hit/regen/walks methods and attribute, loop and fork(), creating a new instance for each child process. The semantics would be more or less the same, just couched in OO terms.

Don't know how to do Unix signal handling in C? Don't worry, it's pretty easy.
posted by majick at 5:47 AM on March 20, 2004


Response by poster: Thanks gang. :) whatnotever, that's what I thought the script did...but since I couldn't get it to run, I wasn't sure. ;)

I'm sorry if my description of what I was shooting for was too vague...but I'm not really a coder...so I'm a little out of my depth.

Basically, the perl writer was doing a geek joke...which I got, but wasn't sure how to respond to in such a way that it was also geeky...rather than just saying, "Nu uh!". ;)

So, my logic process was, "damn, that's kinda funny. Ok, what trumps perl? C++ trumps perl. Crap, I haven't done C in a decade...and even then, I sucked at it. Ok, I should be able to spawn zombie processes by losing the wait argument...how can I write a snippet that generates zombies?" and that's as far as I got. I spent an hour or so looking at c sites...but I just confused myself even more. ;)

The disclaimer in the post, which wasn't well phrased, was meant so that people didn't think I was actually trying to write code that would spawn zombie "processes" that could bog down a system...but I was trying to write silly code that purported to generate "actual" zombies...as in the shuffling, brain-eating variety. I got lost, I think, in reading up on forks and processes and thereby confused real code with silly code. ;)

Methinks that I have been thoroughly outgeeked. It was bound to happen. These young whippersnappers and their perl jokes. Why, in my day, we had Awk and vi and we LIKED it!
posted by dejah420 at 9:53 AM on March 20, 2004


you could reply by discussing the robin hood / friar tuck hack - young whippersnappers won't have heard of that, probably... (related in that you kill one and the other comes to the rescue, iirc).
posted by andrew cooke at 4:27 AM on March 21, 2004


« Older Linux/BSD   |   Cheap travel from NYC to Vermont Newer »
This thread is closed to new comments.