Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, i am using the sigaction function to handle the SIGCHLD signal.Is it possible to use a class member function as the handler function (the sa_handler member of the sigaction ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9

    Handling a signal with a class member function

    Hello,
    i am using the sigaction function to handle the SIGCHLD signal.Is it possible to use a class member function as the handler function (the sa_handler member of the sigaction structure)?
    The function's signature is:
    Code:
    void (*sa_handler)(int);
    I could use a static member function with such a signature,but i wouldn't be able to pass an "handle" to the actual object instance.
    Is a global variable the only solution to this? Or is there some trick i could use?

    Thanks in advance!

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Just curious...why does it have to be a class member?

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9
    Just cause i would like to avoid global stuff

    But if that's the only way....oh well,i tried

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Are you saying you avoid all functions....even class member functions

  5. #5
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9
    Ok,so now i'm using a global variable to keep track of the pid of interest and a static member function to handle the SIG_CHLD signal.

    The member function is:
    Code:
    void MyClass::CallWaitChildProcess(int signo) 
    {          
        int status, child_val;
    
    	if (waitpid(g_ChildProcessPid, &status, WNOHANG) < 0) 
        {
            
            return;
        }
    
       
        if (WIFEXITED(status))               
        {
            child_val = WEXITSTATUS(status); 
            printf("process pid:%d --> exited normally with status %d\n", g_ChildProcessPid,child_val);
        }
    }
    I have a further question.
    I only want to handle the SIG_CHLD generated from the external program i run,maintaining the default behaviour for the SIG_CHLD signals generated from other child processes.So far,the application doesn't fork any other child process,but i can't exclude that in the future someone will add them,so i want these eventual future child processes to be handled in the default way (i.e. being ignored).
    Should i change the CallWaitChildProcess function or waitpid will do the job,"excluding" unrecognized pids?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...