Untitled

 avatar
unknown
plain_text
4 years ago
3.0 kB
7
Indexable


Following is the code in sum3files.c :

#include <stdio.h>
#include <stdlib.h>

#include <pthread.h>

/*
 * Mutex for printing.
 */
pthread_mutex_t printf_mutex = PTHREAD_MUTEX_INITIALIZER;

struct Info
{
    int id;
    char *filename;
};

void* func(void *arg)
{
    struct Info *info = (struct Info*) arg;

    /*
     * Sum of file.
     */
    float sum = 0;

    int numbers_read = 0;

    /*
     * We open the file.
     */
    FILE *f = fopen(info->filename, "r");
    if(f == NULL)
    {
        printf("Cannot open %s\n", info->filename);
        exit(1);
    }

    /*
     * We keep reading the file.
     */
    while(1)
    {
        float number;
        if(fscanf(f, " %f", &number) == EOF)
        {
            /*
             * We are done.
             */
            break;
        }

        numbers_read++;
        sum = sum + number;

        pthread_mutex_lock(&printf_mutex);
        printf("Thread%d reads %d. The value is %.6f. The sum is : %.6f\n",
                info->id, numbers_read, number, sum);
        pthread_mutex_unlock(&printf_mutex);
    }

    /*
     * We close the file.
     */
    fclose(f);

    /*
     * We print the final result.
     */
    pthread_mutex_lock(&printf_mutex);
    printf("Thread%d Sum is : %.6f\n", info->id, sum);
    pthread_mutex_unlock(&printf_mutex);

    return NULL;
}


int main()
{
    /*
     * Info for the 3 threads.
     */
    struct Info infos[3] = {
        {1, "input1.txt"},
        {2, "input2.txt"},
        {3, "input3.txt"},
    };

    /*
     * We spawn the 3 threads, and wait for them to finish.
     */
    int i = 0;

    pthread_t tids[3];
    for(i = 0; i < 3; i++)
    {
        while(pthread_create(&tids[i], NULL, func, &infos[i]));
    }

    for(i = 0; i < 3; i++)
    {
        pthread_join(tids[i], NULL);
    }

    return 0;
}

Following is the run for the given input files :

gcc sum3files.c -o sum3files -lpthread

./sum3files 
Thread1 reads 1. The value is 1.230000. The sum is : 1.230000
Thread1 reads 2. The value is 2.660000. The sum is : 3.890000
Thread1 reads 3. The value is 3.780000. The sum is : 7.670000
Thread1 reads 4. The value is 8.560000. The sum is : 16.230000
Thread1 reads 5. The value is 9.820000. The sum is : 26.049999
Thread2 reads 1. The value is 5.230000. The sum is : 5.230000
Thread2 reads 2. The value is 5.660000. The sum is : 10.889999
Thread2 reads 3. The value is 6.890000. The sum is : 17.779999
Thread2 reads 4. The value is 6.740000. The sum is : 24.519999
Thread2 reads 5. The value is 8.960000. The sum is : 33.480000
Thread1 Sum is : 26.049999
Thread3 reads 1. The value is 5.230000. The sum is : 5.230000
Thread3 reads 2. The value is 5.660000. The sum is : 10.889999
Thread3 reads 3. The value is 8.890000. The sum is : 19.779999
Thread3 reads 4. The value is 9.740000. The sum is : 29.519999
Thread3 reads 5. The value is 7.960000. The sum is : 37.480000
Thread2 Sum is : 33.480000
Thread3 Sum is : 37.480000

Editor is loading...