cs:posix:threads_example2
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/posix/threads_example2?do=diff
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | cs:posix:threads_example2 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Threads Example 2 ====== | ||
+ | **Concepts: | ||
+ | Example regarding threads synchronization. Voters (threads) express a vote, and the main collects the result of the vote | ||
+ | **Text:**\\ | ||
+ | Vedi commento ad inizio programma | ||
+ | |||
+ | **Solution: | ||
+ | <file C threads_example2.c> | ||
+ | /* | ||
+ | Realizzare un programma basato sui thread per la gestione di un' | ||
+ | |||
+ | In particolare: | ||
+ | - Il programma dovrà generare un thread per ogni votante (N_VOTANTI rappresenta il numero di votanti) | ||
+ | - Ogni thread: | ||
+ | * Impiegherà un tempo casuale tra 1 e 5 secondi per eseguire l' | ||
+ | * Deciderà al 50% se votare o non votare | ||
+ | * Nel caso decida di votare, verrà casualmente votato uno tra gli N_CANDIDATI possibili candidati | ||
+ | - Il risultato del voto dovrà essere salvato in un vettore globale di interi con nome voto | ||
+ | - Il thread, una volta ultimato, dovrà restituire il valore 1 | ||
+ | * In caso decida di non votare il thread dovrà restituire il valore 0 | ||
+ | * Un esempio di codice di thread deputato ad eseguire la votazione è il seguente: | ||
+ | |||
+ | static void *th_voter (void *arg) | ||
+ | { | ||
+ | int thread_num; | ||
+ | thread_num = * (int *)arg; | ||
+ | |||
+ | // Attende da 1 a 5 secondi prima di votare | ||
+ | sleep(rand()%5+1); | ||
+ | | ||
+ | // Con una probabilità del 50% non viene eseguito il voto | ||
+ | if (rand()%2) { // Operazione di voto | ||
+ | | ||
+ | voto[thread_num] = rand()%N_CANDIDATI+1; | ||
+ | printf(" | ||
+ | | ||
+ | pthread_exit((void *)1); | ||
+ | }else{ // Voto non eseguito | ||
+ | printf(" | ||
+ | pthread_exit((void *)0); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | * Quando tutti gli N_VOTANTI hanno finito l' | ||
+ | il thread principale dovrà stampare una statistica sul numero di voti ottenuti dai vari candidati | ||
+ | |||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | #define N_VOTANTI 5 | ||
+ | #define N_CANDIDATI 3 | ||
+ | |||
+ | |||
+ | int voto[N_VOTANTI]; | ||
+ | |||
+ | |||
+ | static void *th_voter (void *arg) | ||
+ | { | ||
+ | int thread_num; | ||
+ | thread_num = * (int *)arg; | ||
+ | |||
+ | /* Attende da 1 a 5 secondi prima di votare */ | ||
+ | sleep(rand()%5+1); | ||
+ | | ||
+ | /* Con una probabilità del 50% non viene eseguito il voto */ | ||
+ | if (rand()%2) { /* Operazione di voto */ | ||
+ | | ||
+ | voto[thread_num] = rand()%N_CANDIDATI+1; | ||
+ | printf(" | ||
+ | | ||
+ | pthread_exit((void *)1); | ||
+ | }else{ /* Voto non eseguito */ | ||
+ | printf(" | ||
+ | pthread_exit((void *)0); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | int main (void) | ||
+ | { | ||
+ | int retcode; | ||
+ | pthread_t th[N_VOTANTI]; | ||
+ | void *retval; | ||
+ | int th_parameters[N_VOTANTI]; | ||
+ | int i, j; | ||
+ | | ||
+ | printf(" | ||
+ | for(i=0; i< | ||
+ | th_parameters[i] = i; | ||
+ | retcode = pthread_create (& | ||
+ | if (retcode != 0) | ||
+ | fprintf (stderr, " | ||
+ | } | ||
+ | |||
+ | |||
+ | /* Attesa della terminazione di tutti i thread */ | ||
+ | for(i=0; i< | ||
+ | retcode = pthread_join (th[i], & | ||
+ | if (retcode != 0) | ||
+ | fprintf (stderr, " | ||
+ | if (retval==0) | ||
+ | voto[i] = -1; /* Inserisco -1 nel caso di assenza di voto */ | ||
+ | } | ||
+ | /* gcc 5-voters.c -lpthread */ | ||
+ | |||
+ | printf(" | ||
+ | for(i=0; i< | ||
+ | int n_voti = 0; | ||
+ | for(j=0; j< | ||
+ | if(voto[j]==i+1 && voto[j]!=-1) | ||
+ | n_voti++; | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Comments: |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/posix/threads_example2?do=diff
/web/htdocs/www.skenz.it/home/data/pages/cs/posix/threads_example2.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1