340 Project 2 – Summer 2021 The project must be done individually. No exceptions. You are asked to synchronize the threads of the following story using semaphores and operations on semaphores. Do NOT...

attached files



340 Project 2 – Summer 2021 The project must be done individually. No exceptions. You are asked to synchronize the threads of the following story using semaphores and operations on semaphores. Do NOT use busy waiting or other synchronization tools besides the methods (specified below) of the semaphore class. The grading is based on how correct and complete you implement the synchronization using semaphores and not on the fact that the program runs and have an output You have the following choices: 1. You can submit a pseudo-code implementation Or 2. You can submit a java-code implementation; in that case the project weight will be 8% instead of 6% (you get 1% EC on project 2’s weight). I DO NOT SUBMIT BOTH TYPES OF IMPLEMENTATION: EITHER YOU SUBMIT THE PSEUDO-CODE, OR YOU SUBMIT THE JAVA-CODE. Directions: Synchronize the students, coordinator, chairman threads in the context of the problem described below. Please refer to and read the project notes carefully, tips and guidelines before starting the project. Any synchronization between threads must be done through semaphores. No more interrupts, yield, set/get priority, join, or isAlive. Pseudo-code implementation Due Date: Sunday, June 27 Cutoff: June 28 (4pm) 5 points off for June 28 You are asked to synchronize the threads of the story using semaphores and operations on semaphores. Do NOT use busy waiting. Use pseudo-code similar to the one used in class (NOT java pseudo-code). Mention the operations that can be simulated by a fixed or random sleep_time. Your documentation should be clear and extensive. Explain the reason for each semaphore that you used in your implementation. Explain each semaphore type and initialization. Discuss the possible flows of your implementation. Deadlock is not allowed. Write your code on a .txt or .docx or .pdf document and upload it on Blackboard. No zip file. Each file name should contain your full name. 2. Java-code implementation Due Date: Sunday, June 27 Cutoff: June 29 (4pm) 4 points extra credit for June 27 5 points off for June 28 additional 6 points off for June 29 Using Java programming, synchronize the threads, in the context of the problem. Closely follow the implementation requirements. The synchronization should be implemented through Java semaphores and operations on semaphores (acquire and release) Keep in mind that semaphores are shared variables (should be declared as static) For semaphore constructors, use ONLY: Semaphore(int permits, boolean fair) Creates a Semaphore with the given number of permits and the given fairness setting. In methods use ONLY: acquire(), release(); You can also use: getQueueLength() Returns an estimate of the number of threads waiting to acquire. hasQueuedThreads() Queries whether any threads are waiting to acquire. https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html#Semaphore(int,%20boolean) DO NOT USE ANY OF THE OTHER METHODS of the semaphore’s class, besides the ones mentioned above. Any wait must be implemented using P(semaphores) (acquire). Any shared variable must be protected by a mutex semaphore such that Mutual Exclusion is implemented. Document your project and explain the purpose and the initialization of each semaphore. DO NOT use synchronized methods (beside the operations on semaphores). Do NOT use wait( ), notify( ) or notifyAll( ) as monitor methods. Whenever a synchronization issue can be resolved use semaphores and not a different type of implementation. Use appropriate System.out.println() statements to reflect the time of each particular action done by a specific thread. This is necessary for us to observe how the synchronization is working. Submission similar to project1. Name your project: YourLastname_Firstname_CS340_p2 Upload it on Blackboard.  Do not submit any code that does not compile and run. If there are parts of the code that contain bugs, comment it out and leave the code in. A program that does not compile nor run will not be graded.  Closely follow all the requirements of the project’s description.  The main method is contained in the main thread. All other thread classes must be manually created by either implementing the Runnable interface or extending the Thread class. Separate the classes into separate files (do not leave all the classes in one file, create a class for each type of thread). DO NOT create packages.  The project asks that you create different types of threads. There is more than one instance of a thread. No manual specification of each thread's activity is allowed (e.g. no Passenger5.goThroughTheDoor())  Add the following lines to all the threads you make: public static long time = System.currentTimeMillis(); public void msg(String m) { System.out.println("["+(System.currentTimeMillis()-time)+"] "+getName()+": "+m); }  It is recommended that you initialize the time at the beginning of the main method, so that it is unique to all threads. There should be output messages that describe how the threads are executing. Whenever you want to print something from a thread use: msg("some message about what action is simulated");  NAME YOUR THREADS. Here's how the constructors could look like (you may use any variant of this as long as each thread is unique and distinguishable): // Default constructor public RandomThread(int id) { setName("RandomThread-" + id); }  Design an OOP program. All thread-related tasks must be specified in their respective classes, no class body should be empty.  DO NOT USE System.exit(0); the threads are supposed to terminate naturally by running to the end of their run methods.  Javadoc is not required. Proper basic commenting explaining the flow of the program, self-explanatory variable names, correct whitespace and indentations are required. Setting up project/Submission: Name your project as follows: LASTNAME_FIRSTNAME_CSXXX_PY, where LASTNAME is your last name, FIRSTNAME is your first name, XXX is your course, and Y is the current project number. For example: Doe_John_CS340_p2 PLEASE UPLOAD ONLY THE JAVA FILES (SOURCE FILES) PLEASE UPLOAD YOUR FILE ON BLACKBOARD IN THE CORRESPONDING COLUMN. Mini - graduation in the Computer Science department the old fashion way Today is Graduation Day. There are numStudent graduating students and family members to attend. Students arrive at the college (implemented by sleep of random time). The family members are there too (implement this by having the student generate a random integer between 0 and 2 (0, 1. 2) and instantiate that number of family members. Note: you can name the family members as: student1family0, student1family1 or something similar. The other types of threads (coordinator, chairman) as well as students should be instantiated in main. The auditorium has numRows rows and each row has rowCapacity seats. Graduating students can take the first lower numRows/3 rounded up rows and family members will sit in the remaining, top rows. Students and family members will take a seat in the assigned seating area. Family members will take a seat in their designated area. (Note: you can consider the two designated areas as different resources with the number of instances being the number of available seats). If there is no seat left, the family member will wait (use semaphores) outside until the ceremony ends. Note: Students shouldn’t have this problem. The number of students should be less or equal to 1/3 numRows. Most of the faculty sits in front on the podium attending the ceremony and congratulating the graduated students. Two members of the faculty are more active: the chairman and the coordinator. Everything is so overwhelming and chaotic that seated family members will wait for the chairman to announce the end of the ceremony (use operations on sem). Students will wait for a signal from the coordinator. The last student to be seated (You might want to keep track of number of students) will signal the coordinator and chairman that the ceremony can start. The chairman gives a graduation speech (sleep of random time) and lets the coordinator know (signal) that he is ready to handout the diplomas. The coordinator will signal the students one by one to stand up. Next they will wait to be called by the chairman. The chairman will call (signal) them, one by one (you don’t need to worry about the order) handing out the diploma and taking a picture. Once a student is congratulated, he/she will leave the auditorium and wait for the ceremony to end. Once the last graduate student is congratulated and received the diploma, the chairman will announce (signal) the end of the ceremony. The chairman will invite (signal the students and family members) everyone for some refreshments and snacks on the second floor. To avoid any accidents, the access through the room (which can be simulated by sleep of random time) must be done in a mutual exclusion fashion. Once in, it will take some random time for each student to eat and talk to faculty members. After a while they will get bored and decide to leave. They will leave by parties (one party = one group represented by the student). Once the group is created they will move on and finally terminate. The chairman and coordinator will wait for the very last student or family member to leave such that they can also leave (terminate). Default values: numStudent = 13 rowCapacity = 4 numRows = 11
Jun 23, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here