stuk06

Untitled

Jun 10th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.94 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <sys/ipc.h>
  6. #include <sys/sem.h>   
  7. #include <err.h>
  8. #include <stdlib.h>
  9. #include <sys/stat.h>
  10. #include <wait.h>
  11. #include <errno.h>
  12. #include <pthread.h>
  13. #include <time.h>
  14. #include <sys/prctl.h>
  15.  
  16. #define FIFO "/tmp/my_fifo"
  17. #define BUFF 1
  18. #define COUNT 2
  19.  
  20. #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
  21. #else
  22.     union semun {
  23.         int val;               
  24.         struct semid_ds *buf;          
  25.         unsigned short int *array; 
  26.         struct seminfo *__buf;         
  27.     };
  28. #endif
  29.  
  30.  
  31. int  des;
  32. int pid;
  33. int desF;
  34. char buf[BUFF];
  35. int n;
  36. int n2;
  37. //char buf1[BUFF];
  38. unsigned char buf2[BUFF];
  39. char xA[10];
  40.  
  41. int   semid;
  42. int   semid2;  
  43. int   semid3;
  44.  
  45. /*--Pipe--*/
  46. int pdes[2];
  47. int counter = 0;
  48. char string[]={'a'};
  49.  
  50. int wartP = 0;
  51. int wartD = 0;
  52. int zm=0;
  53. int zm2=0;
  54. int zm3=0;
  55. int zm4=0;
  56.  
  57. int semlock(int semid) {
  58.     struct sembuf opr; 
  59.  
  60.     opr.sem_num =  0;
  61.     opr.sem_op  = -1;        // blokowanie
  62.     opr.sem_flg =  0;        // operacja blokujaca
  63.            
  64.     if (semop(semid, &opr, 1) == -1){
  65.         warn("Blad blokowania semafora!");
  66.         return 0;
  67.     }else{
  68.         return 1;
  69.     }
  70. }
  71.  
  72. int semunlock(int semid) {
  73.     struct sembuf opr; 
  74.  
  75.     opr.sem_num = 0;
  76.     opr.sem_op  = 1;
  77.     opr.sem_flg = 0;
  78.            
  79.     if (semop(semid, &opr, 1) == -1){
  80.         warn("Blad odblokowania semafora!");
  81.         return 0;
  82.     }else{
  83.         return 1;
  84.     }
  85. }
  86.  
  87. void zablokowanieProcP(int signal)
  88. {
  89.     if(zm==0)
  90.     {
  91.         psignal(signal,"\nProces 1: Otrzymalem sygnal");
  92.         printf("Proces 1 wstrzymany\n");
  93.         zm=1;
  94.  
  95.         semlock(semid3);
  96.         semlock(semid2);   
  97.     }
  98.     else psignal(signal,"\nNie można ponownie użyć sygnału: ");
  99.     //sleep(3);
  100.    
  101.     return;
  102. }
  103.  
  104. void odblokowanieProcP(int signal)
  105. {
  106.     if(zm==1)
  107.     {
  108.         psignal(signal,"\nProces 1: Otrzymalem sygnal");
  109.         printf("Proces 1 wznowiony\n");
  110.        
  111.         zm=0;
  112.        
  113.         semunlock(semid3);
  114.         semunlock(semid2);
  115.  
  116.     }
  117.     else psignal(signal,"\nNie można ponownie użyć sygnału: ");
  118.     return;
  119. }
  120.  
  121. void zablokowanieProcD(int signal){
  122.     if(zm2==0)
  123.     {
  124.         psignal(signal,"\nProces 2: Otrzymalem sygnal");
  125.         printf("Proces 2 wstrzymany\n");
  126.  
  127.         zm2=1;
  128.  
  129.         semlock(semid2);
  130.         semlock(semid3);   
  131.     }
  132.     else psignal(signal,"\nNie można ponownie użyć sygnału: ");
  133.     //sleep(3);
  134.    
  135.     return;
  136. }
  137.  
  138. void odblokowanieProcD(int signal){
  139.     if(zm2==1)
  140.     {
  141.         psignal(signal,"\nProces 2: Otrzymalem sygnal");
  142.         printf("Proces 1 wznowiony\n");
  143.        
  144.         zm2=0;
  145.        
  146.         semunlock(semid2);
  147.         semunlock(semid3);
  148.  
  149.     }
  150.     else psignal(signal,"\nNie można ponownie użyć sygnału: ");
  151.     return;
  152. }
  153.  
  154. void zabicieProcP(int signal){
  155.     if(zm3==0)
  156.     {
  157.         psignal(signal,"\nProces 1: Otrzymalem sygnal");
  158.         printf("Proces 1 zabity\n");
  159.         wartP=1;
  160.     }
  161.     else psignal(signal,"\nNie można ponownie użyć sygnału: ");
  162.     //sleep(3);
  163.    
  164.     return;
  165. }
  166.  
  167. void zabicieProcD(int signal){
  168.     if(zm4==0)
  169.     {
  170.         psignal(signal,"\nProces 2: Otrzymalem sygnal");
  171.         printf("Proces 2 zabity\n");
  172.         wartD=1;
  173.         semunlock(semid);
  174.     }
  175.     else psignal(signal,"\nNie można ponownie użyć sygnału: ");
  176.     //sleep(3);
  177.    
  178.     return;
  179. }
  180.  
  181.  
  182. /*----Proces 1------*/
  183. void *threadProcess1(void *arg) {
  184.  
  185.     signal(SIGUSR1, zablokowanieProcP);
  186.     signal(SIGUSR2, odblokowanieProcP);
  187.     signal(SIGBUS, zabicieProcP);
  188.    
  189.     des = open( "/tmp/file.txt", O_WRONLY);
  190.     while(1)
  191.     {              
  192.        
  193.         semlock(semid);     //wejście w proces 1
  194.         if((semctl(semid2, 0, GETVAL))==1) break;
  195.         lseek(des,0,SEEK_SET);
  196.         n=read(0,buf,sizeof(buf));
  197.         if(n==0 || wartP==1)
  198.         {
  199.             semunlock(semid2);
  200.             break;         
  201.         }
  202.        
  203.  
  204.         printf("Proces 1: %d\n",*buf);
  205.         write(des,buf,sizeof(buf));
  206.        
  207.         semunlock(semid2);  //odblokowanie procesu 2
  208.        
  209.         semunlock(semid3);
  210.         semlock(semid3);
  211.        
  212.  
  213.         bzero(buf,sizeof(buf));
  214.         //bzero(buf2,sizeof(buf));
  215.     }
  216.     printf("Proces1 zakonczyl dzialanie\n");
  217.     semunlock(semid2);
  218.     //close(des);
  219.     pthread_exit(0);
  220. }
  221.  
  222. /*----Proces 2------*/
  223. void *threadProcess2(void *arg) {
  224.    
  225.     signal(SIGUSR1, zablokowanieProcD);
  226.     signal(SIGUSR2, odblokowanieProcD);
  227.     signal(SIGBUS, zabicieProcD);
  228.        
  229.     des=open("/tmp/file.txt",O_RDONLY);
  230.            
  231.     desF = open(FIFO,O_WRONLY);
  232.     while(1)
  233.     {                                              
  234.        
  235.         semlock(semid2);    //wejście w proces 2
  236.         if((semctl(semid, 0, GETVAL))==1 || wartD==1)   //semafor procesu pierwszego jest 1, a w tym miejscu powinien byc na 0, gdyby proces 1 był aktywny
  237.         {      
  238.             //printf("tut\n"); 
  239.             break;
  240.         }
  241.         lseek(des,0,SEEK_SET);                       
  242.         n=read(des,buf,sizeof(buf));
  243.        
  244.        
  245.         printf("Proces 2: %d\n",*buf);
  246.  
  247.  
  248.         semunlock(semid);   //odblokowanie procesu 1
  249.                
  250.         write(desF,buf,sizeof(buf));
  251.         printf("FIFO zapis: %d\n",*buf);
  252.  
  253.         bzero(buf,sizeof(buf));
  254.                
  255.     }
  256.     printf("Proces 2 zakonczyl dzialanie\n");
  257.     semunlock(semid2);
  258.     //close(desF);
  259.     close(des);
  260.     //unlink(FIFO);
  261.    
  262.     pthread_exit(0);
  263. }
  264.  
  265.  
  266.  
  267. int main(void)
  268. {
  269.     int err;
  270.     int cnt;
  271.     /* tablica, w ktorej umiescimy informacje watkach :-) */
  272.     pthread_t p_thread[COUNT];
  273.  
  274.     //Dolaczamy wszystkie watki, czyli czekamy, az sie zakoncza.
  275.     /*
  276.     for(cnt = 0; cnt < COUNT; cnt++)
  277.     {
  278.         pthread_join(p_thread[cnt], NULL);
  279.     }*/
  280.    
  281.    
  282.    
  283.     des = open( "/tmp/file.txt", O_CREAT | O_RDWR | O_APPEND,  0666 );
  284.     close(des);
  285.    
  286.     /*Semafor 1*/
  287.     key_t key;         
  288.            
  289.     union semun ctl;       
  290.  
  291.     if ((key = ftok(".", 'A')) == -1)
  292.         errx(1, "Blad tworzenia klucza!");
  293.  
  294.     if ((semid = semget(key, 1, IPC_CREAT | 0600)) == -1)
  295.         errx(2, "Blad tworzenia semafora!");
  296.    
  297.     /* Inicjujemy semafor jako podniesiony (S = 1). */
  298.     ctl.val = 1;
  299.     if (semctl(semid, 0, SETVAL, ctl) == -1)
  300.         errx(3, "Blad ustawiania semafora!");
  301.     //semunlock(semid);
  302.    
  303.     /*Semafor 2*/
  304.     key_t key2;        
  305.                
  306.  
  307.     if ((key2 = ftok(".", 'B')) == -1)
  308.         errx(1, "Blad tworzenia klucza!");
  309.  
  310.     if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1)
  311.         errx(2, "Blad tworzenia semafora!");
  312.    
  313.     /* Inicjujemy semafor jako podniesiony (S = 1). */
  314.     ctl.val = 1;
  315.     if (semctl(semid2, 0, SETVAL, ctl) == -1)
  316.         errx(3, "Blad ustawiania semafora!");
  317.  
  318.     semlock(semid2);    //wstrzymanie procesu 2
  319.  
  320.     /*------------------*/
  321.    
  322.     /*Semafor 3 - synchronizacja sygnałów z proc1 do proc2*/
  323.     key_t key3;        
  324.                    
  325.  
  326.     if ((key3 = ftok(".", 'C')) == -1)
  327.         errx(1, "Blad tworzenia klucza!");
  328.  
  329.     if ((semid3 = semget(key3, 1, IPC_CREAT | 0600)) == -1)
  330.         errx(2, "Blad tworzenia semafora!");
  331.    
  332.     /* Inicjujemy semafor jako podniesiony (S = 1). */
  333.     ctl.val = 1;
  334.     if (semctl(semid3, 0, SETVAL, ctl) == -1)
  335.         errx(3, "Blad ustawiania semafora!");
  336.  
  337.     semlock(semid3);    //wstrzymanie procesu 3
  338.    
  339.    
  340.     /*Semafor 4 - synchronizacja sygnałów z proc2 do proc3*/
  341.     key_t key4;        
  342.     int   semid4;              
  343.  
  344.     if ((key4 = ftok(".", 'D')) == -1)
  345.         errx(1, "Blad tworzenia klucza!");
  346.  
  347.     if ((semid4= semget(key4, 1, IPC_CREAT | 0600)) == -1)
  348.         errx(2, "Blad tworzenia semafora!");
  349.    
  350.     /* Inicjujemy semafor jako podniesiony (S = 1). */
  351.     ctl.val = 1;
  352.     if (semctl(semid4, 0, SETVAL, ctl) == -1)
  353.         errx(3, "Blad ustawiania semafora!");
  354.  
  355.     semlock(semid4);    //wstrzymanie procesu 4
  356.    
  357.     /*------------------------------------------*/
  358.    
  359.    
  360.     /*FIFO*/
  361.     umask(0);
  362.     mknod(FIFO, S_IFIFO|0666, 0);
  363.    
  364.     /*--Pipe--*/
  365.     pipe(pdes);
  366.    
  367.     //signal(SIGUSR1, zablokowanieProcP);
  368.     //signal(SIGUSR2, odblokowanieProcP);
  369.    
  370.     /* Proces 1 - pobiera dane z STDIN (deskryptor 0) i zapisuje w pliku */
  371.     if(fork())
  372.     {
  373.         //rodzic
  374.         /* Proces 2 - odczytuje z pliku i przekazuje dane za pomocą FIFO do procesu 3 */
  375.         if(fork())
  376.         { //rodzic
  377.             if(fork())
  378.             {
  379.                 /*for(cnt = 0; cnt < COUNT; cnt++)
  380.                 {
  381.                     pthread_join(p_thread[cnt], NULL);
  382.                 }*/
  383.                 while( ( pid = waitpid(-1, NULL, 0) ) )
  384.                 {
  385.                     if (errno == ECHILD)
  386.                     {
  387.                         break;
  388.                     }
  389.                 }  
  390.             }
  391.             else
  392.             {
  393.                 //signal(SIGUSR1, zablokowanieProcT);
  394.                 //signal(SIGUSR2, odblokowanieProcT);
  395.                 prctl(PR_SET_NAME,"Proces3",0,0,0);
  396.                
  397.                 /* Proces 3 - wypisuje otrzymane dane na STDOUT (deskryptor 1) */
  398.                 desF = open(FIFO,O_RDONLY);
  399.                
  400.                 while(1)
  401.                 {      
  402.                     semunlock(semid3);         
  403.                     n=read(desF,buf2,sizeof(buf2));
  404.                    
  405.                     if(n==0)
  406.                     {          
  407.                         break;
  408.                     }
  409.                    
  410.                     printf("FIFO odczyt: %x\n",*buf2);
  411.                     //write(1,buf2,sizeof(buf2));
  412.                     semlock(semid3);   
  413.                     bzero(buf2,sizeof(buf2));
  414.                 }
  415.                 printf("Proces3 zakonczyl dzialanie\n");
  416.                 close(desF);
  417.                 unlink(FIFO);
  418.                 //sleep(100);
  419.             }
  420.        
  421.         }
  422.         else
  423.         { //dziecko - proces 2
  424.        
  425.             cnt=1;
  426.             err = pthread_create(&p_thread[cnt], NULL, threadProcess2, (void *)cnt);
  427.             if(err != 0)
  428.                 fprintf(stderr, "Error creating the thread\n");
  429.             prctl(PR_SET_NAME,"Proces2",0,0,0);
  430.             pthread_join(p_thread[1], NULL);
  431.         }
  432.  
  433.        
  434.        
  435.     }
  436.     else //dziecko - proces 1
  437.     {
  438.         cnt=0;
  439.         err = pthread_create(&p_thread[cnt], NULL, threadProcess1, (void *)cnt);
  440.         if(err != 0)
  441.             fprintf(stderr, "Error creating the thread\n");
  442.             prctl(PR_SET_NAME,"Proces1",0,0,0);
  443.         pthread_join(p_thread[0], NULL);
  444.     }
  445.    
  446.  
  447.    
  448.     return 0;
  449. }
Advertisement
Add Comment
Please, Sign In to add comment