Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <math.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #define ILOSC 1
- #define liczba_watkow 3
- #define wartosc_semafora 1
- float t[liczba_watkow];
- sem_t semafor;
- void* p (void* k) { // funkcja watku (watek)
- long nr=(long)k;
- int l,i;
- for (i=0;i<ILOSC;i++) {
- l = rand()%10;
- sem_wait(&semafor);
- t[nr-1] = l;
- sem_post(&semafor);
- printf("watek %ld wylosowal %d \n",nr,l);fflush(stdout);
- }
- return 0;
- }
- int main(int arg, char **argv) {
- sem_init(&semafor, 0, wartosc_semafora);
- pthread_t w[liczba_watkow];
- long i=0;
- float suma;
- srand(time(0));
- for (i=1;i<=liczba_watkow;i++)
- pthread_create(&w[i-1],0,p,(void*)i);
- for (i=0;i<liczba_watkow;i++)
- pthread_join(w[i],0);
- // oblicza wartości srednie
- for (i=0;i<ILOSC;i++) {
- suma=0;
- for (i=0;i<liczba_watkow;i++) suma+= t[i];
- printf("\nsrednia = %f\n",suma/liczba_watkow);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement