Advertisement
TheVarins

mutexy

Jan 18th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/msg.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <sys/shm.h>
  8. #include <time.h>
  9. #include <pthread.h>
  10.  
  11. #define THREADS 2
  12.  
  13. key_t key1;         // zmienne na klucze
  14. int   shmid;
  15. int *buffor= NULL;
  16. pthread_mutex_t  m1,m2,m3,m4;
  17. pthread_t p_thread[THREADS];
  18.  
  19. pid_t pid1, pid2;
  20.  
  21. void sig_handler (int sig){
  22.     pthread_mutex_unlock(&m1);
  23.     pthread_mutex_unlock(&m2);
  24.     pthread_mutex_unlock(&m3);
  25.     pthread_mutex_unlock(&m4);
  26.     pthread_mutex_destroy(&m1);
  27.     pthread_mutex_destroy(&m2);
  28.     pthread_mutex_destroy(&m3);
  29.     pthread_mutex_destroy(&m4);
  30.     kill(getpid(), SIGKILL);
  31. }
  32.  
  33. void *watek2(void *arg) {
  34.     int check=1;
  35.     srand(time(0));
  36.     while(check==1){
  37.         usleep(rand()%200);
  38.         pthread_mutex_lock(&m3);
  39.         printf("Konsument K1 odczytal liczbe %d\n",*buffor);
  40.         if(*buffor==100) check=0;
  41.         pthread_mutex_unlock(&m1);
  42.     }
  43.     pthread_exit((void*)1);
  44.  
  45. }
  46.  
  47. void *watek1(void *arg){
  48.     int check=1;
  49.     srand(time(0));
  50.     while(check==1){
  51.         usleep(rand()%200);
  52.         pthread_mutex_lock(&m4);
  53.         printf("Konsument K2 odczytal liczbe %d\n",*buffor);
  54.         if(*buffor==100) check=0;
  55.         pthread_mutex_unlock(&m2);
  56.     }
  57.     pthread_exit((void*)1);
  58. }
  59.  
  60. int main(void){
  61.     if ((key1 = ftok(".", 'A')) == -1)
  62.         errx(1, "Blad tworzenia klucza!");
  63.  
  64.     shmid = shmget(key1, sizeof(int), IPC_CREAT|0600);
  65.  
  66.     if (shmid == -1){
  67.         perror("Utworzenie segmentu pamieci wspoldzielonej");
  68.         exit(1);
  69.     }
  70.  
  71.     buffor = shmat(shmid, NULL, 0);
  72.  
  73.     if (buffor == NULL){
  74.         perror("Przylaczenie segmentu pamieci wspoldzielonej");
  75.         exit(1);
  76.     }
  77.     if(pthread_mutex_init(&m1, NULL)!=0){
  78.         printf("Blad przy tworzeniu mutex1! \n");
  79.         exit(1);
  80.     }
  81.     if(pthread_mutex_init(&m2, NULL)!=0){
  82.         printf("Blad przy tworzeniu mutex1! \n");
  83.         exit(1);
  84.     }
  85.     if(pthread_mutex_init(&m3, NULL)!=0){
  86.         printf("Blad przy tworzeniu mutex1! \n");
  87.         exit(1);
  88.     }
  89.     if(pthread_mutex_init(&m4, NULL)!=0){
  90.         printf("Blad przy tworzeniu mutex1! \n");
  91.         exit(1);
  92.     }
  93.     pthread_mutex_lock(&m3);
  94.     pthread_mutex_lock(&m4);
  95.  
  96.     int arg=1;
  97.     if(pthread_create(&p_thread[0], NULL, watek1, (void *)arg)){
  98.         printf("Blad przy tworzeniu watku1! \n");
  99.         exit(1);
  100.         }
  101.     if(pthread_create(&p_thread[1], NULL, watek2, (void *)arg)){
  102.         printf("Blad przy tworzeniu watku2! \n");
  103.         exit(1);
  104.         }
  105.  
  106.     int i=0;
  107.     while(i<=100){
  108.         signal(SIGINT, sig_handler);
  109.         pthread_mutex_lock(&m1);
  110.         pthread_mutex_lock(&m2);
  111.         *buffor = i;
  112.         printf("Producent zapisal liczbe %d\n",*buffor);
  113.         pthread_mutex_unlock(&m3);
  114.         pthread_mutex_unlock(&m4);
  115.         i++;
  116.     }
  117.     pthread_join(p_thread[0], NULL);
  118.     pthread_join(p_thread[1], NULL);
  119.     sleep(5);
  120.     kill(getpid(), SIGINT);
  121. return 1;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement