Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1040111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:11:01+00:00 2026-05-16T15:11:01+00:00

I have the following code which I’d hope would wait 2 seconds then wait

  • 0

I have the following code which I’d hope would wait 2 seconds then wait 5 seconds in a loop.

There is a reason why I’m not using the it_interval to refire the timer.

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>

#include <semaphore.h>

sem_t sem;

void sighdlr( int sig )
{
    printf( "sighdlr\n" );
    sem_post( &sem );
}

int main( int c, char *v[] )
{
    signal( SIGALRM, sighdlr );

    struct itimerval itv;
    struct tm tm;
    time_t now;

    while( true )
    {
        itv.it_value.tv_sec = 2;
        itv.it_value.tv_usec = 0;
        itv.it_interval.tv_sec = 0;
        itv.it_interval.tv_usec = 0;
        setitimer( ITIMER_REAL, &itv, NULL );

        sem_wait( &sem );
        now = time( NULL );
        localtime_r( &now, &tm );
        fprintf( stderr, "%02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec );

        itv.it_value.tv_sec = 5;
        itv.it_value.tv_usec = 0;
        itv.it_interval.tv_sec = 0;
        itv.it_interval.tv_usec = 0;
        setitimer( ITIMER_REAL, &itv, NULL );

        sem_wait( &sem );
        now = time( NULL );
        localtime_r( &now, &tm );
        fprintf( stderr, "%02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec );
    }
}

It produces the following output

sighdlr
15:32:50
15:32:50
sighdlr
15:32:52
15:32:52
sighdlr
15:32:54
15:32:54
sighdlr
15:32:56
15:32:56
sighdlr
15:32:58
15:32:58
sighdlr
15:33:00
15:33:00

I can’t work out why it isn’t waiting for the 5 seconds for the second timer.

Any ideas?

Thx
M.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:11:01+00:00Added an answer on May 16, 2026 at 3:11 pm

    Aha! fixed it.

    the sem_wait was being interrupted and returning -1 and errno == EINTR so I have to have something like this.

    do {
        ret = sem_wait( &sem );
    } while( ret < 0 && errno == EINTR );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following code,which does not show me anything #include <iostream> using namespace std;
I have following code which is not working on sencha touch 2, is there
I have the following code which takes List of boolean as a parameter then
I have the following code which downloads video content: WebRequest wreq = (HttpWebRequest)WebRequest.Create(url); using
I have following code which works as is, but does not work when cache=T
I have the following code which will convert a string to TitleCase. I would
I have the following code which works in Internet Explorer, but not in Firefox
I have the following code which I'm using to get the user to input
I have the following code which I am are currently using .... Basically, this
I have following code which successfully sent email using Gmail address. But when i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.