Thread: Child process can't send signal to itself.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Child process can't send signal to itself.

    Hi!

    When I am trying to execute the following program the child process simply waits for the signal.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <signal.h>
    #include <stdlib.h>
    #include <sys/types.h>
    
    static int alarm_fired = 0;
    
    void ding(int sig)
    {
         alarm_fired = 1;
    }
    
    int main()
    {
         pid_t pid;
         (void)signal(SIGALRM, ding);
        
         printf("alarm application starting.\n");
        
         pid = fork();
        
         switch(pid){
         case -1:
                   /* failure */
                   perror("Fork failed");
                   exit(1);
                  
         case 0:
                   /* child */
                   sleep(1);
                   kill(getpid(), SIGALRM);
                   break;
             
         default:
                   exit(0);
                  
    }
         printf("waiting for alarm to go off\n");
        
         pause();
         if(alarm_fired)
              printf("Ding!\n");
         exit(0);
    }
    Output:

    linux:~/prog # ./thread
    alarm application starting.
    linux:~/prog # waiting for alarm to go off
    ...waits here...

    Why can't the child process send the signal to itself?

    Regards,

    Arun

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi!

    It is workig if I remove the pause() call.
    Why is this happening?

    Regards,

    Arun

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi!

    I traced it out. pause() is called after the signal is sent.

    Regards,

    Arun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  2. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  3. Sending a message to parent from child process
    By maxorator in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 04:23 PM
  4. save the output of child process
    By gandalf_bar in forum Linux Programming
    Replies: 3
    Last Post: 05-14-2004, 01:59 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM