Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <signal.h>
- #include <string.h>
- #include <fcntl.h>
- int pid_c1;
- int pid_c2;
- int pid_c3;
- int pid_p;
- int status;
- char litera;
- void rutina_sc (int a) {
- int pid = wait(&status);
- printf("copil %d terminat cu %d\n", pid, WEXITSTATUS(status));
- }
- void rutina_alarm (int a) {
- printf("1\n");
- alarm(1);
- }
- void rutina_usr (int a) {
- printf("C\n");
- }
- int main (int argc, char* argv[]) {
- int pfd13[2], pfd23[2];
- char buff[1000];
- int contor_cifre=0;
- int buff_size;
- int i;
- pid_p = getpid();
- struct sigaction sigActSC;
- sigActSC.sa_handler = rutina_sc;
- sigActSC.sa_flags = 0;
- struct sigaction sigActUSR;
- sigActUSR.sa_handler = rutina_usr;
- sigActUSR.sa_flags = 0;
- struct sigaction sigActALR;
- sigActALR.sa_handler = rutina_alarm;
- sigActALR.sa_flags = 0;
- sigaction(SIGCHLD, &sigActSC, NULL);
- sigaction(SIGUSR1, &sigActUSR, NULL);
- if (argc != 3) {
- printf("wrong arguments\n");
- exit(-1);
- }
- if (pipe(pfd13) < 0) {
- printf("error pipe 1-3\n");
- exit(-2);
- }
- if (pipe(pfd23) < 0) {
- printf("error pipe 2-3\n");
- exit(-3);
- }
- if ((pid_c1 = fork()) < 0) {
- printf("error forking 1\n");
- exit(-4);
- } else if (pid_c1 == 0) {
- close(pfd13[0]);
- close(pfd23[0]);
- close(pfd23[1]);
- dup2(pfd13[1], 1);
- execlp("cat", "cat", argv[1], NULL);
- printf("\n");
- close(pfd13[1]);
- //kill(pid_p, SIGCHLD);
- exit(1);
- } else if ((pid_c2 = fork()) < 0) {
- printf("error forking 2\n");
- exit(-5);
- } else if (pid_c2 == 0) {
- close(pfd13[0]);
- close(pfd13[1]);
- close(pfd23[0]);
- dup2(pfd23[1], 1);
- execlp("cat", "cat", argv[2], NULL);
- printf("\n");
- close(pfd23[1]);
- //printf("ana");
- //kill(pid_p, SIGCHLD);
- exit(1);
- } else if ((pid_c3 = fork()) < 0) {
- printf("error forking 3\n");
- exit(-6);
- } else if (pid_c3 == 0) {
- close(pfd13[1]);
- close(pfd23[1]);
- while (buff_size = read(pfd13[0], &buff, 100)) {
- for(i=0; i<buff_size; i++) {
- if(buff[i] >= '0' && buff[i] <= '9') {
- contor_cifre++;
- if (kill(pid_p, SIGUSR1) == 0) {
- //printf("error sigusr1\n");
- }
- }
- }
- }
- close(pfd13[0]);
- while (buff_size = read(pfd23[0], &buff, 1000)) {
- for(i=0; i<buff_size; i++) {
- if(buff[i] >= '0' && buff[i] <= '9') {
- contor_cifre++;
- kill(pid_p, SIGUSR1);
- }
- }
- }
- close(pfd23[0]);
- //kill(pid_p, SIGCHLD);
- exit(contor_cifre);
- } else {
- close(pfd13[0]);
- close(pfd13[1]);
- close(pfd23[0]);
- close(pfd23[1]);
- sigaction(SIGALRM, &sigActALR, NULL);
- alarm(1);
- while(1);
- exit(4);
- }
- }
Add Comment
Please, Sign In to add comment