【问题标题】:Critical Section Synchronization C临界区同步 C
【发布时间】:2020-01-29 10:16:50
【问题描述】:

尝试实现该程序的关键部分,以便在两个线程之间正确交换,如描述后面所述。

我正在尝试为我的操作系统课程解决问题。问题是我需要输入两个文件,每个文件都放入它们单独的线程中,它们将通读文件,直到它们遇到数字“0”的行。那么另一个线程应该按照相同的规则运行。

这个程序应该接受两个文件输入并通过以特定顺序连接来自文件的两个输入来找出消息,然后在解密后打印输出。

这两个文件的输入如下图

Person1         Person2          
---------       ---------- 
t                  0
0                  h
i                  0
s                  0
0                  i
0                  s
0                  a
t                  0
e                  0
0                  s
t                  0

上面的输入应该产生一个带有这个输出的字符串

示例:“thisisatest”

目前,分配的问题是它没有在两个线程之间正确交换并处于无限循环中。

就像我上面说的,我正在尝试通过使用互斥锁和 Pthreads 来解决这个任务

下面是我的代码的当前实现

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



static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static char *charArray[1000];

void *threadPerson1(void *value){

    FILE *fPTR;
    char buffer[2];

    char *fileName = "Person1.txt";

    fPTR = fopen(fileName, "r");

    if (fPTR == NULL){

        printf("was unable to open: %s\n", fileName);
        return NULL;
    }

    while(1){

        //Entering the critical section
        pthread_mutex_lock(&mutex);

        fscanf(fPTR, "%s", buffer);
        printf("This is Person1: %s\n", buffer);



        if(buffer != "0"){

            charArray[count] = buffer;
            count++;
        }
        if(buffer == "0"){

            pthread_mutex_unlock(&mutex);
        }
        //exiting the critical section
    }


}

void *threadPerson2(void *value){

    FILE *fPTR;
    char buffer[2];

    char *fileName = "Person2.txt";

    fPTR = fopen(fileName, "r");    

    if (fPTR == NULL){

        printf("was unable to open: %s\n", fileName);
        return NULL;
    }

    while(1){

        //entering the critical section
        pthread_mutex_lock(&mutex);

        fscanf(fPTR, "%s", buffer);
        printf("This is Person2: %s\n", buffer);



        if(buffer != "0"){

            charArray[count] = buffer;
            count++;
        }
        if(feof(fPTR)){

            printf("read end of file of: Person2\n");
            fclose(fPTR);
            return NULL;
        }
        if(buffer == "0"){

            pthread_mutex_unlock(&mutex);
        }
        //exiting the critical section


    }


}


int main(int argc, char **argv){


    pthread_t thread1;
    pthread_t thread2;

    pthread_create(&thread1, NULL, threadPerson1, NULL);
    pthread_create(&thread2, NULL, threadPerson2, NULL);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);


    for(int x = 0; x < 1000; x++){

        if(charArray[x] == NULL){

            printf("\n");
            break;
        }else{

            printf("%s", charArray[x]);
        }
    }

    return 0;
}

【问题讨论】:

  • 问题是?
  • 对不起,我没提吗?我最终将如何更改关键部分以使其按我的需要运行?
  • 请阅读How to Ask。编辑问题以包含您希望代码做什么以及它做什么。
  • 请提供更多信息。第一个线程读取直到找到 0,然后点击第二个线程并等待。第二个线程读取直到找到 0 并且...什么?另外,如果文件中没有“0”会怎样?两个踏板是否读取相同的文件?如果有两个文件,如果第二个文件中没有“0”会发生什么?请画图,如果可能的话。乍一看,使用屏障。
  • 您似乎比较的是指针而不是字符串本身,所以buffer != "0" 条件始终是true。您要么需要使用字符串比较函数,如strcmp,要么读取单个字符以与字符常量进行比较。

标签: c multithreading pthreads critical-section


【解决方案1】:

您的程序中至少有两处不正确。首先,如果一个线程释放互斥锁,您不能保证调度程序会允许另一个线程运行,释放线程很可能会继续并立即重新获取互斥锁。使用条件变量,阅读其手册页。另外,这里有一些例子,特别是 4-8:Multithreaded programming guide

二,当你到达文件末尾时,你需要释放互斥锁来清理。最简单的方法是我们在 C++ 中称为 RAII,即使用资源句柄,当句柄对象超出范围时释放互斥锁。你可以在 C 中做类似的事情,例如通过注册一个清理函数,或者使用从那里调用的清理代码“转到”到函数的末尾。

【讨论】:

    猜你喜欢
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2013-03-26
    • 2013-04-07
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多