![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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);
}
}
|
|
||||
|
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 |
|
||||
|
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? |
|
||||
|
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.. |
|
||||
|
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! |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|