69 lines
1.6 KiB
C
69 lines
1.6 KiB
C
#define _GNU_SOURCE
|
|
#include <sched.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
|
|
int childMain(void* args) {
|
|
return 0;
|
|
}
|
|
|
|
void spawnZombies(int number) {
|
|
int numForks = 0;
|
|
int neverStop;
|
|
int forkResult;
|
|
|
|
if(number > 0) {
|
|
printf("I will try to spawn %d zombies.\n", number);
|
|
neverStop = 0;
|
|
} else if(number == 0) {
|
|
printf("I will spawn as many zombies as possible.\n");
|
|
neverStop = 1;
|
|
} else {
|
|
printf("I have no idea what to do, doing nothing instead!");
|
|
return;
|
|
}
|
|
|
|
while(neverStop == 1 || numForks < number) {
|
|
//forkResult = clone(childMain, (void*) NULL, 0, (void*) NULL, (pid_t*) NULL, (struct user_desc*) NULL, (pid_t*) NULL);
|
|
forkResult = fork();
|
|
|
|
switch(forkResult) {
|
|
case -1:
|
|
// Forking did not work
|
|
break;
|
|
case 0:
|
|
// Child process at work, let's end this.
|
|
exit(0);
|
|
break;
|
|
default:
|
|
// Parent shall continue forking ...
|
|
numForks++;
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
int targetNumber;
|
|
if(argc == 2) {
|
|
targetNumber = atoi(argv[1]);
|
|
} else {
|
|
printf("Usage: %s number, pass 0 to spawn as many zombies as possible\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
spawnZombies(targetNumber);
|
|
|
|
printf("All zombies have been spawned. Use CTRL+C to exit.\n");
|
|
|
|
// Wait
|
|
while(1) {
|
|
sleep(60);
|
|
}
|
|
|
|
// That's it. Let somebody else clean up this mess.
|
|
return 0;
|
|
}
|