Signal begginer

This is a discussion on Signal begginer within the C++ Programming forums, part of the General Programming Boards category; Hello guys Can anyone explain to me why the kill(my_pid,SIGINT) terminates the programm without executing the line sleep(15) but executing ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Signal begginer

    Hello guys

    Can anyone explain to me why the kill(my_pid,SIGINT) terminates the programm without executing the line sleep(15) but executing only the printf("Finish\n") ?


    thank you

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/socket.h>	
    #include <stdlib.h>
    
    void catch_int(int sig_num) {
    signal(SIGINT, catch_int);
    printf("interrupt \n");
    }
     
     int main()
     { 
         
         signal(SIGINT, catch_int);
         int pid;
         pid_t my_pid = getpid();
         
         printf("HELLO \n");
         
         pid=fork();
         if(pid==0)
         {
                   printf("CHILD \n");
                   kill(my_pid,SIGINT);
                   exit(1);
                   }
         
         sleep(15);
         printf("Finish\n");        
         return 0;
    }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    I don't know about sockets programming, but that fork() looks suspicious. Can it be sleep() and printf() are being executed by another process? your kill() is followed by an exit(). printf() on that process won't ever run.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    Quote Originally Posted by Mario F.
    I don't know about sockets programming, but that fork() looks suspicious. Can it be sleep() and printf() are being executed by another process? your kill() is followed by an exit(). printf() on that process won't ever run.
    exit() terminates the child process. Try to execute to see the results plz

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    I can't right now. But as I said I have no knowledge of sockets programming... and very little of processes. However, the main process keeps running and that one will execute sleep() and printf(), if you don't see it happening try to debug for processes or increase sleep value significantly. 15 miliseconds is hardly something you can see.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    It's just an implementation detail of sleep. The sleep(15) line is getting executed, it's just getting interrupted too.
    The current process is suspended from execution for the
    number of seconds specified by the argument. The actual
    suspension time may be less than that requested because any
    caught signal will terminate the sleep() following execution
    of that signal's catching routine.
    http://bama.ua.edu/cgi-bin/man-cgi?sleep+3C
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2009, 10:05 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 01:36 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  5. signal handling
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 02:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21