#include #include #include #include #include #include #include volatile int flag1 = 0, flag2 = 0; volatile char abc = 0; volatile int k = 0; volatile pid_t p1, p2, pp; void dadhandler(int signo) { if (signo == SIGIO) { wait(NULL); wait(NULL); _exit(0); } else { kill(p2, SIGALRM); } } void flg(int signo) { flag1 = 1; } void handler(int signo) { //printf("sig : %d %d\n", k, abc); if(signo == SIGUSR1) { if(k == CHAR_BIT - 1) { abc <<= 1; printf("%c", abc); abc = 0; k = 0; } else { k++; abc <<= 1; } } else if(signo == SIGUSR2) { if(k == CHAR_BIT - 1) { abc = (abc << 1) + 1; printf("%c", abc); abc = 0; k = 0; } else { k++; abc = (abc << 1) + 1; } } else if(signo == SIGIO) { kill(pp, SIGIO); printf("\n"); _exit(0); } flag2 = 1; fflush(stdout); } int main(int argc, char *argv[]) { pp = getpid(); struct sigaction ppsa; sigemptyset(&ppsa.sa_mask); ppsa.sa_handler = dadhandler; ppsa.sa_flags = SA_RESTART; sigaction(SIGALRM, &ppsa, NULL); sigaction(SIGIO, &ppsa, NULL); sigset_t pps; sigaddset(&pps, SIGUSR1); sigaddset(&pps, SIGUSR2); sigaddset(&pps, SIGIO); sigaddset(&pps, SIGALRM); sigprocmask(SIG_BLOCK, &pps, NULL); p1 = fork(); if(!p1) { struct sigaction sa; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; sa.sa_handler = handler; sigaction(SIGUSR1, &sa, NULL); sigaction(SIGUSR2, &sa, NULL); sigaction(SIGIO, &sa, NULL); sigprocmask(SIG_UNBLOCK, &pps, NULL); while(1) { while(!flag2); flag2 = 0; kill(pp, SIGALRM); } _exit(0); } p2 = fork(); if(!p2) { int fd = open(argv[1], O_RDONLY); char c; struct sigaction alr; sigemptyset(&alr.sa_mask); alr.sa_handler = flg; alr.sa_flags = SA_RESTART; sigaction(SIGALRM, &alr, NULL); sigprocmask(SIG_UNBLOCK, &pps, NULL); while(read(fd, &c, sizeof(c) == sizeof(c))) { for(int i = CHAR_BIT - 1; i >= 0; --i) { if(c >> i & 1) { kill(p1, SIGUSR2); } else { kill(p1, SIGUSR1); } while(!flag1); flag1 = 0; } } close(fd); kill(p1, SIGIO); _exit(0); } sigprocmask(SIG_UNBLOCK, &pps, NULL); while(1) { pause(); } return 0; }