Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <wait.h>
- #include <time.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <semaphore.h>
- #include <pthread.h>
- #define numberArray 300
- void * Tgenerate(void *arg);
- void * TCalculate(void *arg);
- void * TInc(void *arg);
- void * TDnc(void *arg);
- int novosElementos;
- int posLeitura;
- int numPos;
- int numNeg;
- int helpPOS;
- int helpNEG;
- struct Prova{
- int number;
- int notaG1;
- int notaG2;
- int notaG3;
- int notaFinal;
- };
- pthread_mutex_t LerArray;
- pthread_mutex_t CalculateControl;
- pthread_mutex_t Increment;
- pthread_mutex_t IncrementLOCK;
- pthread_mutex_t Decrement;
- pthread_mutex_t DecrementLOCK;
- pthread_cond_t condInit;
- pthread_cond_t condINC;
- pthread_cond_t condDEC;
- int flag;
- int flagTerm;
- int main(){
- numNeg=0;
- numPos=0;
- flag=0;
- posLeitura=0;
- pthread_mutex_init(&LerArray,NULL);
- pthread_mutex_init(&CalculateControl,NULL);
- pthread_mutex_init(&Increment,0);
- pthread_mutex_init(&IncrementLOCK,0);
- pthread_mutex_init(&Decrement,0);
- pthread_mutex_init(&DecrementLOCK,0);
- pthread_cond_init(&condInit,NULL);
- pthread_cond_init(&condINC,NULL);
- pthread_cond_init(&condDEC,NULL);
- struct Prova array[ numberArray];
- pthread_t threads[5];
- pthread_create(&threads[3],NULL,TInc,NULL);
- pthread_create(&threads[4],NULL,TDnc,NULL);
- sleep(0.1);
- pthread_create(&threads[0],NULL,Tgenerate,(void *)array);
- pthread_create(&threads[1],NULL,TCalculate,(void *)array);
- pthread_create(&threads[2],NULL,TCalculate,(void *)array);
- int i;
- sleep(1);
- pthread_cancel(threads[3]);
- pthread_cancel(threads[4]);
- for ( i = 0; i < 5; i++)
- {
- pthread_join(threads[i],NULL);
- }
- printf("NUM POS : %d \nNUM NEG : %d\n",numPos,numNeg);
- pthread_mutex_destroy(&LerArray);
- pthread_mutex_destroy(&Increment);
- pthread_mutex_destroy(&Decrement);
- pthread_mutex_destroy(&CalculateControl);
- pthread_cond_destroy(&condInit);
- return 0;
- }
- void * Tgenerate(void *arg){
- time_t t;
- /* intializes RNG (srand():stdlib.h; time(): time.h) */
- srand ((unsigned) time (&t));
- struct Prova * array=(struct Prova *) arg;
- int i;
- for ( i = 0; i < numberArray; i++)
- {
- pthread_mutex_lock(&LerArray);
- struct Prova p;
- p.notaG1=rand() %101;
- p.notaG2=rand() %101;
- p.notaG3=rand() %101;
- *(array+i)=p;
- novosElementos++;
- pthread_cond_broadcast(&condInit);
- pthread_mutex_unlock(&LerArray);
- }
- while (posLeitura!=numberArray);
- pthread_mutex_lock(&CalculateControl);
- flag=1;
- pthread_mutex_unlock(&Increment);
- pthread_mutex_unlock(&Decrement);
- pthread_cond_broadcast(&condInit);
- pthread_mutex_unlock(&LerArray);
- pthread_exit(NULL);
- }
- void * TCalculate(void * arg){
- struct Prova * array=(struct Prova *) arg;
- while (posLeitura!= numberArray)
- {
- pthread_mutex_lock(&LerArray);
- while (novosElementos==0&&flag==0)
- {
- pthread_cond_wait(&condInit,&LerArray);
- }
- if (flag==1)
- {
- pthread_mutex_unlock(&LerArray);
- pthread_exit(NULL);
- }
- ((array[posLeitura])).notaFinal=(array[posLeitura].notaG1+array[posLeitura].notaG2+array[posLeitura].notaG3)/3;
- // printf("TESTE2.3 : %d\n",((array[posLeitura])).notaFinal);
- if (array[posLeitura].notaFinal>50)
- {
- pthread_mutex_lock(&Increment);
- pthread_cond_signal(&condINC);
- printf("HELP2\n");
- helpPOS++;
- pthread_mutex_unlock(&Increment);
- }else
- {
- pthread_mutex_lock(&Decrement);
- pthread_cond_signal(&condDEC);
- printf("HELP1\n");
- helpNEG++;
- pthread_mutex_unlock(&Decrement);
- }
- novosElementos--;
- posLeitura++;
- pthread_mutex_unlock(&LerArray);
- }
- //pthread_mutex_unlock(&Increment);
- //pthread_mutex_unlock(&Decrement);
- pthread_mutex_lock(&LerArray);
- pthread_mutex_unlock(&CalculateControl);
- pthread_mutex_unlock(&LerArray);
- pthread_exit(NULL);
- }
- void * TInc(void *arg){
- while (posLeitura!= numberArray)
- {
- pthread_mutex_lock(&Increment);
- while (helpPOS==0){
- pthread_cond_wait(&condINC,&Increment);
- if (flag==1&&helpPOS==0)
- {
- pthread_exit(NULL);
- }
- }
- int i;
- for ( i = 0; i < helpPOS; i++)
- {
- printf("MAIS\n");
- numPos++;
- }
- helpPOS=0;
- pthread_mutex_unlock(&Increment);
- }
- pthread_exit(NULL);
- }
- void * TDnc(void *arg){
- while (posLeitura!= numberArray)
- {
- pthread_mutex_lock(&Decrement);
- while (helpNEG==0)
- {
- pthread_cond_wait(&condDEC,&Decrement);
- if (flag==1&&helpNEG==0)
- {
- pthread_exit(NULL);
- }
- }
- int i;
- for ( i = 0; i < helpNEG; i++)
- {
- printf("MENOS\n");
- numNeg++;
- }
- helpNEG=0;
- pthread_mutex_unlock(&Decrement);
- }
- pthread_exit(NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement