sys/time.h timer only runs once2019 Community Moderator ElectionJava Timer vs ExecutorService?Android timer? How-to?WPF Timer Like C# Timerfill shape with CPOSIX timers and POSIX signals handlingHow to reset the time to zero using time.h for a video game timer?How do I measure request and response times at once using cURL?Timer Task Only Runs Onceios timer only run onceTimer Run Once,Not continuous
Was World War I a war of liberals against authoritarians?
Can "few" be used as a subject? If so, what is the rule?
The English Debate
What is the reasoning behind standardization (dividing by standard deviation)?
pipe commands inside find -exec?
Animating wave motion in water
Why is indicated airspeed rather than ground speed used during the takeoff roll?
How to balance a monster modification (zombie)?
What is the difference between something being completely legal and being completely decriminalized?
Hot air balloons as primitive bombers
Justification failure in beamer enumerate list
Emojional cryptic crossword
Nested Dynamic SOQL Query
Weird lines in Microsoft Word
Can a university suspend a student even when he has left university?
Recursively updating the MLE as new observations stream in
Exit shell with shortcut (not typing exit) that closes session properly
Isn't the word "experience" wrongly used in this context?
What will the Frenchman say?
Fair way to split coins
When should a starting writer get his own webpage?
Do I need to convey a moral for each of my blog post?
Do I need an EFI partition for each 18.04 ubuntu I have on my HD?
Why didn’t Eve recognize the little cockroach as a living organism?
sys/time.h timer only runs once
2019 Community Moderator ElectionJava Timer vs ExecutorService?Android timer? How-to?WPF Timer Like C# Timerfill shape with CPOSIX timers and POSIX signals handlingHow to reset the time to zero using time.h for a video game timer?How do I measure request and response times at once using cURL?Timer Task Only Runs Onceios timer only run onceTimer Run Once,Not continuous
I am trying to implement a timer that continuously counts certain amount of time while the software is running. I wrote a dirty code to try how sys/time.h works.
My understanding is that if I set my it_interval struct to a non-zero value, then the timer should start counting again once it's done counting for the value stored in it_value struct.
However, my code stalls. Could someone tell me what I am missing in my code please? Also, I am using Linux.
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
int main(void)
struct itimerval timer1;
timer1.it_interval.tv_sec = 5;
timer1.it_interval.tv_usec = 0;
timer1.it_value.tv_sec = 5;
timer1.it_value.tv_usec = 0 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1)
for (int i = 0; i < 100000000000000; i++)
getitimer(ITIMER_REAL, &timer1);
printf( "end interval counter: %ld.%ldn",
timer1.it_interval.tv_sec, timer1.it_interval.tv_usec);
printf( "end value counter: %ld.%ldnn",
timer1.it_value.tv_sec, timer1.it_value.tv_usec );
return 0;
My output (shortened of course) is:
end interval counter: 5.0
end value counter: 0.8821
end interval counter: 5.0Alarm clock
Process returned 142 (0x8E) execution time: 5.033 s
Thank you in advance for your help!
c time timer
|
show 5 more comments
I am trying to implement a timer that continuously counts certain amount of time while the software is running. I wrote a dirty code to try how sys/time.h works.
My understanding is that if I set my it_interval struct to a non-zero value, then the timer should start counting again once it's done counting for the value stored in it_value struct.
However, my code stalls. Could someone tell me what I am missing in my code please? Also, I am using Linux.
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
int main(void)
struct itimerval timer1;
timer1.it_interval.tv_sec = 5;
timer1.it_interval.tv_usec = 0;
timer1.it_value.tv_sec = 5;
timer1.it_value.tv_usec = 0 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1)
for (int i = 0; i < 100000000000000; i++)
getitimer(ITIMER_REAL, &timer1);
printf( "end interval counter: %ld.%ldn",
timer1.it_interval.tv_sec, timer1.it_interval.tv_usec);
printf( "end value counter: %ld.%ldnn",
timer1.it_value.tv_sec, timer1.it_value.tv_usec );
return 0;
My output (shortened of course) is:
end interval counter: 5.0
end value counter: 0.8821
end interval counter: 5.0Alarm clock
Process returned 142 (0x8E) execution time: 5.033 s
Thank you in advance for your help!
c time timer
Perhaps it's got something to do with that lonely and unusedstruct sigaction sa;
you got there.
– PSkocik
Mar 7 at 18:56
Oops, I should've erased it. At this moment it's not in my code.
– HumbleBumble
Mar 7 at 19:01
@HumleBumble Actually, in it lies the answer to your question.
– PSkocik
Mar 7 at 19:01
time.c:20:27: warning: comparison is always true due to limited range of data type [-Wtype-limits]
have anything to do with it?
– JL2210
Mar 7 at 19:06
@JL2210 hmm I am not getting that warning, although my compiler setting has all Warnings enabled :(
– HumbleBumble
Mar 7 at 19:12
|
show 5 more comments
I am trying to implement a timer that continuously counts certain amount of time while the software is running. I wrote a dirty code to try how sys/time.h works.
My understanding is that if I set my it_interval struct to a non-zero value, then the timer should start counting again once it's done counting for the value stored in it_value struct.
However, my code stalls. Could someone tell me what I am missing in my code please? Also, I am using Linux.
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
int main(void)
struct itimerval timer1;
timer1.it_interval.tv_sec = 5;
timer1.it_interval.tv_usec = 0;
timer1.it_value.tv_sec = 5;
timer1.it_value.tv_usec = 0 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1)
for (int i = 0; i < 100000000000000; i++)
getitimer(ITIMER_REAL, &timer1);
printf( "end interval counter: %ld.%ldn",
timer1.it_interval.tv_sec, timer1.it_interval.tv_usec);
printf( "end value counter: %ld.%ldnn",
timer1.it_value.tv_sec, timer1.it_value.tv_usec );
return 0;
My output (shortened of course) is:
end interval counter: 5.0
end value counter: 0.8821
end interval counter: 5.0Alarm clock
Process returned 142 (0x8E) execution time: 5.033 s
Thank you in advance for your help!
c time timer
I am trying to implement a timer that continuously counts certain amount of time while the software is running. I wrote a dirty code to try how sys/time.h works.
My understanding is that if I set my it_interval struct to a non-zero value, then the timer should start counting again once it's done counting for the value stored in it_value struct.
However, my code stalls. Could someone tell me what I am missing in my code please? Also, I am using Linux.
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
int main(void)
struct itimerval timer1;
timer1.it_interval.tv_sec = 5;
timer1.it_interval.tv_usec = 0;
timer1.it_value.tv_sec = 5;
timer1.it_value.tv_usec = 0 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1)
for (int i = 0; i < 100000000000000; i++)
getitimer(ITIMER_REAL, &timer1);
printf( "end interval counter: %ld.%ldn",
timer1.it_interval.tv_sec, timer1.it_interval.tv_usec);
printf( "end value counter: %ld.%ldnn",
timer1.it_value.tv_sec, timer1.it_value.tv_usec );
return 0;
My output (shortened of course) is:
end interval counter: 5.0
end value counter: 0.8821
end interval counter: 5.0Alarm clock
Process returned 142 (0x8E) execution time: 5.033 s
Thank you in advance for your help!
c time timer
c time timer
edited Mar 7 at 19:14
HumbleBumble
asked Mar 7 at 18:53
HumbleBumbleHumbleBumble
1516
1516
Perhaps it's got something to do with that lonely and unusedstruct sigaction sa;
you got there.
– PSkocik
Mar 7 at 18:56
Oops, I should've erased it. At this moment it's not in my code.
– HumbleBumble
Mar 7 at 19:01
@HumleBumble Actually, in it lies the answer to your question.
– PSkocik
Mar 7 at 19:01
time.c:20:27: warning: comparison is always true due to limited range of data type [-Wtype-limits]
have anything to do with it?
– JL2210
Mar 7 at 19:06
@JL2210 hmm I am not getting that warning, although my compiler setting has all Warnings enabled :(
– HumbleBumble
Mar 7 at 19:12
|
show 5 more comments
Perhaps it's got something to do with that lonely and unusedstruct sigaction sa;
you got there.
– PSkocik
Mar 7 at 18:56
Oops, I should've erased it. At this moment it's not in my code.
– HumbleBumble
Mar 7 at 19:01
@HumleBumble Actually, in it lies the answer to your question.
– PSkocik
Mar 7 at 19:01
time.c:20:27: warning: comparison is always true due to limited range of data type [-Wtype-limits]
have anything to do with it?
– JL2210
Mar 7 at 19:06
@JL2210 hmm I am not getting that warning, although my compiler setting has all Warnings enabled :(
– HumbleBumble
Mar 7 at 19:12
Perhaps it's got something to do with that lonely and unused
struct sigaction sa;
you got there.– PSkocik
Mar 7 at 18:56
Perhaps it's got something to do with that lonely and unused
struct sigaction sa;
you got there.– PSkocik
Mar 7 at 18:56
Oops, I should've erased it. At this moment it's not in my code.
– HumbleBumble
Mar 7 at 19:01
Oops, I should've erased it. At this moment it's not in my code.
– HumbleBumble
Mar 7 at 19:01
@HumleBumble Actually, in it lies the answer to your question.
– PSkocik
Mar 7 at 19:01
@HumleBumble Actually, in it lies the answer to your question.
– PSkocik
Mar 7 at 19:01
time.c:20:27: warning: comparison is always true due to limited range of data type [-Wtype-limits]
have anything to do with it?– JL2210
Mar 7 at 19:06
time.c:20:27: warning: comparison is always true due to limited range of data type [-Wtype-limits]
have anything to do with it?– JL2210
Mar 7 at 19:06
@JL2210 hmm I am not getting that warning, although my compiler setting has all Warnings enabled :(
– HumbleBumble
Mar 7 at 19:12
@JL2210 hmm I am not getting that warning, although my compiler setting has all Warnings enabled :(
– HumbleBumble
Mar 7 at 19:12
|
show 5 more comments
1 Answer
1
active
oldest
votes
setitimer(ITIMER_REAL,...)
causes SIGALRM
to be sent to the calling process upon timer expiry. SIGALRM
is a normally deadly signal, whose default disposition is to kill the process.
If you want to prevent your process from being killed by this signal, you need to handle it somehow.
Example (prints EXPIRED from the handler every 100ms) based on your code:
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
void handler(int Sig)
(void)Sig;
char msg[]="EXPIREDn";
ssize_t nwr = write(1,msg,sizeof(msg)-1); (void)nwr;
int main(void)
struct itimerval timer1;
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM,&sa,0);
timer1.it_interval.tv_sec = 0;
timer1.it_interval.tv_usec = 100000;
timer1.it_value.tv_sec = 0;
timer1.it_value.tv_usec = 100000 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1) pause();
return 0;
1
Tried the code, works just as I wanted my code to. Thank you so much!
– HumbleBumble
Mar 7 at 19:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050912%2fsys-time-h-timer-only-runs-once%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
setitimer(ITIMER_REAL,...)
causes SIGALRM
to be sent to the calling process upon timer expiry. SIGALRM
is a normally deadly signal, whose default disposition is to kill the process.
If you want to prevent your process from being killed by this signal, you need to handle it somehow.
Example (prints EXPIRED from the handler every 100ms) based on your code:
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
void handler(int Sig)
(void)Sig;
char msg[]="EXPIREDn";
ssize_t nwr = write(1,msg,sizeof(msg)-1); (void)nwr;
int main(void)
struct itimerval timer1;
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM,&sa,0);
timer1.it_interval.tv_sec = 0;
timer1.it_interval.tv_usec = 100000;
timer1.it_value.tv_sec = 0;
timer1.it_value.tv_usec = 100000 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1) pause();
return 0;
1
Tried the code, works just as I wanted my code to. Thank you so much!
– HumbleBumble
Mar 7 at 19:18
add a comment |
setitimer(ITIMER_REAL,...)
causes SIGALRM
to be sent to the calling process upon timer expiry. SIGALRM
is a normally deadly signal, whose default disposition is to kill the process.
If you want to prevent your process from being killed by this signal, you need to handle it somehow.
Example (prints EXPIRED from the handler every 100ms) based on your code:
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
void handler(int Sig)
(void)Sig;
char msg[]="EXPIREDn";
ssize_t nwr = write(1,msg,sizeof(msg)-1); (void)nwr;
int main(void)
struct itimerval timer1;
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM,&sa,0);
timer1.it_interval.tv_sec = 0;
timer1.it_interval.tv_usec = 100000;
timer1.it_value.tv_sec = 0;
timer1.it_value.tv_usec = 100000 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1) pause();
return 0;
1
Tried the code, works just as I wanted my code to. Thank you so much!
– HumbleBumble
Mar 7 at 19:18
add a comment |
setitimer(ITIMER_REAL,...)
causes SIGALRM
to be sent to the calling process upon timer expiry. SIGALRM
is a normally deadly signal, whose default disposition is to kill the process.
If you want to prevent your process from being killed by this signal, you need to handle it somehow.
Example (prints EXPIRED from the handler every 100ms) based on your code:
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
void handler(int Sig)
(void)Sig;
char msg[]="EXPIREDn";
ssize_t nwr = write(1,msg,sizeof(msg)-1); (void)nwr;
int main(void)
struct itimerval timer1;
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM,&sa,0);
timer1.it_interval.tv_sec = 0;
timer1.it_interval.tv_usec = 100000;
timer1.it_value.tv_sec = 0;
timer1.it_value.tv_usec = 100000 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1) pause();
return 0;
setitimer(ITIMER_REAL,...)
causes SIGALRM
to be sent to the calling process upon timer expiry. SIGALRM
is a normally deadly signal, whose default disposition is to kill the process.
If you want to prevent your process from being killed by this signal, you need to handle it somehow.
Example (prints EXPIRED from the handler every 100ms) based on your code:
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
void handler(int Sig)
(void)Sig;
char msg[]="EXPIREDn";
ssize_t nwr = write(1,msg,sizeof(msg)-1); (void)nwr;
int main(void)
struct itimerval timer1;
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM,&sa,0);
timer1.it_interval.tv_sec = 0;
timer1.it_interval.tv_usec = 100000;
timer1.it_value.tv_sec = 0;
timer1.it_value.tv_usec = 100000 ;
setitimer(ITIMER_REAL, &timer1, NULL);
printf( "init interval counter: %ld.%ldn", timer1.it_interval.tv_sec,
timer1.it_interval.tv_usec);
printf( "init value counter: %ld.%ldnn", timer1.it_value.tv_sec,
timer1.it_value.tv_usec );
while(1) pause();
return 0;
answered Mar 7 at 19:08
PSkocikPSkocik
34.5k65578
34.5k65578
1
Tried the code, works just as I wanted my code to. Thank you so much!
– HumbleBumble
Mar 7 at 19:18
add a comment |
1
Tried the code, works just as I wanted my code to. Thank you so much!
– HumbleBumble
Mar 7 at 19:18
1
1
Tried the code, works just as I wanted my code to. Thank you so much!
– HumbleBumble
Mar 7 at 19:18
Tried the code, works just as I wanted my code to. Thank you so much!
– HumbleBumble
Mar 7 at 19:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050912%2fsys-time-h-timer-only-runs-once%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Perhaps it's got something to do with that lonely and unused
struct sigaction sa;
you got there.– PSkocik
Mar 7 at 18:56
Oops, I should've erased it. At this moment it's not in my code.
– HumbleBumble
Mar 7 at 19:01
@HumleBumble Actually, in it lies the answer to your question.
– PSkocik
Mar 7 at 19:01
time.c:20:27: warning: comparison is always true due to limited range of data type [-Wtype-limits]
have anything to do with it?– JL2210
Mar 7 at 19:06
@JL2210 hmm I am not getting that warning, although my compiler setting has all Warnings enabled :(
– HumbleBumble
Mar 7 at 19:12