Created
May 14, 2015 11:57
-
-
Save calindotgabriel/8a969c3da6aeeacf3141 to your computer and use it in GitHub Desktop.
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
/* | |
Progr. C ce primeste ca argumente nume de fisier si pt. fiecare creeaza un thread | |
ce calculeaza dimensiunea lui. La sfarsit se afiseaza maximul dintre dimensiuni. | |
*/ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
int max = -1; | |
pthread_mutex_t lock; | |
void* getFileSize(void* arg) { | |
// printf("am primit fisierul %s \n", (char*)arg); | |
char* fname = (char*) arg; | |
if( access( fname, F_OK ) == -1 ) { | |
printf("Nu pot accesa fisierul %s \n", fname); | |
pthread_exit(NULL); | |
} | |
FILE* f = fopen(fname, "r"); | |
int size = getSize(f); | |
printf("Fisierul %s are dimensiunea %d \n", fname, size); | |
pthread_mutex_lock(&lock); | |
if (size > max) { | |
max = size; | |
} | |
pthread_mutex_unlock(&lock); | |
fclose(f); | |
pthread_exit(NULL); | |
} | |
int getSize(FILE* f) { | |
fseek(f, 0L, SEEK_END); | |
return ftell(f); | |
} | |
int main (int argc, char* argv[]) { | |
pthread_t threads[argc]; | |
pthread_mutex_init(&lock, NULL); | |
int i; | |
for (i = 1 ; i < argc ; i ++) { | |
//printf("creez threadul %d cu arg %s \n", i, argv[i]); | |
pthread_create(&threads[i], NULL, getFileSize, (void*) argv[i]); | |
} | |
for (i = 1 ; i < argc ; i ++) { | |
pthread_join(threads[i], NULL); | |
} | |
if (max == -1) { | |
printf("Nu pot determina maxim, nu am citit nici un fisier"); | |
} | |
else { | |
printf("Max = %d \n", max); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment