/ / Въведение в Pthread - c, linux, multithreading, pthreads

Въвеждане на Pthread - c, linux, multithreading, pthreads

Аз съм дълго време четец на stackoverflow и имамреших сам да помоля за съдействие. Преди да попитам, това е въпрос за домашна работа, тъй като изглежда би било по-добре да ви уведомя всички. Заданието е на многонишково и аз използвам C в Linux среда. Моята грешка е, че след като стартирам файла ./crazy.out, нишката на професора се създава и се показва printF, но нищо друго. не съм сигурен дали правилно използвам мютекса и условните сигнали, за да накарам студентската нишка да работи заедно с нея.

Професорска функция

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!!");

}
}

Нишката на професора.

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;
}

Функция и конец на ученика.

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 questionsn", (*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
}

Отговори:

1 за отговор № 1

според публикуван код:

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

И така, споделеният ресурс е професорът/офиса (има нужда от мютекс)

И така, тълпата са студентите, които искат достъп до професора

в момента професорът се справя с един студент, след което си отива.

Вероятно не това, което се иска, защото не се справя с множество ученици и не се справя с нито един ученик, който има множество въпроси.

IMO:

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

за да може професорът да прави други неща,

професорът може да уведоми студентите, когато са на разположение (може би чрез сигнал за условие)

студентите могат да уведомят професора, че искат да зададат въпрос (може би чрез сигнал за условие)