Created
March 8, 2015 07:55
-
-
Save giannafusaro/ad7929ed4959473d06aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////// | |
// zombifier.cpp | |
// | |
// assignment: hw2 | |
// author: Gianna Fusaro | |
// date: 03/07/15 | |
// class: CS149 | |
// instructor: Dr. Gomez | |
/////////////////////////// | |
#include <unistd.h> | |
#include <iostream> | |
#include <csignal> | |
using namespace std; | |
void SummonTheReaper() { | |
cout << "\n ...\n"; | |
cout << " ;::::;\n"; | |
cout << " ;::::; :;\n"; | |
cout << " ;:::::' :;\n"; | |
cout << " ;:::::; ;.\n"; | |
cout << " ,:::::' ; OOO\\\n"; | |
cout << " ::::::; ; OOOOO\\\n"; | |
cout << " ;:::::; ; OOOOOOOO\n"; | |
cout << " ,;::::::; ;' / OOOOOOO\n"; | |
cout << " ;:::::::::`. ,,,;. / / DOOOOOO\n"; | |
cout << " .';:::::::::::::::::;, / / DOOOO\n"; | |
cout << " ,::::::;::::::;;;;::::;, / / DOOO\n"; | |
cout << " ;`::::::`'::::::;;;::::: ,#/ / DOOO\n"; | |
cout << " :`:::::::`;::::::;;::: ;::# / DOOO\n"; | |
cout << " ::`:::::::`;:::::::: ;::::# / DOO\n"; | |
cout << " `:`:::::::`;:::::: ;::::::#/ DOO\n"; | |
cout << " :::`:::::::`;; ;:::::::::## OO\n"; | |
cout << " ::::`:::::::`;::::::::;:::# OO\n"; | |
cout << " `:::::`::::::::::::;'`:;::# O\n"; | |
cout << " `:::::`::::::::;' / / `:#\n"; | |
cout << " ::::::`:::::;' / / `#\n"; | |
while(true) { | |
pid_t pid = wait(NULL); | |
if(pid == -1) { | |
cout << "\nAll zombies have been slaughtered!\n"; | |
cout << "\nCheck your running processes -- I'll just hang out here until you kill me by pressing enter.\n"; | |
cin.ignore(); | |
exit(0); | |
} else { | |
cout << "...Reaping zombie process " << pid << "\n"; | |
} | |
} | |
} | |
void signalHandler(int signal_number) { | |
// Use a switch statement so we can conveniently add more signal handlers in the future | |
switch(signal_number) { | |
case 20: | |
cout << "\nInterrupt signal 20) SIGCHLD received.\n"; | |
SummonTheReaper(); | |
break; | |
} | |
} | |
int main(int argc, char* argv[]) { | |
// Ensure the arguments are valid | |
try { | |
if(argc != 3) { | |
cout << "Incorrect number of arguments!\n"; throw(0); | |
} else if(strcmp(argv[1],"-n") != 0) { | |
cout << "Unknown option " << argv[1] << "\n"; throw(0); | |
} else if(atoi(argv[2]) < 1) { | |
cout << "Must specify desired number of zombies!\n"; throw(0); | |
} | |
} catch(...) { | |
cout << " Usage: zombifier -n <number of zombies>\n"; | |
exit(0); | |
} | |
// Declare variables | |
int i; | |
int n = atoi(argv[2]); | |
pid_t pid; | |
// Create requested number of zombie processes | |
cout << "Raising " << n << " zombies from the dead!\n\n"; | |
for (i = 0; i < n; i++) { | |
pid = fork(); | |
if (pid > 0) { | |
cout << "...Creating zombie process " << pid << "\n"; | |
sleep(1); | |
} else { | |
// Just kill the children immediately | |
exit(0); | |
} | |
} | |
cout << "\nYour zombie horde is ready! Check your running processes.\n"; | |
cout << " ..... \n"; | |
cout << " C C / \n"; | |
cout << " /< / \n"; | |
cout << " ___ __________/_#__=o \n"; | |
cout << " /(- /(\\_\\________ \\ \n"; | |
cout << " \\ ) \\ )_ \\o \\ \n"; | |
cout << " /|\\ /|\\ |' | \n"; | |
cout << " | _| \n"; | |
cout << " /o __\\ \n"; | |
cout << " / ' | \n"; | |
cout << " / / | \n"; | |
cout << " /_/\\______| \n"; | |
cout << " ( _( < \n"; | |
cout << " \\ \\ \\ \n"; | |
cout << " \\ \\ | \n"; | |
cout << " \\____\\____\\ \n"; | |
cout << " ____\\_\\__\\_\\ \n"; | |
cout << " /` /` o\\ \n"; | |
cout << " |___ |_______|\n"; | |
// Initalize the signal handler AFTER we create the zombies | |
signal(SIGCHLD, signalHandler); | |
// Tell the user how to kill the zombie processes | |
cout << "\nSend me a SIGCHLD signal or press enter to summon the grim reaper.\n"; | |
cin.get(); | |
SummonTheReaper(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment