Seeking Tutor Database
Seeking Tutor Database
Seeking Tutor Database
Department of CSE
CSE-325
Operating System
Project no:01
Project name:
Seeking Tutor Problem
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.
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 .
4. If all students in the waiting area gets a tutor then coordinator notify the tutor and leaves .
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 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;
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]);
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);
}
}
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);
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]);
}
}
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
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;
if(pthread_create(&coordinator,NULL,coordinator_thread,NULL) < 0)
{
perror("Error: thread cannot be created");
exit(1);
}
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.