The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to make the parent process to wait for the child process sennidurai Shell Programming and Scripting 7 09-30-2009 04:32 AM
catching a signal from child process emil2006 High Level Programming 2 05-03-2008 09:20 AM
Can a child process return a specific value to a parent process ? Ametis1970 High Level Programming 8 04-10-2008 12:22 AM
catch SIGCHLD signal in parent process ranjan UNIX for Advanced & Expert Users 2 06-10-2005 01:52 AM
parent and child process question? tosa High Level Programming 0 02-16-2005 03:04 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
vkn_1985 vkn_1985 is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 4
Sending signal from child to parent process!

Hi All,
I facing a problem in handling signals between parent process communication. I am trying to send a signal(SIGINT) from child to parent. I am using kill function to do so and I am trying to read the signal using sigaction(). But the program is ending abruptly and I am not able to figure out why! Could anyone of you please help me?
The code is as follows :



Code:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
void sig_usr(int signo){
if(signo == SIGINT)
printf("Signal caught!");
return;
}

int main(void){
pid_t pid, ppid;
ppid = getpid();
printf("%d ", ppid);
if((pid = fork()) == 0){ //Child
kill(ppid, SIGINT);
}
else{
printf("%d %d ",ppid, pid);
struct sigaction sig;
sigemptyset(&sig.sa_mask);
sig.sa_flags = 0;
sig.sa_handler = sig_usr;
if(sigaction(SIGINT,&sig,NULL) == 0)
printf("Signal processed OKay ");
sleep(10);
}
}

  #2 (permalink)  
Old 4 Weeks Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,794
start by setting up the signal handler before you create the child

Code:
int main(void)
{                     
    pid_t pid, ppid;                    
    ppid = getpid();                    
    struct sigaction sig;               
    sigemptyset(&sig.sa_mask);          
    sig.sa_flags = 0;                   
    sig.sa_handler = sig_usr;           
    
    printf("%d ", ppid);                
    if((pid = fork()) == 0)
    { //Child    
        kill(ppid, SIGINT);                 
    }                                   
    else
    {                               
    printf("%d %d ",ppid, pid);             
    if(sigaction(SIGINT,&sig,NULL) == 0)
    		printf("Signal processed OKay ");   
    sleep(10);                          
    }                                   
}

And... consider not using functions like printf in a signal handler. See async-signal safe
functions:

System Interfaces Chapter 2
  #3 (permalink)  
Old 4 Weeks Ago
vkn_1985 vkn_1985 is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 4
Thank you. But I didnt get why printf() should not be used...Looking at the link you have posted, I am guessing functions like printf cannot be interruped in the middle. Am I right?
And which other function should I consider using for printing within handler?
  #4 (permalink)  
Old 4 Weeks Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,794
The reverse - printf CAN be interrupted so you get a hashcan result.
write() - which is listed in the functions in the link.

Let me rephrase this correctly - async-signal safe functions are defined to be usable in signal handlers and have them behave as advertised.

When executing a call that is not async-signal safe you get undefined results when a second signal hits a signal handler that is currently executing that function. ie., if the function call works it is purely by accident not design.

printf is a large chunk of code made from a lot of functions and system calls. write() is a single base system call that is not made of sub-functions and code in user space like printf. It is a single kernel mode call.

Last edited by jim mcnamara; 4 Weeks Ago at 06:27 PM..
  #5 (permalink)  
Old 4 Weeks Ago
vkn_1985 vkn_1985 is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 4
Thank you. Now I get it.

Hey, I have two more questions. I was trying sigaction before fork(), but I see some unexpected results!
1. I trigger SIGINT signal only once using kill function. I expect this to trigger respective signal handler and do the operation. But even if I am sending SIGINT only once, I am getting "Signal processed OKay" printed twice! Shouldn't this be printed only once as I am sending SIGINT only once?

Output :

Code:
Signal processed OKay 7006 0 Success SIGINT signal caught!!
Signal processed OKay 7006

Source code :

Code:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
static void sig_usr(int signo){
if(signo == SIGINT){
char buf[] = "SIGINT signal caught!!\n";
int wr = strlen(buf);
write(STDOUT_FILENO, buf, wr);
}
return;
}

int main(void)
{
    pid_t pid, ppid;
    ppid = getpid();
    struct sigaction sig;
    sigemptyset(&sig.sa_mask);
    sig.sa_flags = 0;
    sig.sa_handler = sig_usr;
    if(sigaction(SIGINT,&sig,NULL) == 0)
                printf("Signal processed OKay ");
    sleep(10);
    printf("%d ", ppid);
    if((pid = fork()) == 0)
    { //Child    
        kill(ppid, SIGINT);
    }
}


2. Secondly, I am facing some problems when I try to send 2 SIGINT signals one after the other continuously from child process! i.e, when another kill() function is used after the first one in above code. The result I expected was two "SIGINT signal caught!!" messages. But I am getting only one instead.
In general, if I am sending signals of same kind continuously of same kind from child to parent, anything in particular I need to do?

Sorry for the elongated mail. Please help.

Regards!
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0