Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <sys/wait.h>
- #include <sys/sem.h>
- #include <time.h>
- #include <signal.h>
- pid_t other_pid;
- pid_t p1, p2, p3, p4;
- void termhandler(int sgn) {
- signal(SIGINT, termhandler);
- exit(0);
- }
- void hndlr1(int sgn) {
- signal(SIGUSR1, hndlr1);
- // printf("hndlr1\n");
- // fflush(stdout);
- static int cnt = 0;
- ++cnt;
- if (cnt < 2) {
- return;
- }
- if (cnt == 2) {
- kill(getppid(), SIGUSR1);
- return;
- }
- if (cnt == 3) {
- printf("1");
- fflush(stdout);
- kill(getppid(), SIGUSR1);
- }
- if (cnt == 4) {
- kill(p3, SIGUSR1);
- return;
- }
- if (cnt == 5) {
- kill(p4, SIGUSR1);
- return;
- }
- if (cnt == 6) {
- kill(getppid(), SIGUSR1);
- }
- return;
- }
- void hndlr2(int sgn) {
- signal(SIGUSR1, hndlr2);
- // printf("hndlr2\n");
- // fflush(stdout);
- static int cnt = 0;
- ++cnt;
- if (cnt == 1) {
- kill(p1, SIGUSR1);
- return;
- }
- if (cnt == 2) {
- printf("2");
- fflush(stdout);
- kill(p1, SIGUSR1);
- return;
- }
- if (cnt == 3) {
- kill(0, SIGINT);
- }
- return;
- }
- void hndlr3(int sgn) {
- signal(SIGUSR1, hndlr2);
- // printf("hndlr3\n");
- // fflush(stdout);
- static int cnt = 0;
- ++cnt;
- if (cnt == 1) {
- printf("3");
- fflush(stdout);
- kill(getppid(), SIGUSR1);
- return;
- }
- return;
- }
- void hndlr4(int sgn) {
- signal(SIGUSR1, hndlr2);
- // printf("hndlr4\n");
- // fflush(stdout);
- static int cnt = 0;
- ++cnt;
- if (cnt == 1) {
- printf("4");
- fflush(stdout);
- kill(getppid(), SIGUSR1);
- return;
- }
- return;
- }
- int main(int argc, char **argv) {
- signal(SIGINT, termhandler);
- signal(SIGUSR1, hndlr2);
- p2 = getpid();
- p1 = fork();
- if (p1 == 0) {
- signal(SIGUSR1, hndlr1);
- p3 = fork();
- if (p3 == 0){
- signal(SIGUSR1, hndlr3);
- kill(getppid(), SIGUSR1);
- for(;;)
- sleep(1);
- }
- p4 = fork();
- if (p4 == 0) {
- signal(SIGUSR1, hndlr4);
- kill(getppid(), SIGUSR1);
- for(;;)
- sleep(1);
- }
- for(;;)
- sleep(1);
- }
- for (;;)
- sleep(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment