Skip to content
Extraits de code Groupes Projets
main.c 4,73 ko
Newer Older
  • Learn to ignore specific revisions
  • Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    #include <stdlib.h>
    #include <stdio.h>
    #include <stdbool.h>
    #include <unistd.h>
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    #include <stdint.h>
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
    #include <pthread.h>
    #include <semaphore.h>
    
    #include "fonctions.h"
    
    
    FILE *file_in;
    FILE *file_out;
    char *f_in;
    char *f_out;
    int nthreads = 1;
    buffer1 *buffer_1;
    buffer2 *buffer_2;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    int nlines;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    void *reading() {
        file_in = fopen(f_in, "r");
        if (file_in == NULL) {
            printf("Error with file_in.\n");
            exit(EXIT_FAILURE);
        } else {
            printf("file_in opened with succes.\n");
        }
    
        file_out = fopen(f_out, "w");
        if (file_out == NULL) {
            printf("Error with file_out.\n");
            exit(EXIT_FAILURE);
        } else {
            printf("file_out opened with succes.\n");
        }
    
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        uint64_t number;
        while (fscanf(file_in, "%lu", &number) != EOF) {
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
            sem_wait(&(buffer_1->free));
            pthread_mutex_lock(&(buffer_1->mutex));
            put_node_t(buffer_1->numbers, number);
            pthread_mutex_unlock(&(buffer_1->mutex));
            sem_post(&(buffer_1->full));
        }
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
        pthread_mutex_lock(&(buffer_1->mutex));
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        buffer_1->can_stop = true;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        pthread_mutex_unlock(&(buffer_1->mutex));
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        return NULL;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    }
    
    
    void *calculating() {
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        while (buffer_1->can_stop == false || buffer_1->numbers->length != 0) {
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
            uint64_t number;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
            sem_wait(&(buffer_1->full));
            pthread_mutex_lock(&(buffer_1->mutex));
            number = get_node_t(buffer_1->numbers);
            pthread_mutex_unlock(&(buffer_1->mutex));
            sem_post(&(buffer_1->free));
    
            list_t *list_prime = prime_list(number);
    
            sem_wait(&(buffer_2->free));
            pthread_mutex_lock(&(buffer_2->mutex));
            put_node_lst(buffer_2->prime_numbers, list_prime);
            pthread_mutex_unlock(&(buffer_2->mutex));
            sem_post(&(buffer_2->full));
        }
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        return NULL;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    }
    
    void *writing() {
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        int nlines = count_lines(f_in);
        while (nlines > 0) {
            nlines --;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
            sem_wait(&(buffer_2->full));
            pthread_mutex_lock(&(buffer_2->mutex));
            list_t *removed = get_node_lst(buffer_2->prime_numbers);
            pthread_mutex_unlock(&(buffer_2->mutex));
            sem_post(&(buffer_2->free));
    
            int length = removed->length;
            for (int i = 0; i < length; i++) {
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
                fprintf(file_out, "%lu ", get_node_t(removed));
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
            }
            clear_list_t(removed);
            free(removed);
            fprintf(file_out, "\n");
        }
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        return NULL;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    }
    
    int main(int argc, char *argv[]) {
        int opt;
        while ((opt = getopt(argc, argv, "N:")) != -1) {
            switch(opt) {
                case 'N':
                    nthreads = atoi(optarg);
                    printf("Number of threads : %d.\n", nthreads);
                    break;
                default:
                    printf("Invalid number of threads.\n");
                    break;
            }
            f_in = argv[optind];
            f_out = argv[optind + 1];
        }
        
        
        buffer_1 = init_buffer_1(nthreads);
        if (!buffer_1) {
            free(buffer_1);
            printf("Error with buffer_1.\n");
            return -1;
        }
    
        buffer_2 = init_buffer_2(nthreads);
        if (!buffer_2) {
            free(buffer_2);
            printf("Error with buffer_2.\n");
            return -1;
        }
    
        pthread_t read;
        pthread_t calc[nthreads];
        pthread_t write;
    
        if (pthread_create(&read, NULL, &reading, NULL) != 0) {
            printf("Error with creating reading thread.\n");
            return -1;
        }
    
        for (int i = 0; i < nthreads; i++) {
            if (pthread_create(&calc[i], NULL, &calculating, NULL) != 0) {
                printf("Error with creating calculating thread.\n");
                return -1;
            }
        }
    
        if (pthread_create(&write, NULL, &writing, NULL) != 0) {
            printf("Error with creating writing thread.\n");
            return -1;
        }
    
        if (pthread_join(read, NULL) != 0) {
            printf("Error with join reading thread.\n");
            return -1;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        } else {
            printf("Read_thread_join OK.\n");
        }
    
        for (int i = 0; i < nthreads; i++) {
            if (pthread_cancel(calc[i]) != 0) {
                printf("Error with cancel calculating thread[%d].\n", i);
            } else {
                printf("Calc_thread_cancel[%d].\n", i);
            }
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        }
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        for (int i = 0; i < nthreads; i++) {
            if (pthread_join(calc[i], NULL) != 0) {
                printf("Error with join calculating thread.\n");
                return -1;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
            } else {
                printf("Calc_thread_join[%d] OK.\n", i);
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
            }
        }
    
        if (pthread_join(write, NULL) != 0) {
            printf("Error with join writing thread.\n");
            return -1;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        } else {
            printf("Write_thread_join OK.\n");
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
        }
    
        fclose(file_in);
        fclose(file_out);
        printf("Files close.\n");
    
        clear_list_t(buffer_1->numbers);
        free(buffer_1->numbers);
        free(buffer_2->prime_numbers);
    
        free(buffer_1);
        free(buffer_2);
    
        return 0;
    
    Arnaud Lefebvre's avatar
    Arnaud Lefebvre a validé
    }