Skip to content
Advertisement

How to write integers from file to buffer using POSIX pthreads in Linux?

I want to write a program to get integers from a file and put them in a buffer using multiple threads. The buffer size is 20 and there are 100 integers. I can’t use for-loop because one thread should read one integer at a time. As an example, if we are using one thread that thread must run 100 times to read the data from the file to buffer. I wrote following code to using POSIX pthreads. But the loop runs forever and it keeps reading only the first integer on the file. Can someone point me out the error, please? (The integers are not separated by a comma.Ex: 1 2 3 4 ….etc upto 100 )

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



pthread_mutex_t m;
pthread_cond_t  r;
pthread_cond_t w;
FILE *fp;
int cnt_w = 0;
int cnt_r= 0;

int value;
int buf[20];
int cnt = 0;
int z = 0;
int flag =0;


void *read(void *parm);
void *write(void *parm);

int main(int argc, char argv[])
{

    pthread_t rid[2];
    pthread_t wid[2];




    for( int i = 0; i< 2; i++)
    {
        printf("Writer %d Starts n",i+1);
        pthread_create(&wid[i], NULL, write, i);
    }


//    for(int i = 0; i < 2; i++)
//    {
//        printf("Reader %d Starts n",i+1);
//        pthread_create(&rid[i],NULL,read,i);
//    }

//    for(int i = 0; i < 2; i++)
//    {
//        pthread_join(rid[i],NULL);
//    }


    for(int i = 0; i < 2; i++)
    {
        pthread_join(wid[i], NULL);
    }

    for(int i =0;i<3;i++)
    {
        printf("%d ",buf[i]);
    }

    return 0;
}

void *read(void *parm)
{


}

void *write(void *parm)
{
    int x = (int)parm ;
    int y = 0;

    while(flag != -1) {
        pthread_mutex_lock(&m);
        printf("Writer %d Locked Mutexn", x + 1);
        while (cnt != 0) {
            printf("Writer %d is waitingn", x + 1);
        }

        cnt++;
        printf("Writer %d Access CS n", x + 1);

        if ((fp = fopen("/home/pegasus/2/shared_data.txt", "r")) == NULL) {
            fprintf(stderr, "Couldn't find the file");
        } else {
            fscanf(fp, "%d", &value);
            buf[z] = value;


            printf("Buf value = %dn", buf[z]);
            y++;
            printf("z = %dn", z);
            z = z + y;
            printf("z = %dn", z);
            sleep(1);

            if (feof(fp)) {
                flag = -1;
                break;
            }

            fclose(fp);
        }
        printf("Writer %d Finished Access CS n", x + 1);
        printf("x = %d n", y);

        cnt = 0;
        pthread_cond_signal(&r);
        printf("Signal Readern");
        pthread_cond_signal(&w);
        printf("Signal Writern");


        printf("Writer %d Unlocked Mutexn", x + 1);
        pthread_mutex_unlock(&m);

    }

}

Advertisement

Answer

You have numerous problems with the way you use your synchronization objects, some of which I described in comments, but the problem you asked about:

the loop runs forever and it keeps reading only the first integer on the file

occurs because each thread opens the file freshly each time it wants to read a number. Of course it then always reads the same number (the first), and never sees the end of the file.

Instead, open the file just once for the whole program, probably in main().

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement