+ All Categories
Home > Documents > 4061 Session 14 (3/1)

4061 Session 14 (3/1)

Date post: 17-Jan-2016
Category:
Upload: madge
View: 37 times
Download: 0 times
Share this document with a friend
Description:
4061 Session 14 (3/1). Today. Signals Process Groups and Sessions. Today’s Objectives. Define a signal and explain its purpose Explain what happens in the operating system and in a user process when a signal is generated Write and read code to raise, catch, block, and ignore signals in C - PowerPoint PPT Presentation
Popular Tags:
24
4061 Session 14 (3/1)
Transcript
Page 1: 4061 Session 14 (3/1)

4061 Session 14 (3/1)

Page 2: 4061 Session 14 (3/1)

Today

• Signals

• Process Groups and Sessions

Page 3: 4061 Session 14 (3/1)

Today’s Objectives• Define a signal and explain its purpose

• Explain what happens in the operating system and in a user process when a signal is generated

• Write and read code to raise, catch, block, and ignore signals in C

• Understand process groups and sessions– Explain how a shell assigns process groups to

a pipeline of processes, and why

Page 4: 4061 Session 14 (3/1)

Admin

• Quiz 2: Answers online

• Quiz 3: Next Thursday

Page 5: 4061 Session 14 (3/1)

Killing Processes

• kill [ -s signal ] pid …

Page 6: 4061 Session 14 (3/1)

What’s Going on Here?

• Kill is sending a “signal” to the process

• A signal is the operating system telling a process to stop whatever it is doing and deal with a message– Thus, signals are asynchronous with the

process

• By default, the process terminates

• But we can override the default behavior

Page 7: 4061 Session 14 (3/1)

What are Signals Used For?

• Used by the OS to notify processes that an event has occurred

• This is nice because the process does not have to poll for events

Page 8: 4061 Session 14 (3/1)

Signal Types

• There are many signal types– man 7 signal

• Signals can be generated interactively with special chars in the terminal– ctrl-c: SIGINT (terminate)– ctrl-z: SIGTSTP (suspend)– Others defined: stty -a

• How do these keyboard-generated signals (asynchonously) find their way to the program immediately?

Page 9: 4061 Session 14 (3/1)

Signal Vocabulary

• A signal is generated when the event causing the signal occurs

• A signal is delivered when the process actually takes action

• Note: there can be real time between generation and delivery. (This gap is known as the signal’s lifetime)

• A process can catch a signal (by defining a signal handler), block a signal (queue the signal for later delivery), or ignore a signal (ignore the signal permanently).

Page 10: 4061 Session 14 (3/1)

Defaults

• If we don’t catch a signal, a default action takes place– abort– abort w/ core dump– stop– ignore

• Override the defaults by catching, blocking, or ignoring particular signals– Except SIGKILL and SIGSTOP: defaults cannot be

overridden

Page 11: 4061 Session 14 (3/1)

Sending Signals

• We’ve seen how to do it from the command line: kill– Exclude the “SIG” prefix for the signal name– E.g. kill -KILL 7236

• In C:– int kill(pid_t pid, int signal)– pid > 0: specified process id– pid == 0: current process group– pid == -1: all procs except init– pid < -1: process group -pid

Page 12: 4061 Session 14 (3/1)

Raising Signals

• Sending a signal to the current process is called “raising a signal”

int raise(int signal);• Just like: kill(getpid(), signal);• In a single-threaded program, raising a

signal is synchronous– If it’s not blocked or ignored, delivered before

next instruction

Page 13: 4061 Session 14 (3/1)

Blocking Signals

• Blocked signals remain pending until unblocked– Note: we can only remember last signal

• A pending signal is immediately delivered when unblocked

• int sigprocmask(int how, sigset_t *set, sigset_t *oldset)

• How is:– SIG_BLOCK– SIG_UNBLOCK– SIG_SETMASK

Page 14: 4061 Session 14 (3/1)

Signal Sets

• Manipulating signal sets– sigaddset– sigdelset– sigemptyset– sigfillset– sigismember

Page 15: 4061 Session 14 (3/1)

Handling Signals

• The easy way to do it is with “signal”sighandler_t signal(int signum, sighandler_t handler);

• But this way has some serious problems

• So, before we get to the more complex (and better) way, some more structure

Page 16: 4061 Session 14 (3/1)

sigaction

int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);

struct sigaction { void (*sa_handler)(int);void (*sa_sigaction)(int, siginfo_t *, void *);sigset_t sa_mask;

int sa_flags; void (*sa_restorer)(void);}

Page 17: 4061 Session 14 (3/1)

Waiting for a Signal

• pause, sigsuspend, and sigwait

Page 18: 4061 Session 14 (3/1)

System Shutdown

• First send a TERM signal to all processes, wait for a grace period, then send a KILL signal.– KILL is a signal that cannot be handled

Page 20: 4061 Session 14 (3/1)

Process Groups

• Used for the distribution of signals

• A process is always a member of a single process group

• A process group ID is the same as the PID of the group’s initial member– Thus, they share a “name space” with PIDs

• When created, each process is placed into the process group of its parent

Page 21: 4061 Session 14 (3/1)

Sessions

• A set of one or more process groups associated with a terminal

• Uses:– Group user terminal sessions together– Isolate daemons

• setsid

• A process can only join process groups in its own session

Page 22: 4061 Session 14 (3/1)

Shells and Process Groups

• Shells, though, create new process groups, placing related processes (e.g. those connected with a pipe) into the same group

• Steps (for first process in pipeline):– Fork (initially has same PG ID as the shell)– Child issues setpgid() to set its PG ID = PID, creating

a new process group. Child is now a “process leader”– Redirect I/O to pipe– Exec

• Subsequent processes in pipeline:– Same, but children set their PG ID to that of the first

child

Page 23: 4061 Session 14 (3/1)

Some Shell Tricks

• What if the parent creates the second child in the pipeline before the first child becomes the session leader?

• What if the first child exits before the second child issues setpgid?

Page 24: 4061 Session 14 (3/1)

Process Groups

• Read Robbins 11.5

• Additional Reading– Process Groups in BSD: http://

www.informit.com/articles/article.asp?p=366888&seqNum=8&rl=1


Recommended