Created
September 6, 2022 12:41
-
-
Save amaral220x/ef280fc8707f363f1af3130bd172767e to your computer and use it in GitHub Desktop.
Lab 1 Programação Concorrente
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Gabriel Amaral DRE: 121069963 | |
| #define TAM 10000 | |
| #define NTHREADS 2 | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <pthread.h> | |
| double vetor[TAM], copia[TAM]; | |
| void *incrementa(void *arg){ | |
| int inicio = * (int*) arg; | |
| for(int i=0;i<TAM/2;i++){ | |
| vetor[inicio+i] *= 1.1; | |
| } | |
| pthread_exit(NULL); | |
| } | |
| int compara(){ | |
| for(int i=0; i< TAM; i++){ | |
| if(vetor[i]!=copia[i]*1.1){ | |
| return 0; | |
| } | |
| } | |
| return 1; | |
| } | |
| int main(void){ | |
| pthread_t tid_sistema[NTHREADS]; | |
| int thread; | |
| int tid_local[NTHREADS]; | |
| for(int i = 0; i < TAM; i++){ | |
| vetor[i] = 1+i; | |
| copia[i] = vetor[i]; | |
| } | |
| for(thread=0; thread<NTHREADS; thread++) { | |
| printf("--Cria a thread %d\n", thread); | |
| if(thread == 0){ | |
| tid_local[thread] = 0; | |
| }else{ | |
| tid_local[thread] = TAM/2; | |
| } | |
| if (pthread_create(&tid_sistema[thread], NULL, incrementa, (void*) &tid_local[thread])) { | |
| printf("--ERRO: pthread_create()\n"); exit(-1); | |
| } | |
| } | |
| for (thread=0; thread<NTHREADS; thread++) { | |
| if (pthread_join(tid_sistema[thread], NULL)) { | |
| printf("--ERRO: pthread_join() \n"); exit(-1); | |
| } | |
| } | |
| printf("Vetor final: "); | |
| for(int i = 0; i < 10; i++){ | |
| printf("%f ",vetor[i]); | |
| } | |
| printf("E continua...\n"); | |
| printf("Realizando o teste\n"); | |
| if(!compara()){ | |
| printf("Erro encontrado nos calculos!\n"); | |
| } | |
| printf("Teste realizado!\nTudo correto\n"); | |
| printf("--Thread principal terminou\n"); | |
| pthread_exit(NULL); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment