1 Signals (continued) CS 241 April 9, 2012 University of Illinois.

Slides:



Advertisements
Similar presentations
Institut für Angewandte Mikroelektronik und Datentechnik Fachbereich Elektrotechnik und Informationstechnik, Universität Rostock Vorlesung Echtzeitbetriebssysteme.
Advertisements

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
15-213, Fall 06 Outline Shell Lab Processes Signals.
System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
CS Lecture 17 Outline Named pipes Signals Lecture 17
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
UNIX IPC CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of Concurrency2 Creating a UNIX process A process is created by making an exact.
1 Signals COS Goals of Today’s Lecture Overview of signals  Notifications sent to a process  UNIX signal names and numbers  Ways to generate.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems CSE 411 CPU Management Sept Lecture 9 Instructor: Bhuvan Urgaonkar.
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Operating Systems Process Creation
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
Signals and Signal Handling. Signals A predefined message sent between two processes or from the kernel to a process, or by a user to a process A software.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Interprocess Communication Mechanisms. IPC Signals Pipes System V IPC.
Operating Systems Recitation 4, April th, 2002 Signals.
Signals. Introduction r A signal is a mechanism for notifying a process that an event has occurred. m When a signal is sent to a process is normal execution.
4300 Lines Added 1800 Lines Removed 1500 Lines Modified PER DAY DURING SUSE Lab.
CSC Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 22, class 5.
© 숙대 창병모 1 제 10 장 신호 (Signal). © 숙대 창병모 2 Contents 1. Signal Concepts 2. signal() 3. Interrupted System Calls 4. kill() /raise() 5. alarm() pause() 6.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
CS 241 Section Week #10 (04/01/10) ‏. Topics This Section  MP5 Overview  Signals.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Operating Systems Practical Session 3, Signals 1.
Operating Systems Summer Semester 2011 Practical Session 2, Signals 1.
Operating Systems Inter-Process Communication Signals Moti Geva
UNIX signals.
Recitation 7 – 3/18/02 Outline fork and scheduling Signals
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Signals What is a Signal?
Task Control: Signals and Alarms Chapter 7 and 8
Unix Process Management
Practical Session 3, Signals
Chapter 4: Threads.
CSC Advanced Unix Programming, Fall 2015
Exceptional Control Flow
Exceptional Control Flow
CSE 451: Operating Systems Spring 2006 Module 4 Processes
Signals.
Presentation transcript:

1 Signals (continued) CS 241 April 9, 2012 University of Illinois

2 Signals A signal is an asynchronous notification of an event Asynchronous: could occur at any time Interrupts receiving process; jumps to signal handler in that process A (limited) menu of event types to pick from What events could be asynchronous? message arrives on my machine  Mailing agent (user) process should retrieve it Invalid memory access  OS should inform scheduler to remove process from the processor Alarm clock goes off  Process which sets the alarm should catch it

3 Signaling overview Process 1 Process 2 KILL, STOP CHLD, SEGV,... Kernel 1. Generate a signal 2. Kernel state 3. Deliver signal Most signals

4 Signaling overview Process 1 Process 2 KILL, STOP CHLD, SEGV,... Kernel 1. Generate a signal 2. Kernel state 3. Deliver signal Most signals

5 Generating a signal Generated by a process with syscall kill(pid, signal) Sends signal to process pid Poorly named: sends any signal, not just SIGKILL Generated by the kernel, when... a child process exits or is stops (SIGCHLD) floating point exception, e.g. div. by zero (SIGFPE) bad memory access (SIGSEGV)...

6 Signals from the command line: kill kill -l Lists the signals the system understands kill [-signal] pid Sends signal to the process with ID pid Optional argument signal may be a name or a number (default is SIGTERM) kill -9 pid or kill -KILL pid or kill -SIGKILL pid Unconditionally terminates process pid

7 Signals in the interactive terminal Control-C is SIGINT Interactive attention signal Control-Z is SIGSTOP Execution stopped – cannot be ignored Control-Y is SIGCONT Execution continued if stopped Control-\ is SIGQUIT Interactive termination: core dump

8 A program can signal itself Similar to raising an exception  raise(signal) or  kill(getpid(), signal) Or can signal after a delay  unsigned alarm(unsigned seconds);  Calls are not stacked any previously set alarm() is cancelled  alarm(20) Send SIGALRM to calling process after 20 seconds  alarm(0) cancels current alarm

9 Example: What does this do? Example of program signaling itself “Infinite” loop for 10 seconds Then interrupted by alarm Doesn’t matter that while loop is still looping No signal handler set by program; default action: terminate int main(void) { alarm(10); while(1); }

10 Morbid example #include #include int main(int argc, char** argv) { while (1) { if (fork()) sleep(30); else kill(getppid(), SIGKILL); } } What does this do?

11 Signaling overview Process 1 Process 2 KILL, STOP CHLD, SEGV,... Kernel 1. Generate a signal 2. Kernel state 3. Deliver signal Most signals

12 Kernel state A signal is related to a specific process In the process’s PCB (process control block), kernel stores Set of pending signals  Generated but not yet delivered Set of blocked signals  Will stay pending  Delivered after unblocked (if ever) An action for each signal type  What to do to deliver the signal

13 Kernel signaling procedure Signal arrives Set pending bit for this signal Only one bit per signal type! Ready to be delivered Pick a pending, non-blocked signal and execute the associated action – one of:  Ignore  Kill process  Execute signal handler specified by process

14 Signaling overview Process 1 Process 2 KILL, STOP CHLD, SEGV,... Kernel 1. Generate a signal 2. Kernel state 3. Deliver signal Most signals

15 Delivering a signal Kernel may handle it Not delivered to target program at all! SIGSTOP, SIGKILL Target process can’t handle these They are really messages to the kernel about a process, rather than messages to a process But for most signals, target process handles it (if it wants)

16 If process handles the signal... Signal Generated Process Signal delivered if signal not blocked by signal mask... Signal Caught by handler Return from Signal Handler Signal Mask Signal Handler Signal Mask Process Resumes Signal Mask

17 Signal mask Temporarily prevents select types of signals from being delivered Implemented as a bit array Same as kernel’s representation of pending and blocked signals SigIntSigQuitSigKill…SigContSigAbrt101…10

18 Signal mask example Block all signals: Instead of sigfillset, you might try: sigemptyset sigaddset sigdelset sigismember sigset_t sigs; sigfillset(&sigs); sigprocmask(SIG_SETMASK, &sigs, NULL);

19 If it’s not masked, we handle it Three ways to handle Ignore it  Different than blocking! Kill process Run specified signal handler function One of these is the default Depends on signal type Tell the kernel what we want to do: signal() or sigaction()

20 sigaction Changes the action taken by a process when it receives a specific signal Notes signum is any valid signal except SIGKILL and SIGSTOP If act is non-null, new action is installed from act If oldact is non-null, previous action is saved in oldact #include int sigaction(int signum, const struct sigaction * act, struct sigaction * oldact);

21 Example: Catch SIGINT #include #include void handle(int sig) { char handmsg[] = "Ha! Handled!!!\n"; int msglen = sizeof(handmsg); write(2, handmsg, msglen); } int main(int argc, char** argv) { struct sigaction sa; sa.sa_handler = handle; /* the handler function!! */ sa.sa_flags = 0; sigemptyset(&sa.sa_mask); /* block all signals during handler */ sigaction(SIGINT, &sa, NULL); while (1) { printf("Fish.\n"); sleep(1); } } Note: Need to check for error conditions in all these system & library calls!

22 Potentially unexpected behavior Inside kernel, only one pending signal of each type at a time If another arrives while first one still pending, second is lost What’s an interesting thing that could happen during a signal handler? Another signal arrives! Need to either  Write code that does not assume mutual exclusion, or  Block signals during signal handler (signal() and sigaction() can do this for you)

23 How to catch without catching Can wait for a signal No longer an asynchronous event, so no handler! First block all signals Then call sigsuspend() or sigwait() Atomically unblocks signals and waits until signal occurs Looks a lot like condition variables, eh?  cond_wait() unlocks mutex and waits till condition occurs

24 Puzzle: Using signals to send a stream of data