Created
January 30, 2015 14:58
-
-
Save markogresak/c9c19996b3ab5ecc76ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #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