Shells and Signals
CS 240251 SpringFall 2019 2020 FoundationsPrinciples of Programming of Computer Languages Systems shell: program that runs other programs λ Ben Wood
Shells and Signals
https://cs.wellesley.edu/~cs240/s20/ Shells and Signals 1 Shells and Signals 2
Shells and the process hierarchy Shell logic program that runs other programs on behalf of the user sh Original Unix shell (Stephen Bourne, AT&T Bell Labs, 1977) [0] bash “Bourne-Again” Shell, widely used default on most Unix/Linux/Mac OS X systems many others... init [1] while (true) { Print command prompt. Daemon Login shell Read command line from user. e.g. httpd Parse command line. If command is built-in, do it. Else fork process to execute command. Child Child Child in child: Exec requested command (never returns) in parent: Grandchild Grandchild Wait for child to complete. }
Shells and Signals 3 Shells and Signals 4 Terminal ≠ shell To wait or not? User interface to shell and other programs. A foreground job is a process for which the shell waits.* Graphical (GUI) vs. command-line (CLI) $ emacs fizz.txt # shell waits until emacs exits.
Command-line terminal (emulator): A background job is a process for which the shell does not wait*… yet. Input (keyboard) Output (screen, sound) $ emacs boom.txt & # emacs runs in background. [1] 9073 # shell saves background job and is… $ gdb ./umbrella # immediately ready for next command.
don't do this with emacs unless using X windows version
*Also: foregound jobs get input from (and "own") the terminal. Background jobs do not.
Shells and Signals 5 Shells and Signals 6
Signals optional Sending/receiving a signal optional Signal: small message notifying a process of event in system Kernel sends (delivers) a signal to a destination process like exceptions and interrupts by updating state in the context of the destination process. sent by kernel, sometimes at request of another process ID is entire message Reasons: System event, e.g. segmentation fault (SIGSEGV) ID Name Corresponding Event Default Action Can Another process used kill system call: Override? explicitly request the kernel send a signal to the destination process 2 SIGINT Interrupt (Ctrl-tag">C) Terminate Yes 9 SIGKILL Kill process (immediately) Terminate No Destination process receives signal when kernel forces it to react. 11 SIGSEGV Segmentation violation Terminate & Dump Yes 14 SIGALRM Timer signal Terminate Yes Reactions: 15 SIGTERM Kill process (politely) Terminate Yes Ignore the signal (do nothing) 17 SIGCHLD Child stopped or terminated Ignore Yes Terminate the process (with optional core dump) 18 SIGCONT Continue stopped process Continue (Resume) No Catch the signal by executing a user-level function called signal handler 19 SIGSTOP Stop process (immediately) Stop (Suspend) No Like an impoverished Java exception handler 20 SIGTSTP Stop process (politely) Stop (Suspend) Yes … Shells and Signals 7 Shells and Signals 8 Another view of signal handlers as Signals handlers as concurrent flows optional optional concurrent flows Signal handlers run concurrently with main program (in same process). Process A Process B
Signal delivered Icurr user code (main) Process A Process A Process B kernel code context switch user code (main) while (1) handler(){ ; … kernel code context switch } Signal received user code (handler) Time kernel code I next user code (main)
Shells and Signals 9 Shells and Signals 10
optional optional Pending and blocked signals Process Groups A signal is pending if sent but not yet received Every process belongs to exactly one process group (default: parent's group) <= 1 pending signal per type per process pid=10 Shell No Queue! Just a bit per signal type. pgid=10 Signals of type S discarded while process has S signal pending.
Fore- Back- Back- pid=20 pid=32 pid=40 pgid=20 ground ground pgid=32 ground pgid=40 A process can block the receipt of certain signals job job #1 job #2 Receipt delayed until the signal is unblocked Background Background process group 32 process group 40 Child Child
A pending signal is received at most once pid=21 pid=22 getpgrp() pgid=20 pgid=20 Return process group of current process Foreground setpgid() process group 20 Change process group of a process Let's draw a picture... Shells and Signals 11 Shells and Signals 13 Sending signals from the keyboard optional Signal demos optional
Shell: Ctrl-C sends SIGINT (Ctrl-Z sends SIGTSTP) Ctrl-C to every job in the foreground process group. SIGINT – default action is to terminate each process SIGTSTP – default action is to stop (suspend) each process Ctrl-Z pid=10 pgid=10 Shell kill
Fore- Back- Back- pid=20 pid=32 pid=40 pgid=20 ground ground pgid=32 ground pgid=40 job job #1 job #2 kill(pid, SIGINT);
Background Background process group 32 process group 40 Child Child
pid=21 pid=22 pgid=20 pgid=20 Foreground process group 20 Shells and Signals 14 Shells and Signals 15
optional optional A program that reacts to A program that reacts to internally externally generated events (Ctrl-c) generated events
#include
Shells and Signals 36