1 / 7

Fixation des Priorité des Taches

Fixation des Priorité des Taches. Pthread Ordonnancement. # define _MULTI_THREADED # include < pthread.h > # include < sched.h > # include < stdio.h > # include < semaphore.h > # include < string.h > # ifndef _CHECK_H # define _CHECK_H

tschill
Download Presentation

Fixation des Priorité des Taches

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fixation des Priorité des Taches Pthread Ordonnancement

  2. #define _MULTI_THREADED #include <pthread.h> #include <sched.h> #include <stdio.h> #include <semaphore.h> #include <string.h> #ifndef _CHECK_H #define _CHECK_H /* headers used by a majority of the example program */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> staticvoidcheckResults(char *string, intrc) { if (rc) { printf("Error on : %s, rc=%d« , string, rc); exit(EXIT_FAILURE); } return;} #endif //staticsem_tmy_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); voidshowSchedParam(pthread_attr_t *a) {intrc=0; structsched_param p; printf("Getschedulingparameters\n"); rc = pthread_attr_getschedparam(a, &p); printf("The thread attributesobjectindicatespriority: %d\n",p.sched_priority); return; } int main(intargc, char **argv) {pthread_t thread1,thread2 ; char *message1 = "Thread 1"; char *message2 = "Thread 2"; intrc=0; int iret1,iret2; pthread_attr_t attr1,attr2; structsched_param param1,param2; rc = pthread_attr_init(&attr1); rc = pthread_attr_init(&attr2); //sem_init (&my_sem, 0, 0); intpriority_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(&param1, 0, sizeof(structsched_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, &param1); rc=pthread_attr_getschedparam(&attr1, &param1); checkResults("pthread_attr_getschedparamparam ",rc); showSchedParam(&attr1); iret1=pthread_create(&thread1, &attr1,print_message_function,(void*)message1); checkResults("pthread_create thread 1\n", iret1);

  3. //************************** memset(&param2, 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, &param2); rc=pthread_attr_getschedparam(&attr2, &param2); checkResults("pthread_attr_getschedparam ",rc); //*************************** showSchedParam(&attr2); iret2=pthread_create(&thread2,&attr2,print_message_function,(void*) 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; }

  4. Mutex à condition variable

  5. 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

More Related