Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <ctype.h>
  6.  
  7. int position = 0;
  8.  
  9. int nbMinuscules = 0;
  10. int nbMajuscules = 0;
  11. int nbAutres = 0;
  12.  
  13. pthread_mutex_t mutexIndex=PTHREAD_MUTEX_INITIALIZER;
  14.  
  15. pthread_mutex_t mutexMinuscules=PTHREAD_MUTEX_INITIALIZER;
  16. pthread_mutex_t mutexMajuscules=PTHREAD_MUTEX_INITIALIZER;
  17. pthread_mutex_t mutexAutres=PTHREAD_MUTEX_INITIALIZER;
  18.  
  19.  
  20. void* compteMinuscules(void * ch){
  21.     char * texte = (char*) ch;
  22.     while(1){
  23.         pthread_mutex_lock(&mutexIndex);
  24.             if(texte[position]=='\0') {
  25.                 pthread_mutex_unlock(&mutexIndex);
  26.                 pthread_exit(NULL);
  27.             }
  28.             if(islower(texte[position])){
  29.                 // pthread_mutex_lock(&mutexMinuscules);
  30.                 nbMinuscules++;
  31.                 // pthread_mutex_unlock(&mutexMinuscules);
  32.                 position++;
  33.             }
  34.         pthread_mutex_unlock(&mutexIndex);
  35.     }
  36. }
  37.  
  38. void* compteMajuscules(void * ch){
  39.     char * texte = (char*) ch;
  40.     while(1){
  41.  
  42.         pthread_mutex_lock(&mutexIndex);
  43.             if(texte[position]=='\0') {
  44.                 pthread_mutex_unlock(&mutexIndex);
  45.                 pthread_exit(NULL);
  46.             }
  47.             if(isupper(texte[position])){
  48.                 // pthread_mutex_lock(&mutexMajuscules);
  49.                 nbMajuscules++;
  50.                 // pthread_mutex_unlock(&mutexMajuscules);
  51.                 position++;
  52.             }
  53.         pthread_mutex_unlock(&mutexIndex);
  54.     }
  55. }
  56.  
  57. void* compteAutres(void * ch){
  58.     char * texte = (char*) ch;
  59.     while(1){
  60.         pthread_mutex_lock(&mutexIndex);
  61.             if(texte[position]=='\0') {
  62.                 pthread_mutex_unlock(&mutexIndex);
  63.                 pthread_exit(NULL);
  64.             }
  65.             if(!islower(texte[position])&&!isupper(texte[position])){
  66.                 // pthread_mutex_lock(&mutexAutres);
  67.                 nbAutres++;
  68.                 // pthread_mutex_unlock(&mutexAutres);
  69.                 position++;
  70.             }
  71.         pthread_mutex_unlock(&mutexIndex);
  72.     }
  73. }
  74.  
  75. int main (void){
  76.  
  77.     char * texte="Lorem ipsum dolor sit amet, consectetur\
  78. adipiscing elit.  Proin neque sapien,\
  79. facilisis eget tincidunt ut, porttitor\
  80. laoreet lacus. Class aptent taciti\
  81. sociosqu ad litora torquent per conubia\
  82. nostra, per inceptos himenaeos.  Etiam\
  83. semper elementum congue.";
  84.  
  85.     pthread_t thr[3];
  86.  
  87.     if(pthread_create(thr, NULL, compteMinuscules, (void*) texte )){
  88.             fprintf(stderr, "Erreur dans pthread_create\n");
  89.         }
  90.     if(pthread_create(thr+1, NULL, compteMajuscules, (void*) texte )){
  91.             fprintf(stderr, "Erreur dans pthread_create\n");
  92.         }
  93.     if(pthread_create(thr+2, NULL, compteAutres, (void*) texte )){
  94.             fprintf(stderr, "Erreur dans pthread_create\n");
  95.         }
  96.  
  97.     pthread_join(thr[0], NULL);
  98.     pthread_join(thr[1], NULL);
  99.     pthread_join(thr[2], NULL);
  100.  
  101.  
  102.     printf("Nombre de minuscules : %d\n",nbMinuscules);
  103.  
  104.     printf("Nombre de majuscules : %d\n",nbMajuscules);
  105.  
  106.     printf("Nombre d'autres caracteres : %d\n",nbAutres);
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement