Program Semaphores: Write Monitor Alarm Clock Delay Time Units

  • Thread starter Thread starter prashantgolu
  • Start date Start date
  • Tags Tags
    Program
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 8K views
prashantgolu
Messages
50
Reaction score
0
Write a monitor that implements an alarm clock that enables a calling program to delay itself for a specifed number of time units (ticks). You may assume the existence of a real hardware clock that invokes a procedure "tick" in your monitor at regular intervals.

the solution is :

monitor AlarmClock

int now=0;
condition wakeup;


wakeme(int n){
int alarm;
alarm = now + n;
while (now < alarm) wakeup.wait(alarm);
wakeup.signal;
}


tick() {
now = now + 1;
wakeup.signal;
}

Why Wakeup.signal in wake me function...i think the last wakeup signal in tick() will wakeup the process...
 
Physics news on Phys.org
if there is another process that can be woke up at the same tick
 
What library are you using and what language is that? It looks like C, but has some glaring syntax problems.

Sounds like you want a simple message handling system. You will need two threads to properly demonstrate it: one that's doing the waiting, and the one that's looping through ticks until it can trigger the callback.
 
The solution doesn't handle wrap around, and tick should use a separate signal. An example that does:

int now=0;
condition wakeup;
condition ticksleep;

wakeme(int n){
int alarm;
int then;
then = now;
while((now - then) < n) wakeup.wait(ticksleep);
wakeup.signal;
}

tick() {
now = now + 1;
ticksleep.signal;
}