Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <stdio.h>
- #include <string.h>
- int file_pipes[2];
- int buffer[1];
- pid_t fork_result;
- int N = 20;
- // Обработчик для родительского процесса, который прочитает, а потом запишет.
- void parent_rw(int nsig)
- {
- read(file_pipes[0], buffer, sizeof(buffer));
- printf("Parent has read: %s\n", buffer[0]);
- if (buffer[0] >= N)
- exit(EXIT_SUCCESS);
- buffer[0] += 1;
- write(file_pipes[1], buffer, sizeof(buffer));
- printf("Parent has sent data\n");
- kill(fork_result, SIGUSR1);
- }
- void child_write(int nsig)
- {
- read(file_pipes[0], buffer, sizeof(buffer));
- printf("Child has read: %s\n", buffer[0]);
- if (buffer[0] >= N)
- exit(EXIT_SUCCESS);
- buffer[0] += 1;
- write(file_pipes[1], buffer, sizeof(buffer));
- printf("Child has sent data\n");
- kill(getppid(), SIGUSR1);
- }
- int main()
- {
- (void)signal(SIGUSR2, child_write);
- memset(buffer, '0', sizeof(buffer));
- if (pipe(file_pipes) == 0)
- {
- fork_result = fork();
- if (fork_result == -1)
- {
- fprintf(stderr, "Fork failure");
- exit(EXIT_FAILURE);
- }
- if (fork_result == 0) // child.
- while(1);
- else
- { // parent
- (void)signal(SIGUSR1, parent_rw);
- buffer[0] = 1;
- write(file_pipes[1], buffer, sizeof(buffer));
- printf("Parent has sent data\n");
- kill(fork_result, SIGUSR2);
- while(1);
- }
- }
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement