Pthread Ordonnancement
#define _MULTI_THREADED #include #ifndef _CHECK_H #define _CHECK_H /* headers used by a majority of the example program */ #include static void checkResults(char *string, int rc) { if (rc) { printf("Error on : %s, rc=%d«, string, rc); exit(EXIT_FAILURE); } return;} #endif //static sem_t my_sem; int count1 = 0; int count2 = 0; int times = 1000; void *print_message_function(void *ptr); void *thread1_process (void * arg); void *thread2_process (void * arg); void showSchedParam(pthread_attr_t *a) {int rc=0; struct sched_param p; printf("Get scheduling parameters\n"); rc = pthread_attr_getschedparam(a, &p); printf("The thread attributes object indicates priority: %d\n",p.sched_priority); return; } int main(int argc, char **argv) {pthread_t thread1,thread2 ; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int rc=0; int iret1,iret2; pthread_attr_t attr1,attr2; struct sched_param param1,param2; rc = pthread_attr_init(&attr1); rc = pthread_attr_init(&attr2); //sem_init (&my_sem, 0, 0); int priority_max,priority_min; priority_min = sched_get_priority_min(SCHED_RR); printf("param.sched_priority max=%d\n",priority_min); priority_max = sched_get_priority_max(SCHED_RR); printf("param.sched_priority max=%d\n",priority_max); //************************ memset(¶m1, 0, sizeof(struct sched_param)); pthread_attr_setschedpolicy(&attr1, SCHED_RR); rc = pthread_attr_setinheritsched(&attr1, PTHREAD_EXPLICIT_SCHED); if (rc!=0) {printf("pthread_attr_setinheritsched() PB!!");} checkResults("pthread_attr_getinheritsched()\n", rc); param1.sched_priority = 14; pthread_attr_setschedparam(&attr1, ¶m1); rc=pthread_attr_getschedparam(&attr1, ¶m1); checkResults("pthread_attr_getschedparam param ",rc); showSchedParam(&attr1); iret1=pthread_create(&thread1, &attr1,print_message_function,(void*)message1); checkResults("pthread_create thread 1\n", iret1);
//************************** memset(¶m2, 0, sizeof(struct sched_param)); pthread_attr_setschedpolicy(&attr2, SCHED_RR); rc = pthread_attr_setinheritsched(&attr2, PTHREAD_EXPLICIT_SCHED); checkResults("pthread_attr_getinheritsched()\n", rc); param2.sched_priority = 0; pthread_attr_setschedparam(&attr2, ¶m2); rc=pthread_attr_getschedparam(&attr2, ¶m2); checkResults("pthread_attr_getschedparam ",rc); //*************************** showSchedParam(&attr2); iret2=pthread_create(&thread2,&attr2,print_message_function,(v oid*) message2); checkResults("pthread_create thread 1\n", iret2); pthread_join(thread1, NULL); pthread_join(thread2, NULL); printf("count1 = %d, count2 = %d\n", count1, count2); printf("Thread 1 returns: %d\n", iret1); printf("Thread 2 returns: %d\n", iret2); return 0; } void *print_message_function(void *ptr) { char *message; message = (char *) ptr; while (times > 0) { //printf("%s \n", message); int i = 0; for (i = 0; i < 20000; i++) i++; // only for delay if (strcmp(message, "Thread 1") == 0) {count1 += 1;printf("Thread 1 %d\n",count1);} else {count2 += 1;printf("Thread 2 %d\n",count1); } times--; } return NULL; }
Mutex à condition variable
Main Thread - Déclaration et initialisation des données globales et des variables qui nécessitent une synchronisation (comme la variable "count") - Déclarez et initialisez objet condition variable - Déclarer et initialiser un mutex associé - Créer des Thread A et B pour effectuer le travail Thread A Exécution des instructions jusqu'au point où une certaine condition doit se produire (comme "count" doit atteindre une valeur spécifiée) Verrouillage le mutex associé et vérifier la valeur de la variable globale Appel pthread_cond_wait () pour effectuer le blocage en attente du signal provenant the Thread B. Il faut noter qu'un appel à pthread_cond_wait () permet de déverrouille automatiquement et de façon atomique la variable mutex associé de sorte qu'il peut être utilisé par le Thread B. A la réception du signal du réveil, le mutex est verrouillé automatiquement et atomiquement. déverrouillage Explicit du mutex Continuer Thread B Continuer à executer les instructions Verrouillage du mutex associé Changez la valeur de la variable globale que Thread-A est en attente. Vérifier la valeur de la variable global que Thread-A est en attente. Si elle remplit la condition fixée, il faut le signaler à laThread-A. Déverrouiller le mutex. Continuer Main Thread Join / Continue
void * watch_count (void *t) { long my_id = (long)t; printf("Starting watch_count(): thread %ld\n", my_id); /* Lock mutex and wait for signal. Note that the pthread_cond_wait routine will automatically and atomically unlock mutex while it waits. Also, note that if COUNT_LIMIT is reached before this routine is run by the waiting thread, the loop will be skipped to prevent pthread_cond_wait from never returning. */ pthread_mutex_lock(&count_mutex); if (count < COUNT_LIMIT) { printf("watch_count(): thread %ld going into wait...\n", my_id); pthread_cond_wait(&count_threshold_cv, &count_mutex); printf("watch_count(): thread %ld Condition signal received.\n", my_id); count += 125; printf("watch_count(): thread %ld count now = %d.\n", my_id, count); } pthread_mutex_unlock(&count_mutex); pthread_exit(NULL);} void * inc_count (void *t) { int i; long my_id = (long)t; for (i=0; i < TCOUNT; i++) { pthread_mutex_lock(&count_mutex); count++; /* Check the value of count and signal waiting thread when condition is reached. Note that this occurs while mutex is locked. */ if (count == COUNT_LIMIT) { printf("inc_count(): thread %ld, count = %d Threshold reached. ",my_id, count); pthread_cond_signal(&count_threshold_cv); printf("Just sent signal.\n"); } printf("inc_count(): thread %ld, count = %d, unlocking mutex\n", my_id, count); pthread_mutex_unlock(&count_mutex); /* Do some work so threads can alternate on mutex lock */ sleep(1); } pthread_exit(NULL);}
Starting watch_count(): thread 1 watch_count(): thread 1 going into wait... inc_count(): thread 2, count = 1, unlocking mutex inc_count(): thread 3, count = 2, unlocking mutex inc_count(): thread 2, count = 3, unlocking mutex inc_count(): thread 3, count = 4, unlocking mutex inc_count(): thread 3, count = 5, unlocking mutex inc_count(): thread 2, count = 6, unlocking mutex inc_count(): thread 3, count = 7, unlocking mutex inc_count(): thread 2, count = 8, unlocking mutex inc_count(): thread 3, count = 9, unlocking mutex inc_count(): thread 2, count = 10, unlocking mutex inc_count(): thread 3, count = 11, unlocking mutex inc_count(): thread 2, count = 12 Threshold reached. Just sent signal. inc_count(): thread 2, count = 12, unlocking mutex watch_count(): thread 1 Condition signal received. watch_count(): thread 1 count now = 137. inc_count(): thread 3, count = 138, unlocking mutex inc_count(): thread 2, count = 139, unlocking mutex inc_count(): thread 3, count = 140, unlocking mutex inc_count(): thread 2, count = 141, unlocking mutex inc_count(): thread 3, count = 142, unlocking mutex inc_count(): thread 2, count = 143, unlocking mutex inc_count(): thread 3, count = 144, unlocking mutex inc_count(): thread 2, count = 145, unlocking mutex Main(): Waited on 3 threads. Final value of count = 145. Done.