c - 共享缓冲区和同步编程的问题

标签 c pthreads buffer mutex

我刚刚开始学习如何用 C 语言处理线程和同步编程。我正在尝试编写一个使用线程(POSIX 接口(interface))从共享缓冲区读取选定文件的阅读器。子线程将从缓冲区中检索文件名,而父线程将无限地从 stdin 读取文件名并将它们放入缓冲区中。我做错了什么?

pthread_mutex_t lock;

static char* files[NFILES];
int top = NFILES-1;

void putInBuffer(char* file){
    pthread_mutex_lock(&lock);
    if(top < NFILES-1){
        files[top] = file;
        top++;
    }
    pthread_mutex_unlock(&lock);
}

char* removeFromBuffer(){
    char* file;
    pthread_mutex_lock(&lock);
    file = files[top];
    top--;
    pthread_mutex_unlock(&lock);
    return file;
}

void* leitor(){
    int op,i,r,cl;
    char* file;
    char buff[NCHARS];
    char teste[NCHARS];
    while(1){
        pthread_mutex_lock(&lock);
        file = removeFromBuffer();
        printf("%s\n", file);
        op = open(file, O_RDONLY);
        if(op == -1) {
            perror("Open unsuccessful");
            pthread_exit((void*)-1);
        }
        r = read(op, teste, NBYTES);
        if(r == -1){
            perror("Read unsuccessful");
            pthread_exit((void*)-1);
        }
        for(i=0; i<NLINES-1; i++){
         r = read(op, buff, NBYTES);
         if(r == -1){
            perror("Read unsuccessful");
            pthread_exit((void*)-1);
         }
         if(strcmp(buff,teste) != 0){
          perror("Incorrect file");
          pthread_exit((void*)-1);
         }
        }
        cl = close (op);
        if(cl == -1){
            perror("Close unsuccessful");
            pthread_exit((void*)-1);
        }
        printf("Correct file: %s\n", file);
        pthread_mutex_unlock(&lock);
    }
    pthread_exit((void*)0);
    return NULL;
}

int main(){

    pthread_t threads[NTHREADS];
    int i,*status;
    char file[LENFILENAME];
    if (pthread_mutex_init(&lock, NULL))
    {
        perror("\n mutex init failed\n");
        exit(-1);
    }
    for(i=0;i<NTHREADS;i++){
        if(pthread_create(&(threads[i]),NULL, leitor,NULL)){
            perror("Failed to create thread");
            exit(-1);
        }
    }

    while(1){
        read(STDIN_FILENO, file, LENFILENAME);
        printf("%s\n", file);
        putInBuffer(file);
        printf("%s\n", removeFromBuffer());
    }
    for (i=0;i<NTHREADS;i++){
        if(pthread_join(threads[i],(void**)&status)){
            perror("Failed to join thread");
            exit(-1);
        }
        printf("Thread returned %d\n", status);
    }
    pthread_mutex_destroy(&lock);
    return 0;
}

最佳答案

考虑到您的程序正在执行的操作,似乎您应该使用单独的信号量来通知子线程新的输入,而不是使用您创建的互斥体。

每个子线程都应该等待 while 循环顶部的信号量,当前有 pthread_mutex_lock()。父进程完成 putInBuffer 后,应该释放一次信号量。当子线程获取信号量时,它可以调用removeFromBuffer来获取下一个文件并读取它(即您已经写入的内容)。子进程完成文件处理后,不应释放信号量,而应返回循环顶部并再次等待。

您已正确使用 putInBufferremoveFromBuffer 中的互斥体来保护对共享变量 filestop 的访问>.

关于c - 共享缓冲区和同步编程的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27278989/

相关文章:

c - 使用 C 修改从索引开始的数组内容

c++ - 在 AIX 上使用 pthread 进行静态链接

c++ - iOS枚举为什么用十六进制而不是十进制?

c - 理解 char *、char[] 和 strcpy()

pthread 中的条件变量和实时优先级

c - 将参数传递给 pthread

C++延迟printf直到需要

c - 为什么我总是得到字符串数组的最后一个元素?

c - 如何在不截断任何单词的情况下使用缓冲区正确读取 stdin 的输入?

c++ - cpp程序中system函数的使用