Reputation: 2135
I am creating a process with 2 children, 1 of the children is responsible to read questions (line by line from a file), output every question and reading the answer, and the other one is responsable to measure the time elapsed and notify the user at each past 1 minute about the remaining time. My problem is that i couldn't find any useful example of how i can make this set time function to work. Here is what i have tried so far. The problem is that it outputs the same elapsed time every time and never gets out from the loop.
#include<time.h>
#define T 600000
int main(){
clock_t start, end;
double elapsed;
start = clock();
end = start + T;
while(clock() < end){
elapsed = (double) (end - clock()) / CLOCKS_PER_SEC;
printf("you have %f seconds left\n", elapsed);
sleep(60);
}
return 0;
}
Upvotes: 1
Views: 306
Reputation: 1
As I commented, you should read the time(7) man page.
Notice that clock(3) measure processor time, not real time.
I suggest using clock_gettime(2) with CLOCK_REALTIME
(or perhaps CLOCK_MONOTONIC
). See also localtime(3) and strftime(3).
Also timer_create(2), the Linux specific timerfd_create(2) and poll(2) etc... Read also Advanced Linux Programming.
If you dare use signals, read carefully signal(7). But timerfd_create
and poll
should probably be enough to you.
Upvotes: 2
Reputation: 4821
Here is something simple that seems to work:
#include <stdio.h>
#define TIME 300
int main()
{
int i;
for (i=TIME; i > 0; i--)
{
printf("You have [%d] minutes left.\n", i/60);
sleep(60);
}
}
Give it a try.
Upvotes: 0