Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- //tab1, tab2, tab3 -tablice z intami, uporz¹dkowana
- //w kazdej znajduje sie jedna, ta sama liczba x
- //znalezc na ktorej pozycji znajduje sie ten x
- int i,j,k = 0;//indeksy tablic
- //int tab1[] = {1,3,5,6,7,8};
- //int tab2[] = {0,2,4,5,17,18,19,20,21};
- //int tab3[] = {-1,5,52,53,56,57,58,61,62,63,64};
- int tab1[] = {7,9,10,12,23};
- int tab2[] = {1,3,5,7,10};
- int tab3[] = {1,2,3,4,5,6,7,8,9,10,11};
- pthread_mutex_t lock;
- void* f(void* a){
- while((tab1[i]!=tab2[j]) || (tab1[i]!=tab3[k])){
- pthread_mutex_lock(&lock);
- if(tab1[i]<tab2[j]) i++;
- printf("q1 -> ");
- pthread_mutex_unlock(&lock);
- }
- }
- void* g(void* a){
- while(tab1[i]!=tab2[j] || tab1[i]!=tab3[k]){
- pthread_mutex_lock(&lock);
- if(tab2[j]<tab3[k]){j++;}
- printf("p2 -> ");
- pthread_mutex_unlock(&lock);
- }
- }
- void* h(void* a){
- while(tab1[i]!=tab2[j] || tab1[i]!=tab3[k]){
- pthread_mutex_lock(&lock);
- if(tab3[k]<tab1[i]) k++;
- printf("r3 -> ");
- pthread_mutex_unlock(&lock);
- }
- }
- int main()
- {
- pthread_t w1, w2, w3;
- pthread_create(&w1, 0, f, 0);
- pthread_create(&w2, 0, g, 0);
- pthread_create(&w3, 0, h, 0);
- pthread_join(w1, NULL);
- pthread_join(w2, NULL);
- pthread_join(w3, NULL);
- printf("\nPozycja w tab1: %d, pozycja w tab2: %d, pozycja w tab3: %d", i, j, k);
- //fflush(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement