Pthreads and Semaphores Assignment Help
Budget: €8 – €30 EUR
Every morning, students studying in Heraklion go to the stop (Stop A) and wait for the bus to come to go to their school.
Each student can belong to:
● In Mathematics
● In Physics
● In Chemistry
● In the CSD
In addition, each student will sit in the faculty to attend their courses and read for a certain period of time separate for each one. Once he reaches the school stop (Stop B), enters his school and this time passes, he heads back again to the Campus bus stop and waits again for the bus to go back home.
The bus will start from stop A (where the students will also be initialised), and will only start for the University when there are no other people at the stop, or it cannot pick up other people according to the rules explained below. There is room for N people in the bus. In addition, to ensure equality between students from different schools, the bus can accommodate up to (N/4) students from each school. This means that for N = 20, if there are 5 Physics students in the bus, and the next student who wants to enter is from Physics, he will not be able to enter, even if there are empty seats from the other departments. (Each student thread must check if it has space on the bus according to the department it belongs to.) Also, after this bus leaves one stop to reach the next, it needs a time interval (T) of seconds.
Write C code that allows students to be transported to the Campus and back to their home according to the above rules, using semaphores to synchronize them.
The solution should:
● Uses a different thread for each student.
● Uses a thread for the bus.
● Avoids starvation (no student waits forever in
some attitude).
● Follows the priority order of students (FIFO). Students must board the bus in the order they arrived at the bus stop (if they are allowed to board the bus based on the previous criteria) Ends when all students arrive home
Student initiation
1. Initialization of student-threads includes assignment of registration number (AM)
2. Set the study time of the students randomly from 5 to 15 seconds. You can use the random(3) library for convenience.
3. At any moment, students can be:
● At the (Stop A)
● On the bus (Bus)
● At the school (to simplify the exercise, we consider that the school space is the same for everyone and not every student goes to a different school, even if he belongs to a different school) (University)
● At the Campus stop (Stop B)
Bus initialization
The bus holds up to N students, N/4 from each school, and is initially empty. If the bus arrives at a stop which is empty, it will leave for the next stop according to the times you have set for it even if it is empty.
School initiation
The school can accommodate an unlimited number of people, regardless of school, and it is empty at the beginning. The total students who want to read must be up to 200 and will be given at initialization as entry to the program.
Notes
● You can use the sleep(1) library for convenience.
● It is important to print the appropriate messages that will show the
status of each stop, bus and school whenever:
○ a student-thread is created
○ when a student reaches a stop
○ when a student enters the bus
○ when a student enters the faculty
○ when a student arrives home
○ when a student does not fit on the bus and has to wait at the stop.
● Recommended variable initializations:
○ T: 10 sec
○ N: 12
○ Bus waiting time at each stop : 3 sec (Optional)
○ Number of students (input) > 12
● In general try to control scripts that reach the limits of your structures. If for example the bus will take 12 people in total (3 from each section), at stop A there should be >12 people waiting in order to be able to check the full functionality of the exercise
Example of execution with parameters (Ν=4, Number of
students = 5)
See attached screenshots
Each student can belong to:
● In Mathematics
● In Physics
● In Chemistry
● In the CSD
In addition, each student will sit in the faculty to attend their courses and read for a certain period of time separate for each one. Once he reaches the school stop (Stop B), enters his school and this time passes, he heads back again to the Campus bus stop and waits again for the bus to go back home.
The bus will start from stop A (where the students will also be initialised), and will only start for the University when there are no other people at the stop, or it cannot pick up other people according to the rules explained below. There is room for N people in the bus. In addition, to ensure equality between students from different schools, the bus can accommodate up to (N/4) students from each school. This means that for N = 20, if there are 5 Physics students in the bus, and the next student who wants to enter is from Physics, he will not be able to enter, even if there are empty seats from the other departments. (Each student thread must check if it has space on the bus according to the department it belongs to.) Also, after this bus leaves one stop to reach the next, it needs a time interval (T) of seconds.
Write C code that allows students to be transported to the Campus and back to their home according to the above rules, using semaphores to synchronize them.
The solution should:
● Uses a different thread for each student.
● Uses a thread for the bus.
● Avoids starvation (no student waits forever in
some attitude).
● Follows the priority order of students (FIFO). Students must board the bus in the order they arrived at the bus stop (if they are allowed to board the bus based on the previous criteria) Ends when all students arrive home
Student initiation
1. Initialization of student-threads includes assignment of registration number (AM)
2. Set the study time of the students randomly from 5 to 15 seconds. You can use the random(3) library for convenience.
3. At any moment, students can be:
● At the (Stop A)
● On the bus (Bus)
● At the school (to simplify the exercise, we consider that the school space is the same for everyone and not every student goes to a different school, even if he belongs to a different school) (University)
● At the Campus stop (Stop B)
Bus initialization
The bus holds up to N students, N/4 from each school, and is initially empty. If the bus arrives at a stop which is empty, it will leave for the next stop according to the times you have set for it even if it is empty.
School initiation
The school can accommodate an unlimited number of people, regardless of school, and it is empty at the beginning. The total students who want to read must be up to 200 and will be given at initialization as entry to the program.
Notes
● You can use the sleep(1) library for convenience.
● It is important to print the appropriate messages that will show the
status of each stop, bus and school whenever:
○ a student-thread is created
○ when a student reaches a stop
○ when a student enters the bus
○ when a student enters the faculty
○ when a student arrives home
○ when a student does not fit on the bus and has to wait at the stop.
● Recommended variable initializations:
○ T: 10 sec
○ N: 12
○ Bus waiting time at each stop : 3 sec (Optional)
○ Number of students (input) > 12
● In general try to control scripts that reach the limits of your structures. If for example the bus will take 12 people in total (3 from each section), at stop A there should be >12 people waiting in order to be able to check the full functionality of the exercise
Example of execution with parameters (Ν=4, Number of
students = 5)
See attached screenshots