-2

So what I wanted to make is a timer that won't do anything for 25 minutes and then play an mp3 file as an alarm. Only solution I've come across is using the sleep() function, but I don't know if it'll cause any problems due to running for 25 minutes (I see it being used for 3 seconds not 25 * 60). I don't if using it for this long is too taxing on the system or inefficient.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    size_t sessions;
    puts("Enter number of sessions to work: ");
    scanf("%u" , &sessions);

    int work_time  = 25*60; //25 minutes to seconds
    int break_time = 5 *60; //5  minutes to seconds

    for(int i = 1 ; i <= sessions ; i++)
    {
        sleep(work_time);
        printf("Have a 5 minute break...You deserve it :)\n");

        sleep(break_time);
        puts("Break is over, let's get shit done\n\n");
    }

    //next line isn't probably the best way to do it but it works for now
    system("start wmplayer C:\\Users\\me\\Desktop\\myuser\\english\\fartingnoise.mp3");

    return 0;
}

Here's my code so far, my main question is about the sleep function but any criticism of my code is welcome.

  • 4
    [`sleep`](https://linux.die.net/man/3/sleep) accepts an `unsigned int` as a parameter. Regardless of your environment, 25*60 = 1500 should easily fit in an `unsigned int`. Your system will be fine with this. – yano Sep 06 '17 at 22:50
  • 3
    There's existing utilities for doing this. On Unix there's [`at`](https://en.wikipedia.org/wiki/At_(Unix)). On Windows there is [Task Scheduler](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383614(v=vs.85).aspx). I'd suggest using that instead if this is a practical exercise. Otherwise there's no problem sleeping for that long, but it will not survive a restart or that process getting killed. – Schwern Sep 06 '17 at 22:50
  • 2
    Sleeping processes use no CPU resource while asleep. It is simple. The main risk is being swapped out and a delay of a few seconds after the sleep ends. You'll have to measure whether that is a real problem these days. – Jonathan Leffler Sep 06 '17 at 22:51
  • @Schwern okay great I'll check it out. – BareWithImANoob Sep 06 '17 at 22:55
  • @JonathanLeffler I don't mind if it's delayed for a couple of seconds. I may be wrong about this but I've read something about it maybe not executing at all. If it's true could you explain why. – BareWithImANoob Sep 06 '17 at 22:56
  • 1
    `size_t sessions; ... scanf("%u" , &sessions);` --> wrong specifier/type and implies compiler warnings not fully enabled. – chux - Reinstate Monica Sep 06 '17 at 23:09
  • 1
    Consider a helper function that loops: sleeps 1/2 the needed time and then 1/4, 1/8. 1/16 , etc. After waking each time, it notices the true wall time via `time()` and recalculates the remaining sleep time needed. Other non-standard functions provide a more robust solution. – chux - Reinstate Monica Sep 06 '17 at 23:14
  • @chux (https://stackoverflow.com/questions/2125845/platform-independent-size-t-format-specifiers-in-c) I tried using `zu` and `zd` as mentioned in the link but the for loop would loop for more than expected. – BareWithImANoob Sep 06 '17 at 23:30
  • 1
    Since you've not given a link to where you read "something about it maybe not executing at all", it is hard to know whether you misinterpreted what some site said or the site was wrong. While the process is asleep for 25 minutes, it is not doing anything else. It is asleep, and it stays asleep, and the kernel ensure that it stays asleep and is only woken after the 25 minutes have elapsed. Sleeping processes are the most economical ones (apart from those that are dead); they consume the least CPU, and the only cost is the cost of skipping over the entry in the process lists in the kernel. – Jonathan Leffler Sep 06 '17 at 23:31
  • Thanks a lot @chux I think I'll actually implement your solution :) – BareWithImANoob Sep 06 '17 at 23:32
  • @JonathanLeffler I guess I have misinterpreted it, thanks for the clear explanation! – BareWithImANoob Sep 06 '17 at 23:39
  • 2
    After the 25 minutes, (with a dither of a few tens ms), is up, your sleepy thread will become ready to run. If there is a free core available, or if it can preempt another running thread of lower priority, then it will be set running 'immediately'. If the box is overloaded with more higher-priority ready/running threads than cores, then it will have to wait until a core becomes available for some reason or another. Certainly, with a Windows box that is not grossly overloaded, a thread sleeping for an hour wakes up after 3600 seconds 'exactly' if timed with a manual stopwatch:) – Martin James Sep 07 '17 at 00:12
  • 1
    The system does matter. `sleep()` in Unix uses seconds, `Sleep()` in Windows uses milliseconds. This is not a standard function. You need to include the non-standard library where the function is located, such as `#include `. – Lundin Sep 07 '17 at 08:46

1 Answers1

1

No, there is no problem with sleeping for 25 minutes.
In fact, using sleep is about the most efficient way to wait for 25 minutes.

user253751
  • 45,733
  • 5
  • 44
  • 76