Thread: problem with timer & signal

  1. #1
    Registered User
    Join Date
    Sep 2008

    problem with timer & signal

    I write a timer which periodically send signal interrupt to implement function "contextswitch"
    but something wrong with my code
    it can print "inside thr_quantum", "inside timer", "inside interrupt"
    but never print "inside contextswitch"
    I cannot find out what's wrong with it
    please help
    Thank you very much!

    The following is my code.
    /*myhandler: contextswitch*/ 
    void contextswitch(void) 
    fprintf(stderr, "inside contextswitch\n"); 
    /* set up myhandler for SIGALRM */ 
    static int th_interrupt(void) 
    fprintf(stderr, "inside interrupt\n"); 
    struct sigaction act; 
    act.sa_handler = contextswitch; 
    act.sa_flags = 0; 
    return (sigemptyset(&act.sa_mask) || sigaction(SIGALRM , &act, NULL)); 
    /* set ITIMER_REAL for 5-ms intervals */ 
    static int th_timer(time_t n) 
    fprintf(stderr, "inside timer\n"); 
    struct itimerval value; 
    value.it_interval.tv_sec = 0; 
    value.it_interval.tv_usec = n; 
    value.it_value = value.it_interval; 
    return (setitimer(ITIMER_REAL, &value, NULL)); 
    void thr_quantum(int n) 
    fprintf(stderr, "inside quantum\n"); 
    if (th_interrupt() == -1) { 
          perror("Cannot set up interrupt"); 
          exit( 1 ); 
    if (th_timer(n) == -1) { 
          perror("Cannot set up timer"); 
          exit( 1 ); 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Look, it's really quite simple.
    [code] goes before your code
    [/code] goes after it.

    Don't just slap code /code tags all over your post just to make the error message go away, think about why it's there in the first place. It's to make your code readable!

    This will be the last time I edit your post to fix this problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2009, 10:05 AM
  2. Signal handler function - pointer to this gets lost
    By maus in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2009, 09:10 AM
  3. Replies: 2
    Last Post: 04-22-2009, 02:35 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. Help needed with external call, WinExec() and timer.
    By algorithm in forum Windows Programming
    Replies: 9
    Last Post: 11-07-2001, 09:35 AM