Zaibon

Untitled

Jan 26th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9.  
  10.  
  11. typedef struct{
  12.     char filename[128];
  13.     char target[25];
  14.     int idThread;
  15. } Param_thread;
  16.  
  17.  
  18. //prototype
  19. void *fctThread(void* param);
  20. pthread_t thread_handle[4];
  21.  
  22.  
  23. int main(int argc, char** argv)
  24. {
  25.         int i=0, *retThread[4];
  26.         int ret[4];
  27.         Param_thread param[4];
  28.  
  29.         //initionalisation param
  30.         strcpy(param[0].filename,"lorem.txt");
  31.         strcpy(param[0].target,"lor");
  32.         param[0].idThread = i;
  33.  
  34.         strcpy(param[1].filename,"lorem.txt");
  35.         strcpy(param[1].target,"lor");
  36.         param[1].idThread = 1;
  37.  
  38.         strcpy(param[2].filename,"lorem.txt");
  39.         strcpy(param[2].target,"lor");
  40.         param[2].idThread = 2;
  41.  
  42.         strcpy(param[3].filename,"lorem.txt");
  43.         strcpy(param[3].target,"lor");
  44.         param[3].idThread = 3;
  45.        
  46.  
  47.  
  48.         printf("Lancement des thread secondaires\n");
  49.         for ( i = 0; i < 4; ++i)
  50.         {
  51.             printf("lancement thread %d\n",i);
  52.             ret[i] = pthread_create(&thread_handle[i], NULL, (void*(*)(void*))fctThread, (void*)&param[i]);
  53.         }
  54.  
  55.         for ( i = 0; i < 4; ++i)
  56.         {
  57.             printf("join thread %d\n",i);
  58.             ret[i] = pthread_join(thread_handle[i], (void**) &retThread[i]);
  59.             printf("valeur renvoyée par le thread %d : %d\n",i,*retThread[i]);
  60.         }
  61.  
  62.         printf("fin principale\n");
  63. }
  64.  
  65.  
  66. void* fctThread(void *param){
  67.     int nbrOcc = 0,nbr_read=1;
  68.     int hd,i=0,j=0;
  69.     Param_thread *arg = (Param_thread*) malloc(sizeof(Param_thread));
  70.     arg = (Param_thread*) param;
  71.     char output[50];
  72.  
  73.     printf("filename : %s\n",arg->filename);
  74.     printf("cible : %s\n",arg->target);
  75.     printf("idThread : %d\n",arg->idThread);
  76.  
  77.     char buffer[4];
  78.  
  79.     while(nbr_read>0){
  80.         if( (hd = open(arg->filename,O_RDONLY)) == -1 ) {
  81.             perror("erreur ouverture fichier");
  82.             exit(0);
  83.         }
  84.  
  85.         sprintf(output, "%%%ds\n",arg->idThread*5);
  86.         printf(output,"*");
  87.  
  88.  
  89.         if( (lseek(hd,i,SEEK_SET)) == -1 ){
  90.             perror("erreur lseek");
  91.             exit(0);
  92.         }
  93.  
  94.         if ( (nbr_read = read(hd,buffer,3)) < 0){
  95.             printf("fin de fichier\n");
  96.             break;
  97.         }
  98.  
  99.         if( (close(hd)) == -1){
  100.             perror("erreur close");
  101.         }
  102.         // printf("%s-%s\n",buffer,arg->target);
  103.         if(strcmp(buffer,arg->target) == 0){
  104.             nbrOcc++;
  105.         }
  106.        
  107.         i++;
  108.     }
  109.  
  110.     free(arg);
  111.     pthread_exit(&nbrOcc);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment