Thread: Why does the code excute, but I get no print message?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    Why does the code excute, but I get no print message?

    Could someone give me some kind of technical explanation why the follow code will execute, but won't print anything out?

    Code:
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    
    static void sig_alarm(int signo) {
      if(signo == SIGALRM)
        printf("Signal Caught \n");
    }
    
    unsigned int sleep1(unsigned int nsec){
      if(signal(SIGALRM,sig_alarm) == SIG_ERR){
        return(nsec);
      }
      alarm(nsec);
      pause();
      return(alarm(0));
    }
    
    int main(void) {
      sleep(5);
      return 0;
    }
    Thanks
    Last edited by cdalten; 02-11-2006 at 06:53 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Because you call the wrong sleep function
    Code:
    int main(void) {
      sleep1(5);  // not sleep(5)
      return 0;
    }
    Kurt

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    you are not calling the fucntion properly. check out the code again. the main cals sleep fucntion. which seems to call sleep1 in your code.

    ssharish2005

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Yeah, sleep() is actually a function which causes the program to...for lack of a better word sleep, for 5 miliseconds or something or another....which is why the program compiled with no problems. Change the sleep() call in the main to sleep1() and you should be fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 05:15 PM
  2. Source Code for Print a "Box"
    By imbecile in C in forum C Programming
    Replies: 2
    Last Post: 07-27-2003, 11:49 PM
  3. Replies: 1
    Last Post: 01-10-2003, 10:08 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM