Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int threadCount;
- char *fileName;
- int recordsCount;
- char *word;
- pthread_t *threads;
- int fd;
- int bufferSize;
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- void atexit_function();
- void* search(void* word);
- int main(int argc, char *argv[])
- {
- atexit(atexit_function);
- if(argc!=5){
- fprintf(stderr, "Input error <ilosc watkow><nazwa pliku><ilosc rekordow><slowo do wyszukania>\n" );
- exit(EXIT_FAILURE);
- }
- threadCount = atoi(argv[1]);
- fileName = argv[2];
- recordsCount = atoi(argv[3]);
- word = argv[4];
- if((fd = open(fileName,O_RDONLY))<0){
- fprintf(stderr, "open error\n");
- }
- bufferSize = recordsCount * 1024;
- threads = malloc(sizeof(pthread_t) * threadCount);
- int i;
- for (i = 0; i < threadCount; ++i)
- {
- if(pthread_create(threads + i,NULL,search,word)<0){ // Null == atrybuty domyślne ,funkcja, parametry
- fprintf(stderr, "pthread_create error\n");
- }
- }
- for (i = 0; i < threadCount; ++i)
- {
- if(pthread_join(threads[i],NULL) <0){
- fprintf(stderr, "pthread_join error\n");
- }
- }
- exit(EXIT_SUCCESS);
- }
- void atexit_function(){
- pthread_mutex_destroy(&mutex);
- free(threads);
- close(fd);
- }
- void* search(void* word){
- char buf[bufferSize+1];
- long offset;
- int end = 0;
- int bytes = 0;
- char id[1024]; //max
- int i,k;
- char* text; // pointer na znaleziony tekst
- char newline[2];
- sprintf(newline,"\n");
- if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL)<0){ //synchroniczne anulowanie
- fprintf(stderr, "pthread_setcanceltype error\n");
- }
- if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL)<0)
- {
- fprintf(stderr, "pthread_setcancelstate error\n");
- }
- while(!end){
- pthread_mutex_lock(&mutex);
- if((offset = lseek(fd,0,SEEK_CUR))<0){
- fprintf(stderr, "lseek error\n");
- }
- if((bytes = read(fd,buf,bufferSize))<0){ //read nam dziala jak test cancel
- fprintf(stderr, "read error\n");
- }
- pthread_mutex_unlock(&mutex);
- if(bytes == 0){
- end = 1;
- }
- text=strstr(buf, word);
- if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL)<0)
- {
- fprintf(stderr, "pthread_setcancelstate error\n");
- }
- pthread_testcancel();
- if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL)<0)
- {
- fprintf(stderr, "pthread_setcancelstate error\n");
- }
- if (text != NULL) {
- pthread_mutex_lock(&mutex);
- char *id_tmp = strstr(text,newline); //w ajdi[0] mamy \n i potem id az do spacji
- k=1;
- while ( (int)id_tmp[k-1024] != 32) { //az do spacji
- id[k-1]=id_tmp[k-1024];
- k++;
- } //1024 bo powrot do lini
- offset += (strlen(buf) - strlen(text));
- for (i = 0; i < threadCount; ++i){ // anulowanie pozostałych wątków
- if(threads[i] != pthread_self())
- pthread_cancel(threads[i]);
- }
- printf("TID: %lu - znalazlem w rekordzie nr %s o offsecie: %ld\n",(unsigned long) pthread_self(),id,offset);
- pthread_mutex_unlock(&mutex);
- break;
- }
- }
- return NULL;
- }
- /*
- .
- W wersji drugiej wątek, który odszukał napis również anuluje pozostałe wątki,
- lecz anulowanie jest synchroniczne - punktem anulowania wątku jest zakończenie przetwarzania wczytanej ilości rekordów do danych prywatnych.
- W wersji trzeciej wszystkie wątki powinny być odłączone a warunkiem zakończenia wątku jest odczytanie wszystkich rekordów pliku.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement