-
Notifications
You must be signed in to change notification settings - Fork 14
/
TA.c
142 lines (119 loc) · 3.85 KB
/
TA.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//Name: Shehab Mohamed
#include <pthread.h> //Create POSIX threads.
#include <time.h> //Wait for a random time.
#include <unistd.h> //Thread calls sleep for specified number of seconds.
#include <semaphore.h> //To create semaphores
#include <stdlib.h>
#include <stdio.h> //Input Output
pthread_t *Students; //N threads running as Students.
pthread_t TA; //Separate Thread for TA.
int ChairsCount = 0;
int CurrentIndex = 0;
//Declaration of Semaphores and Mutex Lock.
sem_t TA_Sleep;
sem_t Student_Sem;
sem_t ChairsSem[3];
pthread_mutex_t ChairAccess;
//Declared Functions
void *TA_Activity();
void *Student_Activity(void *threadID);
int main(int argc, char* argv[])
{
int number_of_students; //a variable taken from the user to create student threads. Default is 5 student threads.
int id;
srand(time(NULL));
//Initializing Mutex Lock and Semaphores.
sem_init(&TA_Sleep, 0, 0);
sem_init(&Student_Sem, 0, 0);
for(id = 0; id < 3; ++id) //Chairs array of 3 semaphores.
sem_init(&ChairsSem[id], 0, 0);
pthread_mutex_init(&ChairAccess, NULL);
if(argc<2)
{
printf("Number of Students not specified. Using default (5) students.\n");
number_of_students = 5;
}
else
{
printf("Number of Students specified. Creating %d threads.\n", number_of_students);
number_of_students = atoi(argv[1]);
}
//Allocate memory for Students
Students = (pthread_t*) malloc(sizeof(pthread_t)*number_of_students);
//Creating TA thread and N Student threads.
pthread_create(&TA, NULL, TA_Activity, NULL);
for(id = 0; id < number_of_students; id++)
pthread_create(&Students[id], NULL, Student_Activity,(void*) (long)id);
//Waiting for TA thread and N Student threads.
pthread_join(TA, NULL);
for(id = 0; id < number_of_students; id++)
pthread_join(Students[id], NULL);
//Free allocated memory
free(Students);
return 0;
}
void *TA_Activity()
{
while(1)
{
sem_wait(&TA_Sleep); //TA is currently sleeping.
printf("~~~~~~~~~~~~~~~~~~~~~TA has been awakened by a student.~~~~~~~~~~~~~~~~~~~~~\n");
while(1)
{
// lock
pthread_mutex_lock(&ChairAccess);
if(ChairsCount == 0)
{
//if chairs are empty, break the loop.
pthread_mutex_unlock(&ChairAccess);
break;
}
//TA gets next student on chair.
sem_post(&ChairsSem[CurrentIndex]);
ChairsCount--;
printf("Student left his/her chair. Remaining Chairs %d\n", 3 - ChairsCount);
CurrentIndex = (CurrentIndex + 1) % 3;
pthread_mutex_unlock(&ChairAccess);
// unlock
printf("\t TA is currently helping the student.\n");
sleep(5);
sem_post(&Student_Sem);
usleep(1000);
}
}
}
void *Student_Activity(void *threadID)
{
int ProgrammingTime;
while(1)
{
printf("Student %ld is doing programming assignment.\n", (long)threadID);
ProgrammingTime = rand() % 10 + 1;
sleep(ProgrammingTime); //Sleep for a random time period.
printf("Student %ld needs help from the TA\n", (long)threadID);
pthread_mutex_lock(&ChairAccess);
int count = ChairsCount;
pthread_mutex_unlock(&ChairAccess);
if(count < 3) //Student tried to sit on a chair.
{
if(count == 0) //If student sits on first empty chair, wake up the TA.
sem_post(&TA_Sleep);
else
printf("Student %ld sat on a chair waiting for the TA to finish. \n", (long)threadID);
// lock
pthread_mutex_lock(&ChairAccess);
int index = (CurrentIndex + ChairsCount) % 3;
ChairsCount++;
printf("Student sat on chair.Chairs Remaining: %d\n", 3 - ChairsCount);
pthread_mutex_unlock(&ChairAccess);
// unlock
sem_wait(&ChairsSem[index]); //Student leaves his/her chair.
printf("\t Student %ld is getting help from the TA. \n", (long)threadID);
sem_wait(&Student_Sem); //Student waits to go next.
printf("Student %ld left TA room.\n",(long)threadID);
}
else
printf("Student %ld will return at another time. \n", (long)threadID);
//If student didn't find any chair to sit on.
}
}