Signal Review A signal is the software equivalent of a hardware interrupt or fault Signals provide a way for a procedure to be called when one of a defined set of events occurs Events may be CSCI 8530 a fault e g division by zero a user action pressing control C or a process generated signal e g kill Signals Overview Generation and Handling Signal generation delivery and handling are each separate activities occur at three separate times and require separate examination Last updated 4 3 2008 1 Types of Signals 2 Signal Generation Step 1 Each system defines and supports some number of signals identified by number 1 2 and name QNX for example supports 32 non real time signal types Some signals are associated with their generating events e g SIGINT usually generated by some keyboard entry like control C Other signals may have no predefined generating events e g SIGUSR1 Most signals with associated generating events can still be used for arbitrary purposes but carefully See the signal h file for details on signals available in a particular system including their names and numbers Some signals are automatically generated when certain events occur For example arithmetic invalid instruction and memory protection faults will all generate a predefined signal associated with the fault Other signals may be generated to report events For example pressing control C on the keyboard asynchronous I O completion and a timer s expiration can each cause a signal to be generated Finally a process can use the kill and raise functions to explicitly generate signals 3 Signal Delivery Step 2 4 Asynchronous Delivery Signals can only be delivered to a running process If the process is blocked it must moved to the ready state before it can be selected for execution so it can be delivered a signal report Most systems use a bit map to record which signals have occurred prior to their delivery Multiple occurrences of the same signal prior to its delivery will thus be mapped to a single occurrence The order in which signals are delivered if multiple signals are pending is usually system dependent 5 When a signal is delivered to a process it immediately causes whatever it was doing to be temporarily suspended while the action registered for that signal is carried out unless the signal is masked discussed later Upon normal completion of the action associated with the signal process of the interrupted process is resumed either with the very next instruction or with the instruction that caused the fault that generated the interrupt 6 1 Advantages of Asynchrony Disadvantages of Asynchrony 1 Since signals are asynchronous they can be delivered to a process while it is doing something else The advantages of this are immediacy you are notified immediately for some value of immediately when something occurs concurrency you can be off doing something else while other processing goes on concurrently with your application When that other processing completes you are notified via a signal 7 Disadvantages of Asynchrony 2 Complexity If your application must prepare for the possibility of a signal at any point in its execution the application is going to become more complex With complexity comes a lack of robustness difficulty in understanding and maintaining and so on Non determinism You don t really know when a signal is going to arrive relative to other signals or even to the event that caused the signal to be delivered Signal delivery happens under the hood of the operating system and usually isn t well documented 8 Default Actions Lower performance Signals are slower than the synchronous mechanisms for communication because asynchrony costs It takes time to stop an application set up an appropriate environment in which to call the signal handler and then actually call the handler None of that overhead is required when one is just synchronously waiting for an occurrence If a process has not prepared itself to process a particular signal a default action is taken when that signal is delivered to the process For most signals this default action is to terminate the process that is abort or exit For a few other signals the default action is to ignore the signal If on the other hand a process has specified what to do when a signal is delivered then that action is taken instead of the default action 9 Default Action Possibilities 10 Observations Abort terminate the process after generating a dump creation of a file containing a copy of the memory associated with the process suitable for use with a debugger like gdb Exit terminate the process without generating a dump Ignore ignore the signal as if it had not occurred Stop suspend the process e g don t schedule it Continue resumes the process if suspended 11 It is important to note that any action including process termination can only be taken by the process receiving the signal This requires at the very least that the process be scheduled to run On a busy system if the process has a low priority this could take quite some time There may be further delay if a process is swapped out suspended or blocked in an uninterruptible way 12 2 Simple ANSI Signal Handling Signal Generation Functions The simplest signal handling technique is to use the signal function signal SIGNAME functionname SIGNAME is the name from signal h or number of a signal we wish to handle functionname is the name address of the function that will be invoked when SIGNAME is delivered to the process signal returns the previously registered functionname that is a pointer to a function for the signal being registered raise signo causes the specified signal to be sent to the process that executes the call to raise kill pid signo sends the specified signal to the specified process pid If pid is zero then each process in the same group as the calling process is sent the signal If pid is negative then pid identifies a process group each process of which is sent the signal 13 14 The setjmp Function ANSI Signal handling Functions The function named in the signal call is passed the integer number of the signal as its sole argument and has a void return type The function may eventually return to resume execution at the point the program was interrupted terminate using exit or abort or call the longjmp or siglongjmp function The setjmp function is used to record its environment in env for later use by longjmp include setjmp h int setjmp jmp buf env The setjmp function returns 0 when it is initially called Later if longjmp
View Full Document
Unlocking...