Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <stdlib.h>
- int fd, pid1, pid2, pid3;
- FILE *file;
- char arr[20];
- int N = 10;
- int finishedCount = 0;
- void process1_write(int signum)
- {
- printf ("Child 1 write to file, pid = %d\n", getpid());
- for (int i = 0; i < N; ++i)
- {
- printf("c1: %d\n", write(fd, "A", 1));
- }
- printf("Child 1 finished\n" );
- kill(pid1, SIGUSR1);
- }
- void process2_write(int signum)
- {
- printf ("Child 2 write to file, pid = %d\n", getpid());
- for (int i = 0; i < N; ++i)
- {
- printf("c2: %d\n", write(fd, "B", 1));
- }
- printf("Child 2 finished\n" );
- kill(pid1, SIGUSR1);
- }
- void process_main_result(int signum)
- {
- finishedCount++;
- printf("Finished: %d \n",finishedCount);
- if (finishedCount == 2)
- {
- file = fopen("res.txt", "r");
- fgets(arr, N*3, file);
- printf("Result: %s \n",arr);
- kill(pid2, SIGKILL);
- kill(pid3, SIGKILL);
- kill(pid1, SIGKILL);
- }
- }
- int main()
- {
- pid1 = getpid();
- fd = open("res.txt", O_CREAT | O_RDWR | O_TRUNC, 0666);
- printf("Введите количество байт:\n");
- scanf("%d",&N);
- signal(SIGUSR1, process1_write);
- pid2 = fork();
- if (pid2 != 0)
- {
- pid3 = fork();
- }
- if (pid2 == 0)
- {
- printf ("Child 1, pid = %d\n", getpid());
- signal(SIGUSR1, process1_write);
- for(;;);
- }
- else if (pid3 == 0)
- {
- printf ("Child 2, pid = %d\n", getpid());
- signal(SIGUSR1, process2_write);
- for(;;);
- }
- else
- {
- signal(SIGUSR1, process_main_result);
- sleep(5);
- kill(pid2, SIGUSR1);
- sleep(5);
- kill(pid3, SIGUSR1);
- for(;;);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement