Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <sched.h>
  4. #define _GNU_SOURCE
  5.  
  6. void *tarefa(void *id){
  7.     long int tid = (long int )id;
  8.     int i = 1;
  9.     while(i<=5){
  10.         printf("Sou a thread %ld e estou no numero %d .\n", tid, i);
  11.         i++;
  12.         sched_yield();
  13.     }
  14. }
  15. int main(){
  16.     cpu_set_t mascaranucleos; // Bitmask para os n´ucleos
  17.     CPU_ZERO(&mascaranucleos);// Remove a selec¸˜ao de todos os n´ucleos
  18.     CPU_SET(0, &mascaranucleos); // Marca somente um n´ucleo 0 para uso
  19.     sched_setaffinity(0, sizeof(cpu_set_t), &mascaranucleos);
  20.  
  21.     const int NUMTHREADS = 10;
  22.     pthread_t threads[NUMTHREADS];
  23.  
  24.     for (long int i = 0; i < NUMTHREADS; i++) {
  25.         pthread_create(&threads[i], NULL, tarefa, (void *) i);
  26.     }
  27.     for (long int i = 0; i < NUMTHREADS; i++) {
  28.         pthread_join(threads[i], NULL);
  29.     }
  30.     return 0;
  31. }
  32.  
  33. -----
  34. #include <stdio.h>
  35. #include <pthread.h>
  36.  
  37. void *tarefa(void *id){
  38.     long int tid = (long int )id;
  39.     int i = 1;
  40.     while(i<=5){
  41.         printf("Sou a thread %ld e estou no numero %d .\n", tid, i);
  42.         i++;
  43.     }
  44. }
  45. int main(){
  46.     const int NUMTHREADS = 10;
  47.     pthread_t threads[NUMTHREADS];
  48.  
  49.     for (long int i = 0; i < NUMTHREADS; i++) {
  50.         pthread_create(&threads[i], NULL, tarefa, (void *) i);
  51.     }
  52.     for (long int i = 0; i < NUMTHREADS; i++) {
  53.         pthread_join(threads[i], NULL);
  54.     }
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement