I want to make a timer in C - c

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.

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.

Related

Is it possible to execute a condition only after a certain amount of time in C

I am wondering if it is possible to execute a piece of code only if a condition is true for a certain amount of time.
For example :
if(position_goal_reached == 1) //but this should be true for 1 second)
{do something;}
I don't think C is able to do that but I wanted to be sure.
I would recommend you to use time(), sleep(), clock() or usleep().
Note 1: Remember to include #include <unistd.h> or/and #include <time.h>
Note 2: There are some problems with clock() if you're using Unix-like Systems.
I think you wanna do the next:
#include <stdio.h>
#include <time.h>
int main(void)
{
clock_t start;
start = clock();
int milliseconds = 1000; // The amount of milliseconds that the conditional must take (This case 1 second)
if(position_goal_reached == 1)
{ start_again_if:
do something; //What you want to do must be here
if ((int)(clock() - start) < milliseconds ) goto start_again_if;
}
}
I hope this gonna be helpful.

How do I run a timer in C

I am making a trivia game and want to make a countdown timer where the person playing only has a certain time to answer the question. I am fairly new to C and am looking for a basic way to set a timer.
If you don't mind the person playing answering and being told they are too late, you could use something like this.
time.h gives us the ability to track processor clocks to time. It also gives us some nifty functions like double difftime(time_t timer2, time_t timer1) which returns the difference between two timers in seconds.
#include <stdio.h>
#include <time.h>
int main(void) {
time_t start_time;
time_t current_time;
time(&start_time);
time(&current_time);
double delay = 5;
int answer = 0;
double diff = 0;
while (diff < delay) {
diff = difftime(current_time, start_time);
scanf("%d", &answer);
printf("Nope not %d\n", answer);
time(&current_time);
}
printf("Too late\n");
return 0;
}
The only problem is scanf will lock the program and a reply will haft to be given before the loop stops. If this isn't what you are looking for, then you should look into using threads. Which is OS dependent.

Water Solution keep getting error message "did not find 12\n"

#include <cs50.h>
#include <stdio.h>
int main(void) {
printf("How many minutes do you shower for?\n");
int minutes = get_int();
int bottles = minutes * (192/16);
printf("Minutes: %i\n",minutes);
printf("Bottles: %i\n",bottles);
printf("%i minute equals %i bottles.\n", minutes, bottles);
do {
minutes = get_int();
}
while (minutes < 0);
}
This may be a simple problem, but I am very stuck right now and would appreciate the help!
When I use check50 to check the code this message keeps popping up:
:) water.c exists.
:) water.c compiles.
:) 1 minute equals 12 bottles.
:) 2 minute equals 24 bottles.
:( 5 minute equals 60 bottles.
timed out while waiting for program to exit
:( 10 minute equals 120 bottles.
timed out while waiting for program to exit
:) rejects "foo" minutes
:) rejects "" minutes
:) rejects "123abc" minutes

Run code for exactly one second

I would like to know how I can program something so that my program runs as long as a second lasts.
I would like to evaluate parts of my code and see where the time is spend most so I am analyzing parts of it.
Here's the interesting part of my code :
int size = 256
clock_t start_benching = clock();
for (uint32_t i = 0;i < size; i+=4)
{
myarray[i];
myarray[i+1];
myarray[i+2];
myarray[i+3];
}
clock_t stop_benching = clock();
This just gives me how long the function needed to perform all the operations.
I want to run the code for one second and see how many operations have been done.
This is the line to print the time measurement:
printf("Walking through buffer took %f seconds\n", (double)(stop_benching - start_benching) / CLOCKS_PER_SEC);
A better approach to benchmarking is to know the % of time spent on each section of the code.
Instead of making your code run for exactly 1 second, make stop_benchmarking - start_benchmarking the total run time - Take the time spent on any part of the code and divide by the total runtime to get a value between 0 and 1. Multiply this value by 100 and you have the % of time consumed at that specific section.
Non-answer advice: Use an actual profiler to profile the performance of code sections.
On *nix you can set an alarm(2) with a signal handler that sets a global flag to indicate the elapsed time. The Windows API provides something similar with SetTimer.
#include <unistd.h>
#include <signal.h>
int time_elapsed = 0;
void alarm_handler(int signal) {
time_elapsed = 1;
}
int main() {
signal(SIGALRM, &alarm_handler);
alarm(1); // set alarm time-out to 1 second
do {
// stuff...
} while (!time_elapsed);
return 0;
}
In more complicated cases you can use setitimer(2) instead of alarm(2), which lets you
use microsecond precision and
choose between counting
wall clock time,
user CPU time, or
user and system CPU time.

Timer\Counter in C for rate calculation?

Need a running (moving, rolling) average algorithm to calculate the 5-minute average bits that are passed in. All I have to work with is an accumulative value for the bits that are passed in.
For example: I start with 0 bits, 5 minutes later, I have 10 bits, so my average is 10 bits. 5 minutes later, I have 15 bits, so now my average is 7.5 bits. Another 5 minutes later, I have 30 bits, so my average now is 10.8 bits.
My question is, how can I implement a timer\counter in C++ so it would poll the bits value in exact 5 minutes intervals? Obviously I can't use delay 300 seconds. But can I make a timer in the background which would only fire an event (poll the bit value) every 5 minutes?
The Code to my Previous Answer
#define REENTRANT
//The above is neccessary when using threads. This must be defined before any includes are made
//Often times, gcc -DREENTRANT is used instead of this, however, it produces the same effect
#include <pthread.h>
char running=1;
void* timer(void* dump){
unsigned char i=0;
while(running){
for(i=0;i<300 && running;i++){
sleep(1);//so we don't need to wait the 300 seconds when we want to quit
}
if(running)
callback();//note that this is called from a different thread from main()
}
pthread_exit(NULL);
}
int main(){
pthread_t thread;
pthread_create(&thread,NULL,timer,NULL);
//do some stuff
running=0;
pthread_join(thread,NULL);//we told it to stop running, however, we might need to wait literally a second
pthread_exit(NULL);
return 0;
}
The "dumb" solution is to use POSIX threads. You can make a thread and then put it in an infinite loop with sleep() in it.

Resources