Fog Creek Software
Discussion Board

Unix signals and C++

Question: how do you guys handle signals in C++ objects?

In my case I have a program that forks a child process and runs another application, so it has to touch the signal handlers for SIGPIPE and SIGCHLD.

It seems to me that this breaks encapsulation: nothing else can touch those signals, because then the class's internals will be broken.

Anyone have a cleaner way?

Mike Swieton
Sunday, July 6, 2003

Same was you handle Windows messages in Win32.

Have a global handler that consults global tables in order to know which object to redirect the signal to. That's what "DispatchMessage()" does.

P.S: Depending on what exactly your program does, it might be better to ignore the signals (SIG_IGN them), and explicitly poll using wait() or friends. Most Unix texts always offer the most complex solution without discussing the simpler solutions that work in a lot of cases.

Ori Berger
Sunday, July 6, 2003

ACE provides ACE_Sig_Handler [1] and its collaborating friends, as well ACE_Reactor's signal-related registration and dispatch facilities [2]. I register the interested class instances with my reactor to get called back on when a given signal arises.


Steven E. Harris
Monday, July 7, 2003

*  Recent Topics

*  Fog Creek Home