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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page