Quick viewing(Text Mode)

Shells and Signals

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 shell (Stephen Bourne, AT&T Bell Labs, 1977) [0] “Bourne-Again” Shell, widely used default on most Unix//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? to shell and other programs. A foreground job is a process for which the shell .* 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 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 #include main() { #include #include signal(SIGALRM, handler); #include alarm(1); /* send SIGALRM in int beeps = 0; 1 second */ void handler(int sig) { safe_printf("You think hitting ctrl-c will stop me?\n"); /* SIGALRM handler */ while (1) { sleep(2); void handler(int sig) { safe_printf("Well..."); safe_printf("BEEP\n"); } > ./external sleep(1); } printf("OK\n"); if (++beeps < 5) You think hitting ctrl-c will stop me? exit(0); alarm(1); > ./internal } Well...OK else { BEEP > safe_printf("DING DING!\n"); BEEP main() { exit(0); BEEP signal(SIGINT, handler); /* installs ctrl-c handler */ } BEEP while(1) { } BEEP } DING DING! } internal.c > external.c Shells and Signals 26 Shells and Signals 27 Signal summary optional Signals provide process-level exception handling Can generate from user programs Can define effect by declaring signal handler Some caveats Very high overhead >10,000 clock cycles Only use for exceptional conditions Not queued Just one bit for each pending signal type Many more complicated details we have not discussed. Book goes into too much gory detail.

Shells and Signals 36