Seeking Tutor Database

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

East West University

Department of CSE
CSE-325
Operating System

Project no:01

Project name:
Seeking Tutor Problem

GROUP NO: 01 Course Instructor information :

Name of Student & Id :


Tanni Mitra
 Mohammed Tasiful Alam Lecturer
( 2018-1-60-171) Department
 Tanvir Hasan of C.S.E
(2018-1-60-236) East West University

Date of Submitted :
15.01.2021

PROJECT REPORT
CONTENTS

 Problem Statement
 Description of each function
 Code
 Output
 Conclusion

Problem Statement:
We have a club where undergraduate students get help with their programming assignments
from tutor .The club has a coordinator and several tutors to assist the students. Here, students
can come to waiting area if there is no available seat they will go back to programming.
Otherwise, they will wait in the waiting area. The coordinator will then prioritize students in
a queue .Then tutor will find the student from the queue who has the highest priority and
tutor them .If more than one student has the same priority the tutor will choose whoever
came first . After getting the maximum amount of help student terminate. When all students
will be finished getting help, tutor will terminate and then coordinator will be terminated.

Methodologies we have used in the code:

o Pthread_create()
o Pthread_join()
o Sem_init()
o Sem_wait()
o Sem_post()
o Sem_mutex()
Description of each Function:
Student has 3 parts:
1. Student has to start programming and seek help from a tutor ,student needs to get a seat
.After coming to get seat, If student does not find any seat ,then has to go back to
programming and try again later.

2. If a student gets a seat then coordinator gets a notice and then wait for tutor to be
assigned

3. After getting maximum amount of help, the student will stop seeking help .

Coordinator has 4 parts:


1. The coordinator waits for students to come to seek help.

2. And prioritize students according to who has come first.

3. Then coordinator assigns tutor to each student.

4. If all students in the waiting area gets a tutor then coordinator notify the tutor and leaves .

Tutor has 3 parts:


1. Tutor waits for the coordinator to notify if a student has come to seek help.

2. Tutor selects to student to help according to the priority of the student

3. If all students have done seeking help from the tutor ,the tutor waits for coordinators
notification to finish work.
Code

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

struct priority_queue
{
int priority_no;
int time;
};

int finished_student_no=0, finished_tutor_no = 0, request_no = 0, student_no=0, tutor_no=0, help


= 0, total_chair = 0, occupied_chairs=0;

int visited[100];
struct priority_queue pq[100];
int priority[100];
int student_ids[100];
int tutor_ids[100];

sem_t student;
sem_t coordinator;
sem_t tutor[100];
sem_t mutex;

void *student_thread(void *student_id)


{
int s_id=*(int*)student_id;

while(1)
{
if(priority[s_id-1] == help)
{

sem_wait(&mutex);
finished_student_no++;
sem_post(&mutex);
printf("\n\nstudent %d terminates\n\n",s_id);

sem_post(&student);
pthread_exit(NULL);
}

sem_wait(&mutex);
if(occupied_chairs == total_chair)
{
printf("\nStudent: Student %d found no empty chair.\n",s_id);
sem_post(&mutex);
continue;
}

occupied_chairs++;
request_no++;
visited[s_id-1]=request_no;
printf("\nStudent: Student %d takes a seat.\nStudent: Empty chairs = %d\n",s_id,total_chair-
occupied_chairs);
sem_post(&mutex);

sem_post(&student);
sem_wait(&tutor[s_id-1]);

printf("\nStudent: Student %d received help.\n",s_id);

sem_wait(&mutex);
priority[s_id-1]++;
printf("\nStudent: Student %d priority now is %d\n",s_id, priority[s_id-1]);
sem_post(&mutex);
}
}

void *tutor_thread(void *tutor_id)


{
int t_id=*(int*)tutor_id;

while(1)
{
if(finished_student_no==student_no)
{
sem_wait(&mutex);
finished_tutor_no++;
sem_post(&mutex);

sem_wait(&mutex);
printf("\n\ntutor %d terminates\n\n",t_id);
if(finished_tutor_no == tutor_no)
{
printf("\n\ncoordinator terminates\n\n");

}
sem_post(&mutex);

pthread_exit(NULL);
}

sem_wait(&coordinator);

int max_request=student_no*help+1, max_priority = help-1 ,s_id = -1;


sem_wait(&mutex);
for(int i=0;i<student_no;i++)
{
if(pq[i].priority_no>-1 && pq[i].priority_no<=max_priority)
{
if (pq[i].time<max_request)
{
max_priority = pq[i].priority_no;//student's pariority will go
max_request=pq[i].time;//student's arrival time
s_id=student_ids[i];//saving student id in the array
}
}
}

if(s_id==-1) //found no students


{
sem_post(&mutex);
continue;
}

pq[s_id-1].priority_no = -1;
pq[s_id-1].time = -1;
occupied_chairs--;
sem_post(&mutex);

sem_wait(&mutex);
printf("\nTutor: Student %d tutored by Tutor %d\n",s_id,t_id);
sem_post(&mutex);

sem_post(&tutor[s_id-1]);

}
}

void *coordinator_thread(void *arg)


{
while(1)
{
if(finished_student_no==student_no)
{
for(int i=0;i<tutor_no;i++)
{
sem_post(&coordinator);
}
pthread_exit(NULL);
}

sem_wait(&student);

sem_wait(&mutex);
for(int i=0;i<student_no;i++)
{
if(visited[i]>-1)
{
pq[i].priority_no = priority[i];
pq[i].time = visited[i];//priority queue time will be the visited time

printf("\nCoordinator: Student %d with priority %d in the


queue.\n",student_ids[i],priority[i]);
visited[i]=-1;

sem_post(&coordinator);//coordinator passes the signal to tutor


}
}
sem_post(&mutex);
}
}

int main()
{
printf("Enter total student number: ");
scanf("%d", &student_no);
printf("Enter total tutor number: ");
scanf("%d", &tutor_no);
printf("Enter total chair number: ");
scanf("%d", &total_chair);
printf("Enter maximum help number: ");
scanf("%d", &help);

for(int i=0;i<student_no;i++)
{
visited[i]=-1;
pq[i].priority_no = -1;
pq[i].time = -1;
priority[i]=0;
}

sem_init(&student,0,0);
sem_init(&coordinator,0,0);
sem_init(&mutex,0,1);// only for critical secison we have taken 1
for(int i=0;i<student_no;i++)
{
sem_init(&tutor[i],0,0);
}

pthread_t students[student_no];
pthread_t tutors[tutor_no];
pthread_t coordinator;

for(int i = 0; i < student_no; i++)


{
student_ids[i] = i + 1;
if (pthread_create(&students[i], NULL, student_thread, (void*) &student_ids[i]) < 0)
{
perror("Error: thread cannot be created");
exit(1);
}
}

for(int i = 0; i < tutor_no; i++)


{
tutor_ids[i] = i + 1;
if (pthread_create(&tutors[i], NULL, tutor_thread, (void*) &tutor_ids[i]) < 0)
{
perror("Error: thread cannot be created");
exit(1);
}
}

if(pthread_create(&coordinator,NULL,coordinator_thread,NULL) < 0)
{
perror("Error: thread cannot be created");
exit(1);
}

for(int i =0; i < student_no; i++)


{
pthread_join(students[i],NULL);
}

for(int i =0; i < tutor_no; i++)


{
pthread_join(tutors[i],NULL);
}

pthread_join(coordinator, NULL);

return 0;
}
Output
Conclusion
This program synchronizes the tasks between the students, coordinator and tutors. The
students get help according to their priority so no one is left out.

You might also like