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










1















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!










share|improve this question
























  • 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















1















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!










share|improve this question
























  • 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













1












1








1








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















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;






share|improve this answer


















  • 1





    Tried the code, works just as I wanted my code to. Thank you so much!

    – HumbleBumble
    Mar 7 at 19:18










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
);



);













draft saved

draft discarded


















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









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;






share|improve this answer


















  • 1





    Tried the code, works just as I wanted my code to. Thank you so much!

    – HumbleBumble
    Mar 7 at 19:18















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;






share|improve this answer


















  • 1





    Tried the code, works just as I wanted my code to. Thank you so much!

    – HumbleBumble
    Mar 7 at 19:18













0












0








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;






share|improve this answer













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;







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived