Santa Claus sleeps in his shop up at the North Pole , and can only be wakened by either all nine reindeer being back from their year long vacation on the beaches of some tropical island in the South Pacific , or by some elves who are having some difficulties making the toys . One elfs problem is never serious enough to wake up Santa ( Otherwise , he may never get any sleep ) , so , the elves visits Santa in a group of three . When three elves are having their problems solved , any other elves withing to visit Santa must wait for those elves to return . If Santa wakes up to find three elves waiting at his shops door , along with the last reindeer having come back from the tropics , Santa has decided that the elves can wait until after Christmas , because it is more important to get his sleigh ready as soon as possible . ( It is assumed that the reindeer dont want to leave the tropics , and therefor they stay there until the last possible moment . They might not even come back , but since Santa is footing the bill for their year in paradise this could also explain the quickness in their delivering of presents , since the reindeer can't wait to get back to where it is warm . ) The penalty for the last reindeer to arrive is that it must get Santa while the others wait in a warming hut before being harnessed to the sleigh .
Here are some additional specifications :
After the ninth reindeer arrives , Santa must invoke prepareSleigh , and then all nine reindeer must invoke getHitched .
After the third elf arrives , Santa must invoke helpElves . Concurrently , all three elves should invoke getHelp .
All three elves must invoke get Help before any additional elves enter ( increment the elf counter ) .
Santa should run in a loop so he can help many sets of elves . We can assume that there are exactly 9 reindeer , but there may be any number of elves .
Message ordering : ensuring that events happen in order at the right time . Priority : ensuring that the group of Reindeer have priority over any Elf groups that may be waiting at the time . Self - Organization : Santa cannot marshal a group of Elves or Reindeer , these groups must organize among themselves without help from a Santa thread or process . Synchronization : synchronization between various processes . The usual freedom from deadlock . livelock , and starvation .
how would I answer this question in c++? This was in a textbook I found in my school library. I was informed that we'd be doing a lot of operating system problems next semester and I'm trying to familiarize myself with as many concepts as possible.
I tried different types of code but they're always errors that I don't exactly understand or the code runs but shows no output.
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <semaphore.h>
using namespace std;
// Declare semaphores
sem_t santaSem, reindeerSem, elfTex, elfSem;
// Declare variables to keep track of number of reindeer and elves
int numReindeer = 0, numElves = 0;
// Function for Santa Claus thread
void santaClaus() {
while (true) {
// Wait for either all 9 reindeer or 3 elves to be present
sem_wait(&santaSem);
// Check if 9 reindeer are present
bool isReindeer = false;
sem_getvalue(&reindeerSem, &numReindeer);
if (numReindeer == 9) {
isReindeer = true;
cout << "Santa Claus is preparing the sleigh\n";
}
// Check if three elves are present
bool isElves = false;
sem_getvalue(&elfSem, &numElves);
if (numElves >= 3) {
isElves = true;
cout << "Santa Claus is helping the elves\n";
}
// Handle reindeer or elves depending on which is present
if (isReindeer) {
// Signal the reindeer to get hitched
for (int i = 0; i < 9; i++) {
sem_post(&reindeerSem);
}
// Reset the number of reindeer
numReindeer = 0;
} else if (isElves) {
// Signal the elves to get help
for (int i = 0; i < 3; i++) {
sem_post(&elfTex);
}
// Reset the number of elves
numElves = 0;
}
// Nap for a short while before checking again
this_thread::sleep_for(chrono::seconds(1));
}
}
// Function for reindeer threads
void reindeer(int id) {
while (true) {
// Wait for Santa to wake up
sem_wait(&reindeerSem);
// Get hitched to the sleigh
cout << "Reindeer " << id << " is getting hitched\n";
this_thread::sleep_for(chrono::seconds(1));
// Signal Santa when all nine reindeer are present
sem_getvalue(&reindeerSem, &numReindeer);
if (numReindeer == 9) {
sem_post(&santaSem);
}
}
}
// Function for elf threads
void elf(int id) {
while (true) {
// Wait to enter Santa's workshop
sem_wait(&elfTex);
// Get help from Santa
cout << "Elf " << id << " is getting assistance\n";
this_thread::sleep_for(chrono::seconds(1));
// Signal Santa when three elves are present
sem_post(&elfSem);
sem_getvalue(&elfSem, &numElves);
if (numElves == 3) {
sem_post(&santaSem);
}
}
}
int main() {
// Initialize semaphores
sem_init(&reindeerSem, 0, 0);
sem_init(&elfSem, 0, 0);
sem_init(&santaSem, 0, 0);
sem_init(&elfTex, 0, 1);
// Create Santa thread
thread santa(santaClaus);
// Create reindeer threads
thread reindeerThreads[numReindeer];
for (int i = 0; i < numReindeer; i++) {
reindeerThreads[i] = thread(reindeer, i);
}
// Create elf threads
thread elfThreads[numReindeer];
for (int i = 0; i < numReindeer; i++) {
elfThreads[i] = thread(elf, i);
}
// Join threads
santa.join();
for (int i = 0; i <numReindeer; i++) {
reindeerThreads[i].join();
}
for (int i = 0; i < numElves; i++) {
elfThreads[i].join();
}
// Destroy semaphores
sem_destroy(&reindeerSem);
sem_destroy(&elfSem);
sem_destroy(&santaSem);
sem_destroy(&elfTex);
return 0;
}
This is the code I tried. The output is showing nothing at all.
semaphore is signal only mechanism, whilst mutex has additionally a locking mechanism that only owner of locked resource can unlock it, so basically, you get a 2+ threads in a deadlock using a semaphores, which is not locked to owner thread thereby is going out of sync state and stuck in a mutual resource lock wait state.
basically, all reindeer threads wait on elf signal, and all elf threads wait for reindeer signal, thought all threads are in wait state and no one can resolve this issue cause there are no main thread what will initially set one of the wait states to signal state.
i assume on creation all semaphores are in wait state, that is why there are no output.