Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

[URGENT]Signal Handling
Hi,

I have a program that fork()'s + execlp()'s two children. Later in the
program I want to send a signal to the parent(SIGINT) . The parent's signal
handler should then send signals to these two children. I am able to catch
the signal in the parent - on catching this signal I invoke a function that
sends kill(childPid[i],SIGINT) but the signal handler in the child doesn't
seem to be getting invoked. Can someone please tell me why this is
happenning? Since this is kind of urgent I would greatly appreciate a speedy
response. Thanks a lot!

I am including the code below - first the parent's and following that the
child's

Parent code:
void sendKillsToChildren()
{
int i;
for(i=0;i<nNValue;i++)
{
printf("sending kil to %d\n",childPids[i]);
kill(childPids[i],SIGINT);
}
}

void signalHandler(int dud)
{
printf("ctrl-c pressed\n");
printf("sendKillsToChildren\n");
sendKillsToChildren();
}

int main()
{
signal(SIGINT,signalHandler);
int i;

 childPids=(int*)malloc(nNValue*sizeof(pi
d_t));

for(i=0;i<2;i++)
{
switch(childPids[i]=fork())
{
case -1:
//Error
perror("Error in fork() in godclass::forkOne()");
break;
case 0:
//In Child
execlp("child.o",0);
break;
default:
//In parent
printf("child[%d]=%d\n",i,childPids[i]);
break;
}
}

while(1);
}

Child code:
void signalHandler(int dud)
{
printf("recvd signal in %d\n",getpid());
}

int main()
{
signal(SIGINT,signalHandler);
printf("in child process %d\n",getpid());
}




Report this thread to moderator Post Follow-up to this message
Old Post
Rookie
10-19-04 01:56 AM


Re: [URGENT]Signal Handling
In article <cl15qm$pd7$1@gist.usc.edu>,
Rookie <dominicjoseph@rediffmail.com> wrote:
:I have a program that fork()'s + execlp()'s two children. Later in the
:program I want to send a signal to the parent(SIGINT) . The parent's signal
:handler should then send signals to these two children.

:int main()
:{
: signal(SIGINT,signalHandler);

:  switch(childPids[i]=fork())

:   execlp("child.o",0);


I have not looked closely to see what the source of your problem is,
but I do notice that you are not taking any particular care to
ensure that the children are not in the same process group as
the main process. Is it possible that the same signal is going to
all members of the same process group, not just to the main
process?
--
If a troll and a half can hook a reader and a half in a posting and a half,
how many readers can six trolls hook in six postings?

Report this thread to moderator Post Follow-up to this message
Old Post
Walter Roberson
10-19-04 01:56 AM


Re: [URGENT]Signal Handling
In article <cl15qm$pd7$1@gist.usc.edu>, Rookie wrote:

>int main()
>{
> signal(SIGINT,signalHandler);
> int i;

You can't have declarations after code in a function.
These are not complete programs with headers and all.

>   //In Child
>   execlp("child.o",0);

This looks fishy - are you sure you're successfully executing
a real program ?  There's nothing to catch an execlp() failure.

You attempt to signal an entire arrayful of pids rather than
just the ones that have been used.

Most critically are you sure your child hasn't exited anyway
as
int main() {
signal(SIGINT,signalHandler);
printf("in child process %d\n",getpid());
}
contains no delay loop.  Test the child's response to a signal
direct from the command-line.

I prefer my while(1) delay loops to contain a sleep().

With all of the above corrected, and on Linux it works.

--
Elvis Notargiacomo  master AT barefaced DOT ch
http://www.notatla.org.uk/goen/
7.031: OnACPower returned value( 0x1 ) which is Equal To 0x1

Report this thread to moderator Post Follow-up to this message
Old Post
all mail refused
10-19-04 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:57 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.