Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <sched.h>
- #define _GNU_SOURCE
- void *tarefa(void *id){
- long int tid = (long int )id;
- int i = 1;
- while(i<=5){
- printf("Sou a thread %ld e estou no numero %d .\n", tid, i);
- i++;
- sched_yield();
- }
- }
- int main(){
- cpu_set_t mascaranucleos; // Bitmask para os n´ucleos
- CPU_ZERO(&mascaranucleos);// Remove a selec¸˜ao de todos os n´ucleos
- CPU_SET(0, &mascaranucleos); // Marca somente um n´ucleo 0 para uso
- sched_setaffinity(0, sizeof(cpu_set_t), &mascaranucleos);
- const int NUMTHREADS = 10;
- pthread_t threads[NUMTHREADS];
- for (long int i = 0; i < NUMTHREADS; i++) {
- pthread_create(&threads[i], NULL, tarefa, (void *) i);
- }
- for (long int i = 0; i < NUMTHREADS; i++) {
- pthread_join(threads[i], NULL);
- }
- return 0;
- }
- -----
- #include <stdio.h>
- #include <pthread.h>
- void *tarefa(void *id){
- long int tid = (long int )id;
- int i = 1;
- while(i<=5){
- printf("Sou a thread %ld e estou no numero %d .\n", tid, i);
- i++;
- }
- }
- int main(){
- const int NUMTHREADS = 10;
- pthread_t threads[NUMTHREADS];
- for (long int i = 0; i < NUMTHREADS; i++) {
- pthread_create(&threads[i], NULL, tarefa, (void *) i);
- }
- for (long int i = 0; i < NUMTHREADS; i++) {
- pthread_join(threads[i], NULL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement