Exam Day (synchronization of threads using semaphores) Due Date: May 20th Using
ID: 3837957 • Letter: E
Question
Exam Day (synchronization of threads using semaphores) Due Date: May 20th Using Java programming, synchronize the threads in the context of the problem Closely follow the implementation requirements. The symchronization should be implemented through Java semaphores and operations on semaphores (acquire and release) As semaphore constructor use ONLY Semaphore (int permits) Creates a Semaphore with the given number of permits and nonfair fairness setting As methods use ONLY: acquire release You can also use 0; getQueueLength0 Returns an estimate of the number of threads waiting to acquire has Queued Threads Queries whether any threads are waiting to acquire DO NOTUSE ANY OE THE OTHER METH0DS of thesemaphores class, besides the ones mentioned above Any wait must be implemented using P(semaphores (acquire any shared variable must be protected by a mutexsemaphore 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 implementation You should keep the concurrency of the threads as high as possible, however the access to shared structures has to be done in a Mutual Exclusive fashion, using a mutex semaphore Many of the activities can be simulated using the sleep (of a random time method Use appropriate System.out.println() statements to reflect the time of each particular action done by aspecific thread. This is necessary for us to observe howthe symchronization is working.Explanation / Answer
I have created and developed the semaphore system for the students. I have added the comments for each part of the section and the final output.
Let me explain you in more step-by-step procedure:-
Step-1:
// This is the header files in Java for embedding the methods and properties of each class
import java.util.*;
private static int numStudents = DefineConstants.NUM_STUDENTS;
private static Semaphore studentsLock = new Semaphore();
// The main() method is the starting point of the program
private static void Main(void argc, String[] args)
{
// Declare the different variables
int studentsVal;
String student_name = new String(new char[32]);
boolean classroomTime = (argc == 2 && (strcmp(args[1], "-v") == 0));
InitThreadPackage(classroomTime);
studentsLock = SemaphoreNew("The name of the students grouping together are: ", 1);
// The for loop uses the students value to be noted
for (studentsVal = 0; studentsVal < DefineConstants.CLASS_CAPACITY; studentsVal++)
{
student_name = String.format("The student leave value is #%d", studentsVal);
ThreadNew(student_name, studentsSize, 0);
}
RunAllThreads();
SemaphoreFree(studentsLock);
System.out.print(" The instructions will get terminated after all students leave ");
}
private static void studentsSize()
{
boolean studentsResult = false;
int instructorLeaveThread = 0;
while (!studentsResult)
{
studentsRandomNum(10, 100);
SemaphoreWait(studentsLock);
if (numStudents == 0)
{
studentsResult = true;
}
else
{
numStudents--;
instructorLeaveThread++;
System.out.printf("%s The No. of students present in the classroom are", ThreadName(), numStudents);
}
SemaphoreSignal(studentsLock);
}
System.out.printf("%s All the students left from the classroom ", ThreadName(), instructorLeaveThread);
}
private static void studentsRandomNum(int timeDurationMicroSecs, int timeDurationSecs)
{
int gradeObj;
int studentRanging = timeDurationSecs - timeDurationMicroSecs;
PROTECT(gradeObj = random());
ThreadSleep(timeDurationMicroSecs + gradeObj % studentRanging);
}
final class DefineConstants
{
public static final int NUM_STUDENTS = 14;
public static final int CLASS_CAPACITY = 8;
public static final int GROUP_SIZE = 3;
}
package tangible;
// Created a RandomNumbers class for generating random numbers.
public final class RandomNumbers
{
private static Random r;
public static int nextNumber()
{
if (r == null)
Seed();
return r.nextInt();
}
public static int nextNumber(int ceiling)
{
if (r == null)
Seed();
return r.nextInt(ceiling);
}
public static void seed()
{
r = new Random();
}
public static void seed(int seed)
{
r = new Random(seed);
}
}
The semaphore is like an integer which is used to increase and decrease it.
Program:-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.