Skip to content

Instantly share code, notes, and snippets.

@markogresak
Created January 30, 2015 14:58
Show Gist options
  • Select an option

  • Save markogresak/c9c19996b3ab5ecc76ca to your computer and use it in GitHub Desktop.

Select an option

Save markogresak/c9c19996b3ab5ecc76ca to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
char sign;
int energy;
void moreEnergy() {
energy += 10;
printf("Juhu! Nova energija! (%d).\n", energy);
}
void changeSign() {
sign = sign == '.' ? '*' : '.';
}
void newProcess() {
int child = fork();
if(child == 0) {
sleep((energy % 7) + 1);
int status = (42 * energy) % 128;
printf("Izhodni status childa: %d\n", status);
exit(status);
}
else if(child > 0)
printf("PID fork childa: %d\n", child);
}
void killZombie() {
int status;
wait(&status);
if(WIFEXITED(status)) {
status = WEXITSTATUS(status);
printf("Status zombija: %d\n", status);
}
}
int main(int argc, char* argv[]) {
signal(SIGTERM, moreEnergy);
signal(SIGUSR1, changeSign);
signal(SIGUSR2, newProcess);
signal(SIGCHLD, killZombie);
printf("PID: %d\n", getpid());
energy = argc == 1 ? 42 : atoi(argv[1]);
sign = '.';
while(energy > 0) {
printf("%c", sign);
fflush(0);
sleep(1);
energy--;
}
printf("\nOut of energy!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment