C: SIGALRM - alarm to display message every second

前端 未结 5 1131
北荒
北荒 2020-12-03 08:53

So I\'m trying to call an alarm to display a message \"still working..\" every second. I included signal.h.

Outside of my main I have my function: (I never declare/d

5条回答
  •  失恋的感觉
    2020-12-03 09:28

    ooga is correct that you keep reloading the alarm so that it will never go off. This works. I just put a sleep in here so you don't keep stepping on yourself in the loop but you might want to substitute something more useful depending on where you are headed with this.

    void display_message(int s)
    {
         printf("copyit: Still working...\n" );
        // alarm(1);    //for every second
        // signal(SIGALRM, display_message);
    }
    
    int main(int argc, char *argv[])
    {
        int ret;
    
        while(1)
        {
            signal(SIGALRM, display_message);
            alarm(1);
    
            if ((ret = sleep(3)) != 0)
            {
                printf("sleep was interrupted by SIGALRM\n");
            }
        }
    
        return (0);
    }
    

提交回复
热议问题