setitimer and nanosleep issue. - Unix

This is a discussion on setitimer and nanosleep issue. - Unix ; So I'm having a problem where the setitimer signal isn't going threw when nanosleep is sleeping. I know this is a problem with the sleep() function but thought nanosleep allowed signals. At least the man page says it does. here's ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: setitimer and nanosleep issue.

  1. setitimer and nanosleep issue.

    So I'm having a problem where the setitimer signal isn't going threw
    when nanosleep is sleeping. I know this is a problem with the sleep()
    function but thought nanosleep allowed signals. At least the man page
    says it does.

    here's what I have

    int main()
    {
    struct itimerval interval;
    struct timespec sleep;

    sleep.tv_sec = 1;
    sleep.tv_nsec = 0;

    /* Timer */
    interval.it_interval.tv_sec = 5;
    interval.it_interval.tv_usec = 0;
    interval.it_value.tv_sec = 1;
    interval.it_value.tv_usec = 0;

    signal(SIGVTALRM, handler);
    setitimer(ITIMER_VIRTUAL, &interval, NULL);

    while(1)
    {
    nanosleep(&sleep, NULL);
    printf("bye\n");

    }

    return 0;
    }
    void handler(int signal)
    {
    printf("HI\n");
    }

    When I disable nansleep the setitimer works just fine however when I
    enable it, it seems the nanosleep is overwriting it. Any Suggestions?

    Ryan

  2. Re: setitimer and nanosleep issue.

    On Nov 8, 11:38 am, Ryan Knopp wrote:
    > So I'm having a problem where the setitimer signal isn't going threw
    > when nanosleep is sleeping. I know this is a problem with the sleep()
    > function but thought nanosleep allowed signals. At least the man page
    > says it does.


    Yes, and actually this is the same for sleep() as well, as far as I
    know.

    > here's what I have
    >
    > int main()
    > {
    > struct itimerval interval;
    > struct timespec sleep;
    >
    > sleep.tv_sec = 1;
    > sleep.tv_nsec = 0;
    >
    > /* Timer */
    > interval.it_interval.tv_sec = 5;
    > interval.it_interval.tv_usec = 0;
    > interval.it_value.tv_sec = 1;
    > interval.it_value.tv_usec = 0;
    >
    > signal(SIGVTALRM, handler);
    > setitimer(ITIMER_VIRTUAL, &interval, NULL);
    >
    > while(1)
    > {
    > nanosleep(&sleep, NULL);
    > printf("bye\n");
    >
    > }
    >
    > return 0;}
    >
    > void handler(int signal)
    > {
    > printf("HI\n");
    >
    > }
    >
    > When I disable nansleep the setitimer works just fine however when I
    > enable it, it seems the nanosleep is overwriting it. Any Suggestions?


    ITIMER_VIRTUAL counts CPU time. When your process is sleeping, it's
    not using CPU time, so the timer doesn't run, and won't expire.

    You probably want to use ITIMER_REAL instead.


  3. Re: setitimer and nanosleep issue.

    On 2007-11-08, Ryan Knopp wrote:
    > So I'm having a problem where the setitimer signal isn't going threw
    > when nanosleep is sleeping. I know this is a problem with the sleep()
    > function but thought nanosleep allowed signals. At least the man page
    > says it does.


    Hm. Maybe the siginterrupt function can help here? According to man page
    on linux by default system calls are not interupted by signals, unless
    this function is used to tell otherwise.

    --
    Minds, like parachutes, function best when open

+ Reply to Thread