How does an alarm clock application work?

0

My phone is dead and I don't have an alarm clock. Here was my solution (in Bash) to ensure I will wake up tomorrow morning

while [ 1 ]; do
  TIME=$(date +'%H'):$(date +'%M')
  echo $TIME
  if [ $TIME == "06:15" ];then
    osascript -e "set Volume 3"
    open /Path/To/MyFavouriteSong.mp3
    break
  fi
  sleep 58
done

This code works well (on Mac OSX 10.12.2 and as long as my computer does not go to sleep) but it feels somewhat silly.

It is unclear to me how does the CPU do to measure a certain time. I don't know if typically an alarm clock app tests every 58 seconds (or other amount of time closer but still shorter than one minute) if it is already time to sound the alarm or if there is some mechanism for the CPU to send a signal to the program at a specific time.

Remi.b

Posted 2017-02-05T21:40:04.310

Reputation: 2 431

Question was closed 2017-02-09T17:18:36.457

I see a close vote because "This question is not about computer hardware or software, within the scope defined in the help center.". Would the question be a better fit on StackOverflow or elsewhere? – Remi.b – 2017-02-05T21:45:40.790

Perhaps take a look at how cron daemons are implemented? – user1686 – 2017-02-05T21:47:56.663

The close vote was mine. This might be a borderline question, so I wanted to let potential closure be organic rather than post a comment and influence other voters. My reasoning is that programming questions are off-topic, and how any/all alarm clock programs work is overly broad. I don't really know which would be the best site for this, so I didn't suggest one. SO seems logical, but I'm not much of a user of that site so not sure how well this fits there. If this comment attracts feedback that this is on-topic here, I'll retract the close vote. – fixer1234 – 2017-02-05T22:10:42.577

Use a cron job. – DavidPostill – 2017-02-06T12:57:43.120

@fixer1234 IMHO the core of the question is how does the CPU do to measure a certain time? and I consider that on-topic. Some editing to focus the question would help though. – I say Reinstate Monica – 2017-02-06T18:14:48.770

Answers

3

Here https://unix.stackexchange.com/questions/64191/how-do-the-internals-of-the-cron-daemon-work is explained (on the example of cron) how the you could implement a program, that does something every X seconds/minutes. It is done similar to your approach.

If your question was, how the cpu knows the current time: Usually there is a hardware clock sitting on the Mainboard, working the same way as a common quartz wristwatch, which can communicate to the cpu. The cpu can ask these clock every ms or whatever, what the exact time is. This is the reason why there is usually a coin cell battery sitting on the mainboard: To keep the hardware clock running.

Jounathaen

Posted 2017-02-05T21:40:04.310

Reputation: 397