├── README.md ├── .gitattributes ├── .vscode └── settings.json ├── lesson-1 ├── index.c └── exer_c └── lesson_2 └── exer-2 /README.md: -------------------------------------------------------------------------------- 1 | # parallel-programming 2 | 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "ATV3-1.C": "cpp", 4 | "ATV3-2.C": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /lesson-1/index.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define NUM_THREADS 5 9 | 10 | // Criando uma STRUCT 11 | struct dados_para_thread{ 12 | int thread_id; 13 | int thread_rn; 14 | }; 15 | 16 | void *print_hello(void *threadid){ 17 | struct dados_para_thread *dadosthread; 18 | dadosthread= (struct dados_para_thread *)threadid; 19 | printf("No principal: criando a thread - id: #%d\t Random Number: %d\n\n", dadosthread->thread_id,dadosthread->thread_rn);//In main: creating thread 20 | //printf("Hello World! It's me, thread #%ld!\n", tid); 21 | pthread_exit((void *)NULL); 22 | }; 23 | 24 | 25 | int main(int argc, char *argv[]){ 26 | system("cls||clear"); 27 | pthread_t threads[NUM_THREADS]; 28 | struct dados_para_thread dados_para_thread_array[NUM_THREADS]; 29 | 30 | int error_code; 31 | long t,res; 32 | 33 | for(t = 0;t < NUM_THREADS; t++){ 34 | dados_para_thread_array[t].thread_id = t; 35 | dados_para_thread_array[t].thread_rn = rand() % 100; 36 | error_code = pthread_create(&threads[t], NULL, 37 | print_hello, (void *) &dados_para_thread_array[t]); 38 | 39 | if (error_code){ 40 | printf("ERROR; return code from pthread_create() is %d\n", error_code); 41 | exit(-1); 42 | }; 43 | res=pthread_join(threads[t], NULL); 44 | }; 45 | 46 | //printf("id: %d\n", res); 47 | printf("Hello World! Sou eu, a thread #%ld!\n", res); 48 | pthread_exit(NULL); 49 | }; 50 | -------------------------------------------------------------------------------- /lesson_2/exer-2: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define NUM_THREADS 5 9 | 10 | // Criando uma STRUCT 11 | struct dados_para_thread{ 12 | int thread_id; 13 | int thread_rn; 14 | }; 15 | 16 | void *print_hello(void *threadid){ 17 | struct dados_para_thread *dadosthread; 18 | dadosthread= (struct dados_para_thread *)threadid; 19 | printf("No principal: criando a thread - id: #%d\t Random Number: %d\n\n", dadosthread->thread_id,dadosthread->thread_rn);//In main: creating thread 20 | //printf("Hello World! It's me, thread #%ld!\n", tid); 21 | pthread_exit((void *)NULL); 22 | }; 23 | 24 | 25 | int main(int argc, char *argv[]){ 26 | system("cls||clear"); 27 | pthread_t threads[NUM_THREADS]; 28 | struct dados_para_thread dados_para_thread_array[NUM_THREADS]; 29 | 30 | int error_code; 31 | long t,res; 32 | 33 | for(t = 0;t < NUM_THREADS; t++){ 34 | dados_para_thread_array[t].thread_id = t; 35 | dados_para_thread_array[t].thread_rn = rand() % 100; 36 | error_code = pthread_create(&threads[t], NULL, 37 | print_hello, (void *) &dados_para_thread_array[t]); 38 | 39 | if (error_code){ 40 | printf("ERROR; return code from pthread_create() is %d\n", error_code); 41 | exit(-1); 42 | }; 43 | res=pthread_join(threads[t], NULL); 44 | }; 45 | 46 | //printf("id: %d\n", res); 47 | printf("Hello World! Sou eu, a thread #%ld!\n", res); 48 | pthread_exit(NULL); 49 | }; 50 | -------------------------------------------------------------------------------- /lesson-1/exer_c: -------------------------------------------------------------------------------- 1 | //NATHALY LORENA SANTOS ROCHA 2 | //MATRICULA: 201833840058 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define NUM_THREADS 8 11 | 12 | // Criando uma STRUCT 13 | struct dados_para_thread{ 14 | int thread_id; 15 | int thread_rn; 16 | int multp; 17 | }; 18 | 19 | struct dados_para_thread dados_para_thread_array[NUM_THREADS]; 20 | 21 | 22 | void *print_hello(void *threadarg){ 23 | int taskid, mult; 24 | struct dados_para_thread *dadosthread; 25 | 26 | sleep(1); 27 | 28 | dadosthread = (struct dados_para_thread *) threadarg; 29 | taskid = dadosthread->thread_id; 30 | mult = dadosthread->thread_rn; 31 | 32 | intptr_t *resul = (intptr_t*) malloc(sizeof(intptr_t)); 33 | *resul = taskid * mult; 34 | printf("No principal: criando a thread - id: #%d\t X %d = %d\n\n", dadosthread->thread_id,dadosthread->thread_rn, *resul);//In main: creating thread 35 | //printf("Hello World! It's me, thread #%ld!\n", tid); 36 | dadosthread-> multp = *resul; 37 | pthread_exit((void *)resul); //retorna o resultado para a thread principal 38 | }; 39 | 40 | 41 | int main(int argc, char *argv[]){ 42 | system("cls||clear"); 43 | pthread_t threads[NUM_THREADS]; 44 | 45 | 46 | int error_code, *resultado,random; 47 | long t,res; 48 | 49 | for(t = 0;t < NUM_THREADS; t++){ 50 | dados_para_thread_array[t].thread_id = t; 51 | dados_para_thread_array[t].thread_rn = rand() % 100; 52 | error_code = pthread_create(&threads[t], NULL, 53 | print_hello, (void *) &dados_para_thread_array[t]); 54 | 55 | if (error_code){ 56 | printf("ERROR; return code from pthread_create() is %d\n", error_code); 57 | exit(-1); 58 | } 59 | 60 | } 61 | for(t = 0;t < NUM_THREADS; t++){ 62 | pthread_join(threads[t], (void **)&resultado); 63 | printf("Thread id: %d retorna o valor de: %d\n",dados_para_thread_array[t].thread_id,*resultado); 64 | if (*resultado != (t * dados_para_thread_array[t].thread_rn)){ 65 | printf("ERROR: Result for thread %d is incorrect\n", t); 66 | 67 | }; 68 | } 69 | 70 | for(t = 0; t < NUM_THREADS; t++){ 71 | int resultado = dados_para_thread_array[t].multp; 72 | printf("Thread id: %d Struct mult:%d\n", dados_para_thread_array[t].thread_id, resultado); 73 | if (resultado != (t * dados_para_thread_array[t].thread_rn)){ 74 | printf("ERROR: Result for thread %d is incorrect\n", t); 75 | 76 | } 77 | } 78 | pthread_exit(NULL); 79 | return 0; 80 | } 81 | --------------------------------------------------------------------------------