Download presentation
Presentation is loading. Please wait.
Published byEstella Evans Modified over 9 years ago
1
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class
2
Alerts Thread state contains flag, alert-pending Exception alerted Alert (thread) alert-pending to true, wakeup a waiting thread AlertWait (mutex, condition) if alert-pending set to false and raise exception else wait as usual Boolean b = TestAlert() tests and clear alert-pending TRY while (empty) AlertWait (m, nonempty); return (nextchar()); EXCEPT Thread.Alerted: return (eof);
3
Using Alerts sibling = Fork (proc, arg); while (!done) { done = longComp(); if (done) Alert (sibling); else done = TestAlert(); }
4
Wisdom Do s Reserve using alerts for when you don’t know what is going on Only use if you forked the thread Impose an ordering on lock acquisition Write down invariants that should be true when locks aren’t being held Don’t s Call into a different abstraction level while holding a lock Move the “last” signal beyond scope of Lock Acquire lock, fork, and let child release lock Expect priority inheritance since few implementations Pack data and expect fine grain locking to work
5
Signals Signals notify processes of internal or external events. –the Unix software equivalent of interrupts/exceptions –only way to do something to a process “from the outside” –Unix systems define a small set of signal types Examples of signal generation: –keyboard ctrl-c and ctrl-z signal the foreground process –synchronous fault notifications, syscall errors –asynchronous notifications from other processes via kill –IPC events (SIGPIPE, SIGCHLD) –alarm notifications signal == “upcall”
6
Process Handling of Signals 1. Each signal type has a system-defined default action. abort and dump core (SIGSEGV, SIGBUS, etc.) ignore, stop, exit, continue 2. A process may choose to block (inhibit) or ignore some signal types. 3. The process may choose to catch some signal types by specifying a (user mode) handler procedure. specify alternate signal stack for handler to run on system passes interrupted context to handler handler may munge and/or return to interrupted context
7
Using Signals int alarmflag=0; alarmHandler () { printf(“An alarm clock signal was received\n”); alarmflag = 1; } main() { signal (SIGALRM, alarmHandler); alarm(3); printf(“Alarm has been set\n”); while (!alarmflag) pause (); printf(“Back from alarm signal handler\n”); } Suspends caller until signal Instructs kernel to send SIGALRM in 3 seconds Sets up signal handler
8
User’s View of Signals II main() { int (*oldHandler) (); printf (“I can be control-c’ed\n”); sleep (3); oldHandler = signal (SIGINT, SIG_IGN); printf(“I’m protected from control-c\n”); sleep(3); signal (SIGINT, oldHandler); printf(“Back to normal\n”); sleep(3); printf(“bye\n”); }
9
Yet Another User’s View main(argc, argv) int argc; char* argv[]; { int pid; signal (SIGCHLD,childhandler); pid = fork (); if (pid == 0) /*child*/ { execvp (argv[2], &argv[2]); } else {sleep (5); printf(“child too slow\n”); kill (pid, SIGINT); } childhandler() {int childPid, childStatus; childPid = wait (&childStatus); printf(“child done in time\n”); exit; } SIGCHLD sent by child on termination; if SIG_IGN, dezombie Collects status
10
Delivering Signals 1. Signal delivery code always runs in the process context. 2. All processes have a trampoline instruction sequence installed in user-accessible memory. 3. Kernel delivers a signal by doctoring user context state to enter user mode in the trampoline sequence. First copies the trampoline stack frame out to the signal stack. 4. Trampoline sequence invokes the signal handler. 5. If the handler returns, trampoline returns control to kernel via sigreturn system call. Handler gets a sigcontext (machine state) as an arg; handler may modify the context before returning from the signal.
11
When to Deliver Signals? run user run kernel ready blocked run wakeup trap/fault sleep preempted suspend/run new fork zombie exit swapout/swapin (suspend) Interrupt low- priority sleep if signal is posted. Check for posted signals after wakeup. Deliver signals when resuming to user mode. Deliver signals when returning to user mode from trap/fault.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.