Program Semaphores: Write Monitor Alarm Clock Delay Time Units

  • Thread starter prashantgolu
  • Start date
  • Tags
    Program
In summary, the conversation discusses the implementation of an alarm clock monitor that allows a calling program to delay itself for a specified number of time units. It assumes the existence of a hardware clock that triggers a "tick" procedure at regular intervals. The solution involves using two threads and two conditions to handle waiting and triggering the callback function. However, it does not handle wrap around and can be improved.
  • #1
prashantgolu
50
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...
 
Technology news on Phys.org
  • #2
if there is another process that can be woke up at the same tick
 
  • #3
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.
 
  • #4
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;
}
 

What are program semaphores?

Program semaphores are synchronization mechanisms used in computer programming to control access to shared resources. They ensure that only one process can access a resource at a time, preventing conflicts and ensuring data integrity.

How do semaphores work?

Semaphores work by using two operations: wait and signal. The wait operation decreases the value of the semaphore, while the signal operation increases it. When the value is greater than 0, a process can access the resource. If the value is 0, the process must wait until the semaphore is signaled by another process.

What is a monitor in programming?

A monitor is a high-level synchronization construct that uses semaphores to manage access to shared resources. It allows multiple processes to safely access a shared resource without conflicts by using mutual exclusion and condition variables.

What is an alarm clock delay in programming?

An alarm clock delay is a feature in programming that allows a process to wait for a specific amount of time before continuing execution. This can be useful for controlling the timing of processes or implementing timeouts.

Why are time units important in programming?

Time units are important in programming because they allow for precise timing and control of processes. By using time units, programmers can ensure that processes are executed in a specific order and that they do not run for an indefinite amount of time, preventing potential issues such as deadlocks.

Similar threads

  • STEM Academic Advising
Replies
13
Views
2K
Back
Top