Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. //tab1, tab2, tab3 -tablice z intami, uporz¹dkowana
  6. //w kazdej znajduje sie jedna, ta sama liczba x
  7. //znalezc na ktorej pozycji znajduje sie ten x
  8.  
  9. int i,j,k = 0;//indeksy tablic
  10. //int tab1[] = {1,3,5,6,7,8};
  11. //int tab2[] = {0,2,4,5,17,18,19,20,21};
  12. //int tab3[] = {-1,5,52,53,56,57,58,61,62,63,64};
  13.  
  14.  
  15. int tab1[] = {7,9,10,12,23};
  16. int tab2[] = {1,3,5,7,10};
  17. int tab3[] = {1,2,3,4,5,6,7,8,9,10,11};
  18.  
  19. pthread_mutex_t lock;
  20.  
  21.  
  22. void* f(void* a){
  23.     while((tab1[i]!=tab2[j]) || (tab1[i]!=tab3[k])){
  24.         pthread_mutex_lock(&lock);
  25.         if(tab1[i]<tab2[j]) i++;
  26.         printf("q1 -> ");
  27.         pthread_mutex_unlock(&lock);
  28.  
  29.     }
  30. }
  31.  
  32. void* g(void* a){
  33.     while(tab1[i]!=tab2[j] || tab1[i]!=tab3[k]){
  34.         pthread_mutex_lock(&lock);
  35.         if(tab2[j]<tab3[k]){j++;}
  36.         printf("p2 -> ");
  37.         pthread_mutex_unlock(&lock);
  38.     }
  39. }
  40.  
  41. void* h(void* a){
  42.     while(tab1[i]!=tab2[j] || tab1[i]!=tab3[k]){
  43.         pthread_mutex_lock(&lock);
  44.         if(tab3[k]<tab1[i]) k++;
  45.         printf("r3 -> ");
  46.         pthread_mutex_unlock(&lock);
  47.     }
  48. }
  49. int main()
  50. {
  51.  
  52.     pthread_t w1, w2, w3;
  53.  
  54.  
  55.     pthread_create(&w1, 0, f, 0);
  56.     pthread_create(&w2, 0, g, 0);
  57.     pthread_create(&w3, 0, h, 0);
  58.  
  59.     pthread_join(w1, NULL);
  60.     pthread_join(w2, NULL);
  61.     pthread_join(w3, NULL);
  62.  
  63.  
  64.  
  65.     printf("\nPozycja w tab1: %d, pozycja w tab2: %d, pozycja w tab3: %d", i, j, k);
  66.     //fflush(stdout);
  67.  
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement