Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- #define MAX 20
- #define THREAD_MAIN 0
- #define THREAD_CONTROLLER 1
- int count = 0;
- pthread_mutex_t mutex;
- int scheduler;
- void *controlling(void *arg);
- int numero_primo(int num);
- int main()
- {
- printf("Benvenuto nel programma.\n");
- pthread_t controller;
- pthread_mutex_init(&mutex, NULL);
- pthread_create(&controller, NULL, &controlling, NULL);
- while(count < MAX)
- {
- if(scheduler == THREAD_MAIN)
- {
- pthread_mutex_lock(&mutex);
- count ++;
- pthread_mutex_unlock(&mutex);
- scheduler = THREAD_CONTROLLER;
- }
- }
- }
- void *controlling(void *arg)
- {
- while(count <= MAX)
- {
- if(scheduler == THREAD_CONTROLLER)
- {
- pthread_mutex_lock(&mutex);
- if(numero_primo(count))
- {
- printf("%d\n", count);
- fflush(stdout);
- }
- pthread_mutex_unlock(&mutex);
- scheduler = THREAD_MAIN;
- }
- }
- }
- int numero_primo(int num)
- {
- for(int i = 2; i < num; i++)
- if( (num % i) == 0)
- return 0;
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement