Thread: Signal understanding

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    12

    Signal understanding

    Hi

    i need explanation ... how it is possible this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    #include <string.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <sys/wait.h>
    
    
    void handler_readPipe(int sig)
    {
        printf("I have signal ... \n");
    }
    
    
    int main () 
    {
        int pid = fork();
        if(pid == -1)
        {
            return -1;
        }
    
    
        if(pid == 0)
        {
            sleep(5);
            kill(getppid(), SIGUSR1);
        }
        else
        {
            struct sigaction sa;
            sa.sa_handler = &handler_readPipe;
            sa.sa_flags = SA_RESTART;
            sigaction( SIGUSR1, &sa, NULL );
            sleep(10);
            printf("Message affter kill\n");
        }
       return(0);
    }
    How it is possible, if I kill a parent from a child, the process will run to the end anyway ... how is it possible to execute the line: printf ("Message affter kill \ n");


    Code:
    I have signal ... 
    Message affter kill

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Despite the name, kill doesn't kill a process per se.
    Whether a process actually dies depends on the nature of
    a) the signal being sent.
    b) what signal handling the recipient task has installed.

    For example, SIGKILL is always fatal to the recipient task (you cannot catch SIGKILL).

    Ordinarily, the default action of USR1 is indeed to terminate the process.
    signal(7) - Linux manual page

    But you added a handler for it, so now your process runs your desired function, and then resumes (and no, this isn't what SA_RESTART means).
    sigaction(2) - Linux manual page
    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.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    The important point you are missing is that most system calls can fail with EINTR

    EINTR The call was interrupted by a signal before any data was read;
    see signal(7).
    In the case of sleep():

    RETURN VALUE
    Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal
    handler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Signal Handling
    By DarrenY in forum C Programming
    Replies: 4
    Last Post: 06-04-2006, 11:30 AM
  2. Signal() problem
    By DarrenY in forum C Programming
    Replies: 8
    Last Post: 06-02-2006, 11:56 AM
  3. Signal Programming
    By cprogrammer_18 in forum Linux Programming
    Replies: 2
    Last Post: 01-14-2006, 11:51 AM
  4. Signal
    By Gerard Fremaint in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-10-2002, 02:03 PM
  5. signal.h
    By DavidP in forum C++ Programming
    Replies: 1
    Last Post: 02-19-2002, 06:46 PM

Tags for this Thread