c - 线程和同步

标签 c multithreading synchronization pthreads semaphore

我正在编写代码。说明如下: 有教授和学生人数(代码中假设为 3)。学生可以提出问题,教授会回答,但请记住以下几点-

(i) 一次只有一个人在说话,

(ii) 每个学生的问题都由教授回答,并且

(iii) 在教授回答完上一个问题之前,没有学生再问另一个问题。

代码的可能输出是:

Student 0 enters the office.
Student 1 enters the office.
Student 1 asks a question.
Professor starts to answer question for student 1.
Professor is done with answer for student 1.
Student 1 is satisfied.
Student 0 asks a question.
Professor starts to answer question for student 0.
Professor is done with answer for student 0.
Student 0 is satisfied.
Student 0 leaves the office.
Student 2 enters the office.
Student 2 asks a question.
Professor starts to answer question for student 2.
Professor is done with answer for student 2.
Student 2 is satisfied.
Student 2 leaves the office.

我的代码附在下面,我没有得到想要的输出。非常感谢对我做错的任何帮助。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/fcntl.h>

sem_t student,professor, askQuestion,numberOfStudents;
int id=0,number=0;

void AnswerStart()
{
  printf("Professor starts to answer question for studnet %d\n",id);
  return;
}

void AnswerDone()
{
  printf("Professor is done with the answer of student %d\n",id);
  return;
}

void QuestionStart()
{
    printf("Student %d asked a question\n",id);
    return;
}

 void QuestionDone()
{
  printf("Studnet %d is stisfied\n",id);
  return;
}

void Studnet(void *a)
{
  sem_wait(&numberOfStudents);
  number++;
  printf("Student %d enters office\n",(int *)a);
  sem_post(&numberOfStudents);

  sem_wait(&askQuestion);
  id = (int *)a;
  QuestionStart();
  sem_post(&professor);
  sem_wait(&student);
  QuestionDone();
  sem_post(&askQuestion);

  sem_wait(&numberOfStudents);
  number--;
  printf("Student %d leaves office\n",(int *)a);
  sem_post(&numberOfStudents);
}

void Professor(void *a)
{
  printf("Professor is in office\n");
  while(1){
     sem_wait(&professor);
     AnswerStart();
     AnswerDone();
     sem_post(&student);
     sem_wait(&numberOfStudents);
     if(number==0)
     return;
     sem_post(&numberOfStudents);
 }

}


int main(){

sem_init(&student,0,0);
sem_init(&professor,0,0);
sem_init(&askQuestion,0,1);
sem_init(&numberOfStudents,0,1);

pthread_t studentThread[3], professorThread;
pthread_create(&professorThread,NULL,Professor,NULL);
pthread_create(&studentThread[0],NULL,Studnet,(void *)0);
pthread_create(&studentThread[1],NULL,Studnet,(void *)1);
pthread_create(&studentThread[2],NULL,Studnet,(void *)2);

pthread_join(studentThread[0],NULL);
pthread_join(studentThread[1],NULL);
pthread_join(studentThread[2],NULL);
pthread_join(professorThread,NULL);

sem_destroy(&student);
sem_destroy(&professor);
sem_destroy(&askQuestion);
sem_destroy(&numberOfStudents);
return 0;
}

最佳答案

当然可以!(花了一段时间=])

您正在为所有学生使用相同的 id 变量,并从不同的线程读取和写入它!

删除全局 id 变量而不是

id = (int *)a;

写:

int id = (int)a;

并让你的打印函数接受一个 int 参数,如下所示:

void QuestionStart(int id)
void QuestionDone(int id)

为了让你的教授印有学生编号,你可以使用一个全局变量,我们称它为 currentStudent,它只会在获取教授的信号量后设置。
[* 我认为您需要另一个信号量来将教授的工作与分配该变量分开]


如果仍有问题,请尝试在每次打印后添加此行:

fflush(stdout);

[可能还有打印冲洗问题]

关于c - 线程和同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29146699/

相关文章:

C 程序守护进程使用 100% 的 cpu 使用率

c - 如何在程序中打印 Ansii c 扩展代码?

c++ - 如何杀死用C++创建的分离线程

c# - 运行时不同线程中变量的作用域

c++ - 并发 C++11 - 可以使用哪些工具链?

objective-c - NSManagedObjectContext 异步或同步保存更改?

php - 正确的方式? 2 个 mysql DB 之间的自动同步

C : how can I change from file descriptor to FILE struct and vice versa?

c - printf中的*有什么用?

java - 多线程循环的效率