Here is a highly over-simplified way of doing your assignment....
Code:
#include <windows.h>
#include <time.h>
//use only one of these streams
#include <iostream> //If you're using C++
#include <stdio.h> //If you're just using C
//my preference instead of either iostream or stdio...
#include <conio.h>
bool processorBusy = false;
int jobOne[5] = {0, 0, 0, 0, 0};
int jobTwo[5] = {0, 0, 0, 0, 0};
int percentCompleted[5] = {0, 0, 0, 0, 0};
int OneActiveThreads = 0, TwoActiveThreads = 0;
int whichJob = 0;
int wait=0;
int kp=0;
int i=0, n, aRandomNumber;
while (kp == 0){
//seed the random number generator for each time a pass is made
//through all the threads in either job. This ensures the numbers you
//generate are as random as possible.
if (i == 0)
srand(time(NULL));
if (jobOne[i] == 0){
//generate a random number between 1 and 50
aRandomNumber = 1 + (rand() % 50);
if (aRandomNumber % 2 == 1){ //if the number is odd
jobOne[i] = aRandomNumber;
OneActiveThreads++;
}
}
if (jobTwo[i] == 0){
//generate a random number between 1 and 50
aRandomNumber = 1 + (rand() % 50);
if (aRandomNumber % 2 == 0) //if the number is even
jobTwo[i] = aRandomNumber;
}
//if the random number in JobOne's current thread is less than that
//of JobTwo....JobOne goes first on the processor
if (jobOne[i] <= jobTwo && jobOne[i] != 0){
processorBusy = true;
whichJob = 1;
}
//...otherwise JobTwo gets the processor
else if (jobTwo[i] != 0) {
processorBusy = true;
whichJob = 1;
}
//Inner loop, that increments the completeness of each active thread
for (n = 0; n <= 5; n++){
if (whichJob == 1){
//if a thread is waiting to be executed, and JobTwo isn't waiting
//too long....
if (jobOne[n] > 0 && wait < 100){
printf("Job1: Thread %d % completed", percentCompleted);
percentCompleted[n] += 10;
//When a thread has completed, nullify it's value so that it isn't
//incremented again
if (percentCompleted[n] == 100){
jobOne[n] = 0;
OneActiveThreads--;
}
//If there are threads in the other job in a waiting state
//increment wait
if (TwoActiveThreads > 0)
wait++;
//If all the threads in this job have completed, exit the FOR
//loop structure... this allows the system to decide which job
//goes next
if (OneActiveThreads == 0){
//if there are threads one the other side waiting for
//execution, give them a chance to...
if (wait > 0)
whichJob = 2;
else{
//otherwise, let the system decide who goes next
whichJob = 0;
processorBusy = false;
}
//exit the for loop structure and reset wait...
wait = 0;
break;
}
}// if (jobOne)
}// if (whichJob)
else if (whichJob == 2){
//if a thread is waiting to be executed, and JobOne isn't waiting
//too long....
if (jobTwo[n] > 0 && wait < 100){
printf("Job2: Thread %d % completed", percentCompleted);
percentCompleted[n] += 10;
//When a thread has completed, nullify it's value so that it isn't
//incremented again, additionally, reset the percent completed
//for that thread.
if (percentCompleted[n] == 100){
jobTwo[n] = 0;
TwoActiveThreads--;
percentCompleted[n] = 0;
}
//If there are threads in the other job in a waiting state
//increment wait
if (OneActiveThreads > 0)
wait++;
//If all the threads in this job have completed, exit the FOR
//loop structure... this allows the system to decide which job
//goes next
if (TwoActiveThreads == 0){
//if there are threads one the other side waiting for
//execution, give them a chance to...
if (wait > 0)
whichJob = 1;
else{
//otherwise, let the system decide who goes next
whichJob = 0;
processorBusy = false;
}
//exit the for loop structure and reset wait...
wait = 0;
break;
}
}// if (jobTwo)
}//for
//Sleep for 400 oseconds so that you can
//actually SEE the simulation running
Sleep(400);
//increment i so the system can determine if the next thread in
//either job becomes activated. There are only 5 threads in each
//job, so i can only be a value between 0 and 4.
if (i < 4)
i++;
else
i = 0;
//passively check if the user pressed escape. This does not wait for
//input, rather it checks the keyboard register to see if the escape
//key is held down. This is actually executed in a separate thread
//that is managed by Windows.
kp = GetAsyncKeyState(VK_ESCAPE);
}
Please bear in mind that this is SAMPLE code. That means it probably won't compile without errors and it probable has some minor logic flaws. So don't think you can copy / paste and submit for your assignment. ;D This code is just meant to guide you along the right path. In the end, you have to come up with your own code. Hopefully, this code will be of significant help for you.
Again I stress, that there are MANY ways to implement semaphore emulation. This is just ONE of them.
I must ask though, which Institution are you at? UWI? UTECH? IMP? Where?