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

Can fork be used in a signal handler?
Hello group,

I have a problem that i have to create a new process, when my process
gets a SIGCHLD.
So i want to use fork inside the signal handler for SIGCHLD.

Can i use like that? I know that non-reentrant functions can not be
used inside a signal handler. Is fork() a reentrant function or not?

And in the signal handler i'm traversing a linked list which has the
complexity of O(n2). Is that ok if a signal handler to be that heavy?
Note: no global variables are being changed inside the handler.

Please assist me ASAP.

Thanks.

-Kiran.

Report this thread to moderator Post Follow-up to this message
Old Post
kiran
03-28-08 01:11 PM


Re: Can fork be used in a signal handler?
kiran <edu.mvk@gmail.com> writes:
> I have a problem that i have to create a new process, when my process
> gets a SIGCHLD. So i want to use fork inside the signal handler for
> SIGCHLD.
>
> Can i use like that?

Assuming there are no 'other issues' (ie 'multiple threads'), yes.
I would nevertheless rather do the fork outside the signal handler.

> I know that non-reentrant functions can not be
> used inside a signal handler.

This is wrong. A signal handler must only call functions which are
async-signal-safe (that's different from 'reentrant') if the signal
could have interrupted a non async-signal-safe function. A list of
those is given in the 'Signal Concepts' section of SUS.

> And in the signal handler i'm traversing a linked list which has the
> complexity of O(n2).

Traversing a linked-list is an operation with a linear complexity
(O(n)).

Report this thread to moderator Post Follow-up to this message
Old Post
Rainer Weikusat
03-29-08 12:22 AM


Re: Can fork be used in a signal handler?
kiran <edu.mvk@gmail.com> writes:

>I have a problem that i have to create a new process, when my process
>gets a SIGCHLD.
>So i want to use fork inside the signal handler for SIGCHLD.

>Can i use like that? I know that non-reentrant functions can not be
>used inside a signal handler. Is fork() a reentrant function or not?

>And in the signal handler i'm traversing a linked list which has the
>complexity of O(n2). Is that ok if a signal handler to be that heavy?
>Note: no global variables are being changed inside the handler.

How do you guarantee that the linked list is in a stable state in the
signal handler?

fork() can legally be called from signal handlers (per POSIX).

See http://www.unix.org/single_unix_specification/

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

Report this thread to moderator Post Follow-up to this message
Old Post
Casper H.S. Dik
03-29-08 12:22 AM


Re: Can fork be used in a signal handler?
kiran <edu.mvk@gmail.com> writes:
> I have a problem that i have to create a new process, when my process
> gets a SIGCHLD.
> So i want to use fork inside the signal handler for SIGCHLD.

Have you read the documentation for the operating system you're using?
You didn't say what you're using, but on Solaris I see this for
fork(2):

| MT-Level                    | Async-Signal-Safe.          |

and the attributes(5) page says:

Async-Signal-Safe

Async-Signal-Safe refers to particular library functions
that  can  be  safely  called  from  a signal handler. A
thread that is executing an  Async-Signal-Safe  function
will  not  deadlock with itself if interrupted by a sig-
nal. Signals are only a problem  for  MT-Safe  functions
that acquire locks.

Async-Signal-Safe functions are  also  MT-Safe.  Signals
are  disabled  when  locks are acquired in Async-Signal-
Safe functions. These signals prevent a  signal  handler
that might acquire the same lock from being called.

So, you can do it there at least.

> And in the signal handler i'm traversing a linked list which has the
> complexity of O(n2). Is that ok if a signal handler to be that heavy?
> Note: no global variables are being changed inside the handler.

That's something you'll have to evaluate on your own.  Nobody here
knows the underlying requirements or detailed design of your program.

--
James Carlson, Solaris Networking              <james.d.carlson@sun.com>
Sun Microsystems / 35 Network Drive        71.232W   Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757   42.496N   Fax +1 781 442 1677

Report this thread to moderator Post Follow-up to this message
Old Post
James Carlson
03-29-08 12:22 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 09:22 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.