c - 如何在 Linux 中使用 POSIX pthreads 将整数从文件写入缓冲区?

标签 c linux pthreads posix mutex

我想编写一个程序来从文件中获取整数并使用多个线程将它们放入缓冲区中。缓冲区大小为 20,有 100 个整数。我无法使用 for 循环,因为一个线程应该一次读取一个整数。举个例子,如果我们使用一个线程,该线程必须运行 100 次才能将数据从文件读取到缓冲区。我编写了以下代码来使用 POSIX pthreads。但循环会永远运行,并且它只会读取文件中的第一个整数。有人可以指出我的错误吗? (整数不以逗号分隔。例如:1 2 3 4 ....etc 最多 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 Mutex\n", x + 1);
        while (cnt != 0) {
            printf("Writer %d is waiting\n", 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 = %d\n", buf[z]);
            y++;
            printf("z = %d\n", z);
            z = z + y;
            printf("z = %d\n", 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 Reader\n");
        pthread_cond_signal(&w);
        printf("Signal Writer\n");


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

    }

}

最佳答案

您在使用同步对象的方式上存在许多问题,其中一些问题我在评论中描述过,但您询问的问题:

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

发生的原因是每个线程每次想要读取数字时都会重新打开文件。 当然它总是读取相同的数字(第一个),并且永远不会看到文件末尾。

相反,在整个程序中只打开一次文件,可能在 main() 中。

关于c - 如何在 Linux 中使用 POSIX pthreads 将整数从文件写入缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50610150/

相关文章:

c - libiptc : adding DNAT rule with destination IP

linux - Apache httpd :bind:Address already in use

c++ - 为什么 pthread_exit 会抛出被省略号捕获的东西?

c++ - 在销毁 pthread 互斥体之前锁定它是一种好习惯吗?

c - 如何从 C 中的 "raw"内存中读取/写入类型值?

c - 如何填充两个数组删除重复的数字

linux - 创建一个 git 预接收 Hook

c++ - std::map 插入/删除的并发问题

c - c的多维数组理解

c - 为什么GDB和Objdump的指令地址是一样的?