Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- int stop = 0;
- int finish = 0;
- // Signal handling
- void parent_sigusr()
- {
- signal(SIGUSR1, parent_sigusr);
- stop = 1;
- // printf("parent stop %d\n", getpid());
- }
- void parent_sigcont()
- {
- signal(SIGCONT, parent_sigcont);
- stop = 0;
- // printf("parent resume %d\n", getpid());
- }
- void parent_sigint()
- {
- signal(SIGINT, parent_sigint);
- finish = 1;
- // printf("parent kill %d\n", getpid());
- }
- void child_sigusr()
- {
- signal(SIGUSR1, child_sigusr);
- stop = 1;
- // printf("p %d stop\n", getpid());
- }
- void child_sigcont()
- {
- signal(SIGCONT, child_sigcont);
- stop = 0;
- // printf("p %d resume\n", getpid());
- }
- void child_sigint()
- {
- // printf("p %d kill\n", getpid());
- kill(getppid(), SIGINT);
- exit(0);
- }
- int main()
- {
- pid_t wpid;
- char *fpath = "/tmp/fifopath";
- char *filepath = "/tmp/filepath.txt";
- FILE *fp_read;
- fp_read = fopen(filepath, "a+");
- int p1 = fork();
- if (p1 != 0) {
- // printf("p0 pid = %d\n", getpid());
- // printf("p1 pid = %d\n", p1);
- int p2 = fork();
- if (p2 != 0) {
- // printf("p2 pid = %d\n", p2);
- int p3 = fork();
- if (p3 != 0) {
- // printf("p3 pid = %d\n", p3);
- signal(SIGINT, parent_sigint);
- signal(SIGUSR1, parent_sigusr);
- signal(SIGCONT, parent_sigcont);
- int p_counter = 0;
- int stop_sent = 0;
- // Wait for all processes to finish
- while (1) {
- while (stop == 1) {
- // Means we need to stop - send info to children
- if (stop_sent == 0) {
- kill(p1, SIGUSR1);
- kill(p2, SIGUSR1);
- kill(p3, SIGUSR1);
- stop_sent = 1;
- }
- pause();
- // If we got SIGINT we need to exit the stop
- if (finish == 1) {
- break;
- }
- // We got a signal to continue so propagate to children
- if (stop == 0) {
- kill(p1, SIGCONT);
- kill(p2, SIGCONT);
- kill(p3, SIGCONT);
- }
- }
- stop_sent = 0;
- if (finish == 1) {
- kill(p1, SIGINT);
- kill(p2, SIGINT);
- kill(p3, SIGINT);
- }
- finish = 0;
- // Wait for children but without stopping execution
- // so we can propagate signals
- wpid = waitpid(-1, NULL, WNOHANG);
- if (wpid != 0) {
- finish = 1;
- p_counter = p_counter + 1;
- // If all children exit we exit
- if (p_counter == 3) {
- break;
- }
- }
- }
- fclose(fp_read);
- exit(0);
- } else {
- // P3
- signal(SIGINT, child_sigint);
- signal(SIGUSR1, child_sigusr);
- signal(SIGCONT, child_sigcont);
- char buff[2];
- int ok;
- int i = 0;
- while(1) {
- while (stop == 1) {
- kill(getppid(), SIGUSR1);
- pause();
- if (stop == 0) {
- kill(getppid(), SIGCONT);
- }
- }
- // Reading from a file
- ok = fscanf(fp_read, "%2s", buff);
- // ok > 0 means we read something
- if (ok > 0) {
- // print on the standard error
- fprintf(stderr, "%s ", buff);
- if (i == 14)
- fprintf(stderr, "\n");
- i = (i + 1) % 15;
- }
- }
- }
- } else {
- // P2
- signal(SIGINT, child_sigint);
- signal(SIGUSR1, child_sigusr);
- signal(SIGCONT, child_sigcont);
- char read_c;
- char converted_c[3];
- int fd_read = open(fpath, O_RDONLY);
- FILE *fp_write;
- while(read(fd_read, (void *)&read_c, 1)) {
- while (stop == 1) {
- // send to mother pid
- kill(getppid(), SIGUSR1);
- pause();
- if (stop == 0) {
- kill(getppid(), SIGCONT);
- }
- }
- // Converting to hex
- sprintf((char*)(converted_c),"%02X", read_c);
- // Add ending character
- converted_c[2] = '\0';
- // open, write to file and close
- fp_write = fopen(filepath, "a");
- fprintf(fp_write, "%s", converted_c);
- fclose(fp_write);
- }
- exit(0);
- }
- } else {
- // P1
- signal(SIGINT, child_sigint);
- signal(SIGUSR1, child_sigusr);
- signal(SIGCONT, child_sigcont);
- int fd_write;
- mkfifo(fpath, 0666);
- // open fifo
- fd_write = open(fpath, O_WRONLY);
- char c;
- while ((c = getchar()) != EOF) {
- while (stop == 1) {
- // Notify P0 to STOP
- kill(getppid(), SIGUSR1);
- pause();
- if (stop == 0) {
- kill(getppid(), SIGCONT);
- }
- }
- // write to fifo what we read from
- write(fd_write, (void *)&c, 1);
- }
- close(fd_write);
- exit(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement