【文章內(nèi)容簡介】
SIG_DFL Can install to restore default signal handler Subsequently, process will handle 2/SIGINT signals using the default handler for 2/SIGINT signals int main(void) { void (*pfRet)(int)。 pfRet = signal(SIGINT, somehandler)。 … pfRet = signal(SIGINT, SIG_DFL)。 … } 31 Outline 1. UNIX Process Control 2. Signals 3. C90 Signal Handling 4. C90 Signal Blocking 5. POSIX Signal Handling/Blocking 6. Conclusion 7. (optional) Alarms and Interval Timers 32 Race Conditions in Signal Handlers A race condition is a flaw in a program whereby the correctness of the program is critically dependent on the sequence or timing of other events. Race conditions can occur in signal handlers… 33 Race Condition Example void addSalaryToSavings(int iSig) { int iTemp。 iTemp = iSavingsBalance。 iTemp += iMonthlySalary。 iSavingsBalance = iTemp。 } Handler for hypothetical “update monthly salary” signal 34 Race Condition Example (cont.) void addSalaryToSavings(int iSig) { int iTemp。 iTemp = iSavingsBalance。 iTemp += iMonthlySalary。 iSavingsBalance = iTemp。 } 2022 (1) Signal arrives。 handler begins executing 35 Race Condition Example (cont.) void addSalaryToSavings(int iSig) { int iTemp。 iTemp = iSavingsBalance。 iTemp += iMonthlySalary。 iSavingsBalance = iTemp。 } void addSalaryToSavings(int iSig) { int iTemp。 iTemp = iSavingsBalance。 iTemp += iMonthlySalary。 iSavingsBalance = iTemp。 } 2022 (2) Another signal arrives。 first instance of handler is interrupted。 second instance of handler begins executing 2022 36 Race Condition Example (cont.) void addSalaryToSavings(int iSig) { int iTemp。 iTemp = iSavingsBalance。 iTemp += iMonthlySalary。 iSavingsBalance = iTemp。 } void addSalaryToSavings(int iSig) { int iTemp。 iTemp = iSavingsBalance。 iTemp += iMonthlySalary。 iSavingsBalance = iTemp。 } 2022 2022 (3) Second instance executes to pletion 2050 2050 37 Race Condition Example (cont.) void addSalaryToSavings(int iSig) { int iTemp。 iTemp = iSavingsBalance。 iTemp += iMonthlySalary。 iSavingsBalance = iTemp。 } 2022 (4) Control returns to first instance, which executes to pletion 2050 2050 Lost 50 !!! 38 Blocking Signals in Handlers Blocking signals ? To block a signal is to queue it for delivery at a later time Why block signals when handler is executing? ? Avoid race conditions when another signal of type x occurs while the handler for type x is executing How to block signals when handler is executing? ? Automatic during execution of signal handler!!! ? Previous sequence cannot happen!!! ? While executing a handler for a signal of type x, all signals of type x are blocked ? When/if signal handler returns, block is removed 39 Race Conditions in General Race conditions can occur elsewhere too int iFlag = 0。 void myHandler(int iSig) { iFlag = 1。 } int main(void) { if (iFlag == 0) { /* Do something */ } } Problem: myflag might bee 1 just after the parison! Must make sure that critical sections of code are not interrupted 40 Blocking Signals in General How to block signals in general? ? Not possible in C90 ? Possible using POSIX functions… 41 Outline 1. UNIX Process Control 2. Signals 3. C90 Signal Handling 4. C90 Signal Blocking 5. POSIX Signal Handling/Blocking 6. Conclusion 7. (optional) Alarms and Interval Timers 42 POSIX Signal Handling C90 standard ? Defines signal() and raise() functions – Work across all systems (UNIX, LINUX, Windows), but… – Work differently across some systems!!! ? On some systems, signals are blocked during execution of handler for that type of signal but not so on other (older) systems ? On some (older) systems, handler installation for signals of type x is cancelled after first signal of type x is received。 must reinstall the handler but not so on other systems ? Does not provide mechanism to block signals in general 43 POSIX Signal Handling POSIX standard ? Defines kill(), sigprocmask(), and sigaction() functions – Work the same across