Advertisement
Alx09

Untitled

Jun 4th, 2022
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <arpa/inet.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <signal.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <sys/wait.h>
  12.  
  13. pid_t fiu;
  14. int pipeMesaj[2];
  15. void handlerSIGUSR1(int semnal){
  16.         printf("proces fiu a gasit cuvantul\n");
  17.     exit(0); // oprim proces parinte   
  18. }
  19. void handlerSIGALRM(){
  20.   printf("proces fiu nu a gasit cuvantul\n");
  21.   kill(fiu, SIGINT);
  22.   exit(0);// oprim process parinte
  23. }
  24.  
  25. void main(int argc, char * argv[]){
  26. char buffer[500];
  27. int fileIn, bytesRead;
  28.  
  29. pipe(pipeMesaj);
  30.  
  31. if(fiu = fork()){ //parinte
  32.    signal(SIGALRM, handlerSIGALRM);
  33.    signal(SIGUSR1, handlerSIGUSR1);
  34.    alarm(5);
  35.    fileIn = open(argv[1], O_RDONLY);   
  36.    close(pipeMesaj[0]);
  37.   while( bytesRead = read(fileIn,  &buffer, sizeof(buffer)) != 0)
  38.     write(pipeMesaj[1], &buffer, sizeof(buffer));
  39.   wait(0);
  40.   }
  41.   else{ // fiu
  42.        close(pipeMesaj[1]);
  43.     while(read(pipeMesaj[0], &buffer, sizeof(buffer)) != 0){
  44.         if(strstr(buffer, argv[2]) != NULL){
  45.         kill((int)getppid(), SIGUSR1);
  46.         exit(0);
  47.         }
  48.    
  49.        
  50.   }
  51.  
  52.  
  53. }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement