Created
May 7, 2013 14:44
-
-
Save pstachula-dev/5533124 to your computer and use it in GitHub Desktop.
Wątki lab7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include <sys/errno.h> | |
char * nazwa_pliku; | |
char znak; | |
int bloki, i, j, ilosc_znakow, ilosc_watkow, err, reszta; | |
pthread_t * th; | |
struct argumenty { | |
int ilosc_znakow; | |
int blok_danych; | |
char * dane; | |
int nr_watku; | |
}; | |
pthread_mutex_t fastMutex = PTHREAD_MUTEX_INITIALIZER; | |
void* znajdz_znak (void* args) | |
{ | |
struct argumenty * arg = (struct argumenty*) args; | |
int tmp = arg->blok_danych * arg->nr_watku; | |
printf("Id watku: %d\n", pthread_self()); | |
for(j; j < tmp ; j++) | |
{ | |
if(arg->dane[j] == znak) | |
{ | |
pthread_mutex_lock(&fastMutex); | |
printf("Ilosc znalezionych znakow: (%d) \n", arg->ilosc_znakow++ +1); | |
pthread_mutex_unlock(&fastMutex); | |
} | |
} | |
ilosc_znakow += arg->ilosc_znakow; | |
return NULL; | |
} | |
/* | |
* | |
*/ | |
int main(int argc, char** argv) | |
{ | |
/*if (argc < 4) | |
return -1; | |
nazwa_pliku = argv[1]; | |
znak = argv[2][0]; | |
ilosc_watkow = atoi(argv[3]);*/ | |
nazwa_pliku = "text"; | |
znak = 'x'; | |
ilosc_watkow = 2; | |
// Dane poczatkowe | |
FILE* file = fopen(nazwa_pliku, "r"); | |
if (file == NULL) | |
return -1; | |
int rozmiar; | |
fseek(file, 0, SEEK_END); | |
rozmiar = ftell (file); | |
rewind(file); | |
char * dane = (char*)malloc(sizeof(char) * rozmiar); | |
if(dane == NULL) | |
{ | |
printf("Blad pamieci!", stderr); | |
return -1; | |
} | |
fread(dane, sizeof(char), rozmiar, file); | |
fclose(file); | |
// Obsluga pliku | |
bloki = rozmiar / ilosc_watkow; | |
if(reszta = bloki % ilosc_watkow) | |
{ | |
ilosc_watkow += 1; | |
th = (pthread_t *)malloc(sizeof(pthread_t) * ilosc_watkow); | |
} | |
else | |
{ | |
th = (pthread_t *)malloc(sizeof(pthread_t) * ilosc_watkow); | |
} | |
struct argumenty * args = (struct argumenty *)malloc(sizeof(struct argumenty)); | |
printf("Ilosc blokow %d \n\n", bloki); | |
for(i=0; i < ilosc_watkow; i++) | |
{ | |
args[i].blok_danych = bloki; | |
args[i].dane = dane; | |
args[i].ilosc_znakow = 0; | |
args[i].nr_watku = i+1; | |
err = pthread_create(&th[i], NULL, znajdz_znak, &args[i]); | |
if (err != 0) | |
{ | |
printf("\nNie mozna utworzyc watku :[%s]\n", strerror(err)); | |
return -1; | |
} | |
else | |
{ | |
printf("\nWatek [%d] utworzony pomyslnie!\n", i+1); | |
} | |
} | |
// Tworzneia watkow | |
for (i = 0; i < ilosc_watkow; i++) | |
{ | |
if (pthread_join(th[i], NULL) != 0) | |
{ | |
return -1; | |
} | |
} | |
// Oczekiwanie na zwolnienie poprzedniego watku | |
printf("\nWYNIK, Ogolna liczba znakow = %d \n", ilosc_znakow); | |
free(th); | |
free(args); | |
free(dane); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment