Project
CS 5500 Advanced Operating Systems
Programming Project
by Hyungbae Park
Main function
• int main(int argc, char *argv[]) { // Initialize your variables
// Create TA thread sleep(1); for(i=0;i<MAX;i++) {
sleep(1); // Create students threads
}
for(j=0;j<MAX;j++) { // Join all the threads
} }
Semaphore wait() function
• void wait(int sema, int param) {
while(sema == 0) {} if(param == 0)
printf("[SEMAPHORE] TA gets the lock!\n");
else printf("[SEMAPHORE] Student %d gets
the lock!\n",param); sema--;
}
Semaphore signal() function
• void signal(int sema, int param) {
if(param == 0) printf("[SEMAPHORE] TA releases the
lock!\n"); else
printf("[SEMAPHORE] Student %d releases the lock!\n",param);
sema++; }
Thread sleepingta() function
• void *sleepingta(void *param) { // Wait & get the lock printf("[TA THREAD] TA is in the office.\n"); // Release the lock while(ENDOFPROGRAM) {
// Add your logic } // End thread
}
Thread student() function
• void *student(void *param) { // Wait & get the lock printf("[STUDENT THREAD] Student %d is
coming!\n",*(int*)param);
// Add your logic
// Release the lock // End thread
}
Variables
• #include <pthread.h> #include <stdio.h>
#define SLEEP 0 #define WAKEUP 1
int semaphore; int MAX, TA, TTL; int ENDOFPROGRAM; int curstudent; int waitinglist;
Flowchart - main()
MAIN START
Create TA thread (TA is sleeping)
Get user input (Number of students)
Create student threads
MAIN END
Flowchart – sleepingta()
TA THREAD START
TA is sleeping
Student wakes TA up
No students in 10 seconds
Help the student
Waiting students
TA THREAD END
YesNo
Yes
No
Flowchart – student()
STUDENT THREAD START
Student wakes TA up
Thee waiting students
Seat on the chair and wait
Helped by TA
STUDENT THREAD END
Is TA sleeping
Yes
No
Yes No
Results
• [SEMAPHORE] TA gets the lock! [TA THREAD] TA is in the office. [SEMAPHORE] TA releases the lock! [TA THREAD] Waiting students = 0, Student in the office = 0 [SEMAPHORE] Student 1 gets the lock! [STUDENT THREAD] Student 1 is coming! [STUDENT THREAD] Waiting students = 0, Student in the office = 1 [STUDENT THREAD] Student 1 wakes up TA and gets help. [SEMAPHORE] Student 1 releases the lock! [SEMAPHORE] Student 2 gets the lock! [STUDENT THREAD] Student 2 is coming! [STUDENT THREAD] Student 2 is seating on the waiting chair #1. [STUDENT THREAD] Waiting students = 1, Student in the office = 1 [SEMAPHORE] Student 2 releases the lock! [TA THREAD] TA is helping a student... [SEMAPHORE] Student 3 gets the lock! [STUDENT THREAD] Student 3 is coming! [STUDENT THREAD] Student 3 is seating on the waiting chair #2. [STUDENT THREAD] Waiting students = 2, Student in the office = 1 [SEMAPHORE] Student 3 releases the lock! [SEMAPHORE] Student 4 gets the lock! [STUDENT THREAD] Student 4 is coming! [STUDENT THREAD] Student 4 is seating on the waiting chair #3. [STUDENT THREAD] Waiting students = 3, Student in the office = 1 [SEMAPHORE] Student 4 releases the lock!
Results – cont'd
• [SEMAPHORE] Student 5 gets the lock! [STUDENT THREAD] Student 5 is coming! [STUDENT THREAD] Student 5 is waiting. [TA THREAD] TA is done with the student! [TA THREAD] Waiting students = 2, Student in the office = 1 [TA THREAD] TA is helping a student... [STUDENT THREAD] Student 5 is seating on the waiting chair #3. [STUDENT THREAD] Waiting students = 3, Student in the office = 1 [SEMAPHORE] Student 5 releases the lock! [TA THREAD] TA is done with the student! [TA THREAD] Waiting students = 2, Student in the office = 1 [TA THREAD] TA is helping a student... [TA THREAD] TA is done with the student! [TA THREAD] Waiting students = 1, Student in the office = 1 [TA THREAD] TA is helping a student... [TA THREAD] TA is done with the student! [TA THREAD] Waiting students = 0, Student in the office = 1 [TA THREAD] TA is helping a student... [TA THREAD] TA is done with the student! [TA THREAD] Waiting students = 0, Student in the office = 0
Submission
• It should be YOUR OWN WORK
• Please submit your original source code! – DO NOT copy & paste your source code to any text
editors
– If you submit non-compilable files, then you will get zero for the project
• Submission link is available on Blackboard
• Extra credit (+5) for the submission by 6/11 11:59 pm