首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >丝线游戏攻略

丝线游戏攻略
EN

Stack Overflow用户
提问于 2015-07-18 09:52:54
回答 1查看 229关注 0票数 0

我是一位长期阅读堆栈溢出的读者,并决定自己寻求帮助。在我问之前,这是一个家庭作业问题,因为似乎最好通知你们所有人。任务是多线程,我在linux环境中使用C。我的错误是,一旦我运行./crazy.out文件,就会创建教授线程,并显示printF,但没有其他任何内容。我不确定我是否正确地使用互斥和条件信号使学生线程与它一起工作。

教授函数

代码语言:javascript
复制
void Professor()
{
//Initialize global variables
studentCondition = 0;
professorCondition = 0;
snack = 0;
wakeup = 0;
students = 0;

//Initialize pthread mutex
pthread_mutex_init(&lock_prof, NULL);
pthread_mutex_init(&lock_stud, NULL);
pthread_mutex_init(&lock_wait, NULL);
pthread_mutex_init(&lock_snack, NULL);
pthread_mutex_init(&lock_question, NULL);

//Initialize pthread conditions
pthread_cond_init(&professor, NULL);
pthread_cond_init(&student, NULL);

if( pthread_create(&pStack, NULL, professorThread(), NULL))
{
    perror("Thread creation failed!!");

}
}

教授的线索。

代码语言:javascript
复制
void * professorThread()
{

printf("Crazy professor's hours have started!\n");

if(students == 0)
{
    Nap();  
}
pthread_cond_wait(&professor, &lock_prof);
professorCondition = 1;
while(professorCondition)
{
    professorCondition = 0;
    if(stud != NULL)
    {
        AnswerStart();
        AnswerDone();
        //Increment snack counter
        snack++;

        //Since answer is finished, tell the student
        pthread_cond_signal(&student);

        //If 3 questions have been answered, snack time!
        if(snack == 3)
        {
            pthread_cond_wait(&professor, &lock_question);//Lock professor     while he snacks. No questions please.
            Snack();
            snack = 0;
        }
    }
}
return EXIT_SUCCESS;
}

学生函数和线程。

代码语言:javascript
复制
void Student(int id, int numQuestions)
{
struct student * newStudent = malloc(sizeof(student));

newStudent->id = id + 1001;
newStudent->numQuestions = numQuestions;

pthread_t tStack;

if(pthread_create(&tStack, NULL, (void *) &studentThread, (void *) newStudent ) )
{
    perror("Creation of thread occurred.");
    exit(0);
}
}

void * studentThread(void * student)
{
struct student * s = student;

printf("Student %d is at professor's door and wants to ask %d questions\n", (*stud).id, (*stud).numQuestions);

//don't forget increment num of students
students++;
if(stud != NULL)
{
    data = (*data).next;
    (*data).next = s;
}else
     {
    data = stud;
    stud = s;
     }
//mutex lock students
pthread_mutex_lock(&lock_stud);
while(1){


    //wait for prof
    pthread_mutex_lock(&lock_prof);
    //ask q
    if(stud != NULL)
    {
        stud = NULL;
        pthread_cond_signal(&professor);
    }   

    if( stud != NULL)
    {
        QuestionStart();
        QuestionDone();
    }

    //mutex unlock
    pthread_mutex_unlock(&lock_prof);   
}
//run question loop until numstudents = 0
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-18 12:23:49

根据张贴的代码:

代码语言:javascript
复制
1) the professor arrives at his office
2) if no students in a the crowd outside the office, then professor naps
3) professor lets one student, from crowd, into his office
4) the student asks a question
5) the professor answers the question
6) the student leaves the office
7) the professor leaves his office

因此,共享的资源是教授/办公室(需要互斥)。

所以,人群是那些想要接触教授的学生。

目前,教授处理一个学生,然后离开。

可能不是想要的,因为它不处理多个学生,也不处理任何一个有多个问题的学生。

海事组织:

代码语言:javascript
复制
there needs to be multiple student threads.   
Each student thread has a random number of questions. (which could be 0)
student loop:
    if number student questions is > 0
    then student pends on professor resource
    else student exits loop (exits student thread)

    When a student gets the professor resource, 
        the student locks the professor resource.(enters office)
        the student asks question
        the professor answers question
        the student unlocks the professor resource (exits office)
        the number of questions for that student is decremented
end loop

为了让教授能够做其他事情,

教授可以让学生知道他们什么时候有空(也许是通过条件信号)。

学生们可以让教授知道他们想问一个问题(也许是通过条件信号)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31489921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档