stuk06

final

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