Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <dirent.h>
- #include <string.h>
- static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
- struct thread_info {
- pthread_t thread_id;
- int thread_num;
- char *file;
- char *current_dir;
- };
- static void *count_lines(void *args)
- {
- pthread_mutex_lock(&lock);
- int count = 0;
- struct thread_info *tinfo = args;
- printf("%s\n", tinfo->file);
- char *line;
- line = (char*) malloc (sizeof(char));
- FILE *file = fopen(tinfo->file, "r");
- fgets(line, 256, file);
- fclose(file);
- if(count < 1)
- {
- printf("%s%s contient %d lignes\n", tinfo->current_dir, tinfo->file, count);
- } else
- {
- printf("%s%s contient %d ligne\n", tinfo->current_dir, tinfo->file, count);
- }
- pthread_mutex_unlock(&lock);
- pthread_exit(NULL);
- }
- int main(int argc, char* argv[]) {
- char directory[256];
- struct dirent *dir;
- char *current_dir = getcwd(directory, 256);
- DIR* d = opendir(current_dir);
- struct thread_info *tinfo;
- while((dir = readdir(d)) != NULL)
- {
- if(strcmp(dir->d_name, "..") != 0 && strcmp(dir->d_name, ".") != 0)
- {
- if(dir->d_type == 8)
- {
- }
- }
- }
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- tinfo = malloc(sizeof(struct thread_info));
- tinfo->file = "contributors.txt";
- tinfo->current_dir = current_dir;
- pthread_create(&tinfo->thread_id, &attr, count_lines, &tinfo);
- pthread_join(tinfo->thread_id, NULL);
- pthread_mutex_destroy(&lock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment