【发布时间】:2015-05-22 16:36:13
【问题描述】:
我正在编写代码。说明如下: 有教授和学生人数(代码中假设为 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;
}
【问题讨论】:
-
这似乎是哲学家就餐问题的变体,您可能想搜索并阅读它?
-
很高兴知道您的输出有什么问题。或者...输出是什么?
-
我在 linux 上检查了它(在修复了线程函数的 void* return 和 void* 自 64 位以来强制转换为 int 之后)并且 它工作正常。我能想到的唯一一件事是程序挂起,因为教授循环
while(1)并且正在等待主要。你的输出有什么问题? -
@ArnonZilca 我得到这个输出:
Professor is in office Student 0 enters office Student 1 enters office Student 2 enters office Professor starts to answer question for studnet 0 Student 0 asked a question Student 1 asked a question Student 2 asked a question Professor is done with the answer of student 2 Studnet 2 is stisfied Studnet 2 is stisfied Studnet 2 is stisfied Professor starts to answer question for studnet 2 Student 0 leaves office Student 1 leaves office Student 2 leaves office Professor is done with the answer of student 2 -
教授开始回答在学生提出问题之前显示。
标签: c multithreading synchronization pthreads semaphore