Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <pthread.h>
  5. #include <errno.h>     
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10.  
  11. int threadCount;
  12. char *fileName;
  13. int recordsCount;
  14. char *word;
  15.  
  16. pthread_t *threads;
  17. int fd;
  18. int bufferSize;
  19.  
  20. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  21.  
  22. void atexit_function();
  23. void* search(void* word);
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.     atexit(atexit_function);
  28.  
  29.     if(argc!=5){
  30.         fprintf(stderr, "Input error <ilosc watkow><nazwa pliku><ilosc rekordow><slowo do wyszukania>\n" );
  31.         exit(EXIT_FAILURE);
  32.     }
  33.  
  34.     threadCount = atoi(argv[1]);
  35.     fileName = argv[2];
  36.     recordsCount = atoi(argv[3]);
  37.     word = argv[4];
  38.  
  39.     if((fd = open(fileName,O_RDONLY))<0){
  40.         fprintf(stderr, "open error\n");
  41.     }
  42.  
  43.     bufferSize = recordsCount * 1024;
  44.     threads = malloc(sizeof(pthread_t) * threadCount);
  45.  
  46.     int i;
  47.     for (i = 0; i < threadCount; ++i)
  48.     {
  49.         if(pthread_create(threads + i,NULL,search,word)<0){ // Null == atrybuty domyślne ,funkcja, parametry
  50.             fprintf(stderr, "pthread_create error\n");
  51.         }
  52.     }
  53.  
  54.     for (i = 0; i < threadCount; ++i)
  55.     {
  56.         if(pthread_join(threads[i],NULL) <0){
  57.             fprintf(stderr, "pthread_join error\n");
  58.         }
  59.     }
  60.  
  61.     exit(EXIT_SUCCESS);
  62. }
  63.  
  64. void atexit_function(){
  65.     pthread_mutex_destroy(&mutex);
  66.     free(threads);
  67.     close(fd);
  68. }
  69.  
  70. void* search(void* word){
  71.  
  72.     char buf[bufferSize+1];
  73.     long offset;
  74.     int end = 0;
  75.     int bytes = 0;
  76.     char id[1024]; //max
  77.     int i,k;
  78.  
  79.     char* text;             // pointer na znaleziony tekst
  80.     char newline[2];
  81.     sprintf(newline,"\n");
  82.  
  83.     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL)<0){ //synchroniczne anulowanie
  84.         fprintf(stderr, "pthread_setcanceltype error\n");
  85.     }
  86.  
  87.  
  88.     if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL)<0)
  89.     {
  90.         fprintf(stderr, "pthread_setcancelstate error\n");
  91.     }
  92.  
  93.     while(!end){
  94.  
  95.         pthread_mutex_lock(&mutex);
  96.  
  97.         if((offset = lseek(fd,0,SEEK_CUR))<0){
  98.             fprintf(stderr, "lseek error\n");
  99.         }
  100.  
  101.         if((bytes = read(fd,buf,bufferSize))<0){ //read nam dziala jak test cancel
  102.             fprintf(stderr, "read error\n");
  103.         }
  104.         pthread_mutex_unlock(&mutex);
  105.  
  106.  
  107.         if(bytes == 0){
  108.             end = 1;
  109.         }
  110.  
  111.         text=strstr(buf, word);
  112.  
  113.         if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL)<0)
  114.         {
  115.             fprintf(stderr, "pthread_setcancelstate error\n");
  116.         }
  117.        
  118.         pthread_testcancel();
  119.  
  120.         if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL)<0)
  121.         {
  122.         fprintf(stderr, "pthread_setcancelstate error\n");
  123.         }  
  124.        
  125.         if (text != NULL) {
  126.  
  127.  
  128.             pthread_mutex_lock(&mutex);
  129.            
  130.             char *id_tmp = strstr(text,newline);        //w ajdi[0] mamy \n i potem id az do spacji
  131.  
  132.             k=1;
  133.             while ( (int)id_tmp[k-1024] != 32) {            //az do spacji
  134.                 id[k-1]=id_tmp[k-1024];
  135.                 k++;
  136.             } //1024 bo powrot do lini
  137.            
  138.             offset +=  (strlen(buf) - strlen(text));
  139.  
  140.             for (i = 0; i < threadCount; ++i){      // anulowanie pozostałych wątków
  141.                 if(threads[i] != pthread_self())
  142.                     pthread_cancel(threads[i]);
  143.             }
  144.             printf("TID: %lu - znalazlem w rekordzie nr %s o offsecie: %ld\n",(unsigned long) pthread_self(),id,offset);
  145.  
  146.             pthread_mutex_unlock(&mutex);
  147.             break;
  148.         }  
  149.     }
  150.  
  151.     return NULL;
  152. }
  153.  
  154. /*
  155. .
  156. W wersji drugiej wątek, który odszukał napis również anuluje pozostałe wątki,
  157. lecz anulowanie jest synchroniczne - punktem anulowania wątku jest zakończenie przetwarzania wczytanej ilości rekordów do danych prywatnych.
  158. W wersji trzeciej wszystkie wątki powinny być odłączone a warunkiem zakończenia wątku jest odczytanie wszystkich rekordów pliku.
  159.  
  160.  
  161.  
  162. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement