CSE 421: Introduction to Operating Systems - Recitation 5: UNIX ...

9 downloads 289 Views 298KB Size Report
2 Oct 2013 ... Makefile and Modular Programming in C. TA: Kyungho Jeon (kyunghoj@buffalo. edu). CSE 421: Introduction to Operating Systems ...
CSE 421: Introduction to Operating Systems Recitation 5: UNIX Signals TA: Kyungho Jeon ([email protected]) University at Buffalo, the State University of New York

October 2, 2013

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

About Me

• 4th year PhD student • But TA first time • From South Korea

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Today, We will study...

• What UNIX signals are • How to generate signals • How to catch and handle signals

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Review

• UNIX/C programming • Network Socket Programming • POSIX Threads • Makefile and Modular Programming in C

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

What are (UNIX) Signals?

• Software interupts!

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

What are (UNIX) Signals?

• Software interupts! • Provide a way of handling asynchronous events

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

What are (UNIX) Signals?

• Software interupts! • Provide a way of handling asynchronous events • e.g., a terminal user types Ctrl-C to stop a running program

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

What are (UNIX) Signals?

• Software interupts! • Provide a way of handling asynchronous events • e.g., a terminal user types Ctrl-C to stop a running program • UNIX signal is a Inter-Process Communication (IPC) interface

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

What are (UNIX) Signals?

• Software interupts! • Provide a way of handling asynchronous events • e.g., a terminal user types Ctrl-C to stop a running program • UNIX signal is a Inter-Process Communication (IPC) interface • Sources of Signals: • Hardware exceptions: divide by 0 • Software conditions: SIGSEGV (segmentation fault), SIGPIPE (write to a closed socket) • Terminal (user inputs)

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Common Signals Every signal has a name and is defined by an integer SIGHUP(1) hangup - sent to a process when its controlling terminal has disconnected SIGINT(2) interrupt - Ctrl-C pressed by user SIGQUIT(3) quit - Ctrl- pressed by user SIGILL(4) Illegal instruction (default core) SIGABRT(6) Abort process SIGKILL(9) kill (cannot be caught or ignored) SIGSEGV(11) Segmentation fault SIGALRM(14) Alarm clock timeout SIGUSR1,2(10,12) User-defined signals

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Process Group: Diagram

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Process Group

A process group is a collection of one or more processes • A single foreground process group • One or more backgroup process groups • Usually associated with the same job • Receives signals from the same terminal • Every process belongs to exactly one process group.

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Signals from Keyboard

The most common way of sending signals to processes is using the keyboard: Ctrl-C Causes the system to send an SIGINT to the running process. Ctrl-Z Causes the system to send an SIGTSTP to the running process. Like PAUSE. Ctrl-\ SIGABRT

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Signals from Command Line (kill & fg)

kill(1) command $ kill [options] pid ... • -l lists all the signals you can send • -signal is a signal name or number • Please read “man kill” for details!

The fg command will resume execution of the process by sending it a CONT signal

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Signals from Command Line (kill & fg)

Demo!

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Signal Disposition

We can let the OS kernel do one of the four things on receipt of a signal

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Signal Disposition

We can let the OS kernel do one of the four things on receipt of a signal 1

Ignore the signal: SIGKILL and SIGSTOP cannot be ignored

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Signal Disposition

We can let the OS kernel do one of the four things on receipt of a signal 1

Ignore the signal: SIGKILL and SIGSTOP cannot be ignored

2

Catch the signal: We will discuss later

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Signal Disposition

We can let the OS kernel do one of the four things on receipt of a signal 1

Ignore the signal: SIGKILL and SIGSTOP cannot be ignored

2

Catch the signal: We will discuss later

3

Block the signal: OS queues signals for possible later delivery

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Signal Disposition

We can let the OS kernel do one of the four things on receipt of a signal 1

Ignore the signal: SIGKILL and SIGSTOP cannot be ignored

2

Catch the signal: We will discuss later

3

Block the signal: OS queues signals for possible later delivery

4

Let the default apply

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Default Actions

• Every signal has a default action • The default action for most signals is to terminate the process. • See the Professor’s Recitation 5 slides for details.

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Catching the Signal

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Changing Default Action

The simplest interface to the signal features of the UNIX is the signal function. Synopsis #include typedef void Sigfunc(int); Sigfunc signal(int signo, Sigfunc *func); Actions (The value of func): SIG DFL (Re)Set to default action SIG IGN Set to ignore the specified signal type *func Catch the signal and run the provided function

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Non-Catchable Signals

• Most signals may be caught by the process, but there are a

few signals that the process cannot catch, and cause the process to terminate. e.g., KILL and STOP • If you install no signal handlers of your own, the runtime

environment sets up a set of default signal handlers.

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Catching the Signal: Code

sigusr.c https://gist.github.com/kyunghoj/6778988

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Alarm Signal

• SIGALRM can be used as a kind of “alarm clock” for a process • By setting a disposition for SIGALRM, a process can set an

alarm to go off at a specified time with the call: Synopsis #include unsigned int alarm(unsigned int seconds); int pause(void);

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Alarm Signal: Code

sleep1.c https://gist.github.com/kyunghoj/6779065

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Interval Timers Unix system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and the timer (potentially) restarts. Three Timers: ITIMER REAL decrements in real time, and deliver SIGALRM upon expiration. ITIMER VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expirtion. ITIMER PROF decrements both when the process executes and when the system is executing on behalf of the process. Used to profile the time spent by the application in user and kernel space. SIGPROF is delivered upon expiration.

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Interval Timers

Synopsis #include int getitimer (int which, struct itimerval *value); int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue);

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Summary

UNIX Signals • Signal Types and Actions • Generating & Catching Signals • ALARM Signal • Interval timers

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Project 1

• Before starting your project...

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Project 1

• Before starting your project... • Set up your development environments • Dept servers? Your own machine? • Know how to write C programs • Know how to compile and run your program

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

Project 1

• Before starting your project... • Set up your development environments • Dept servers? Your own machine? • Know how to write C programs • Know how to compile and run your program • Know what you are doing • Read for Network Socket Programming

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems

References

W. Richard Stevens and Stephen Rago. Advanced Programming in the Unix Environment, 2nd ed. • There’s a newer (2013) version available...

TA: Kyungho Jeon ([email protected])

CSE 421: Introduction to Operating Systems