LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2015, 06:18 AM   #1
Dark Sorrow
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Rep: Reputation: Disabled
Unable to generate or catch User Defined Signal - 1


I am trying to generate and catch signal SIGUSR1.
I am creating a child process using fork system call, using sigaction to associate the function handler with the signal and use kill to invoke the signal.
However either signal is not getting invoked or handler fuction is not being called.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

void childProcess(void);
void parentProcess(pid_t);
void segHandler(int);

int main(void)
{
    pid_t pid;
    pid = fork();
    switch(pid)
    {
        case -1 :
            printf("Could not fork\n");
            exit(EXIT_FAILURE);

        case 0 :
            printf("Entering Child\n");
            childProcess();
            break;

        default :
            printf("Entering Parent\n");
            printf("Immediate PID : %d\n", pid);
            parentProcess(pid);
    }
    return EXIT_SUCCESS;
}

void childProcess(void)
{
    int op;
    struct sigaction act;
    act.sa_handler = segHandler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    op = sigaction(SIGUSR1, &act, 0);
    if(op == -1)
    {
        printf("Unable to sigaction\n");
        exit(EXIT_FAILURE);
    }
    else
        printf("Sigaction successfully set\n");
}

void parentProcess(pid_t pid)
{
    printf("Parent Process Sleeping for 5 Seconds\n");
    sleep(5);
    printf("Parent Process Woke up after 5 Seconds\n");
    printf("Calling kill on PID : %d\n", pid);
    kill(pid, SIGUSR1);
}

void segHandler(int sig)
{
    printf("Generated signal has been caugth\n");
    printf("Recideved Signal : %s", strsignal(sig));
}
 
Old 03-25-2015, 07:29 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
I'm under the impression your childProcess() function returns, followed promptly by termination of the program at "return EXIT_SUCCESS;".

Adding sleep() to the child could change that situation.
 
1 members found this post helpful.
Old 03-25-2015, 09:30 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You're exiting your parent process immediately after sending the signal. This also would kill the child; however the child is likely already gone because once childProcess() is complete, it returns to main() and exits. The child process should enter a forever loop of some type to keep it alive and waiting for the signal, and then maybe use the SIGUSR1 to cause the child to exit finally.

The parent should wait for a terminate signal from the child using something like waitpid(3), when the child exits, the parent will receive a signal and be able to check the child's exit status to determine if it was normal termination versus not.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User defined linux commands in a user defined shell Sushma05 Linux - Newbie 3 09-13-2013 07:21 AM
how to catch the sigfpe signal jideesha Linux - Software 3 08-01-2012 03:49 PM
[SOLVED] [C++] Cannot catch SIGPIPE signal dwhitney67 Programming 1 03-27-2011 08:20 PM
why the process cannot catch the HUP signal alpinist_dou Linux - Software 1 02-03-2010 10:01 AM
how to send user defined signal in linux feetyouwell Linux - Software 6 12-26-2007 04:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration