Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <wait.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- #include <limits.h>
- long long mult;
- pid_t gen0, gen1, gen2;
- int fd[2];
- void
- hndlr_end(int sig)
- {
- signal(SIGUSR1, hndlr_end);
- printf("hndlr_end\n");
- if (!gen2 && !gen1) {
- exit(0);
- } else {
- wait(NULL);
- exit(0);
- }
- }
- void
- hndlr(int sig)
- {
- signal(SIGUSR1, hndlr);
- printf("hndlr\n");
- printf("gen0= %d, gen1 = %d, gen2 = %d\n", gen0, gen1, gen2);
- long long elm;
- if (!gen2 && !gen1) { //grandson
- read(fd[0], &elm, sizeof(elm));
- printf("%lld\n", elm);
- elm *= mult;
- write(fd[1], &elm, sizeof(elm));
- kill(gen0, SIGUSR1);
- } else if (!gen1) { //son
- if (scanf("%lld", &elm) != 1) {
- printf("END\n");
- signal(SIGUSR1, hndlr_end);
- kill(gen2, SIGUSR1/*!SIGTERM*/);
- wait(NULL);
- printf("*********\n");
- kill(gen0, SIGUSR1);
- exit(0);
- }
- write(fd[1], &elm, sizeof(elm));
- kill(gen2, SIGUSR1);
- } else {
- read(fd[0], &elm, sizeof(elm));
- printf("%lld\n", elm);
- kill(gen1, SIGUSR1);
- }
- }
- int
- main(int argc, char **argv)
- {
- signal(SIGUSR1, hndlr);
- setbuf(stdin, NULL);
- setbuf(stdout, NULL);
- sscanf(argv[1], "%lld", &mult);
- pipe(fd);
- gen0 = getpid();
- gen1 = fork();
- if (gen1 < 0) {
- _exit(0);
- } else if (!gen1) {
- gen2 = fork();
- if (gen2 < 0) {
- _exit(0);
- } else if (!gen2) {
- kill(getppid(), SIGUSR1);
- while (1) {
- pause();
- }
- } else {
- while (1) {
- pause();
- }
- }
- } else {
- while (1) {
- pause();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement