shell program that runs other programs cs 251 fa fall
play

shell: program that runs other programs CS 251 Fa Fall 2019 2019 - PowerPoint PPT Presentation

shell: program that runs other programs CS 251 Fa Fall 2019 2019 CS 240 Spring 2020 Principles of Programming Languages Foundations of Computer Systems Ben Ben W Wood Ben Wood Shells and Signals https://cs.wellesley.edu/~cs240/s20/


  1. λ shell: program that runs other programs CS 251 Fa Fall 2019 2019 CS 240 Spring 2020 Principles of Programming Languages Foundations of Computer Systems Ben Ben W Wood 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 Original Unix shell (Stephen Bourne, AT&T Bell Labs, 1977) sh [0] “ Bourne-Again” Shell, widely used bash default on most Unix/Linux/Mac OS X systems many others... init [1] while (true) { Print command prompt. Daemon Read command line from user. Login shell 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: Wait for child to complete. Grandchild Grandchild } Shells and Signals 3 Shells and Signals 4

  2. 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 optional optional Signals Sending/receiving a signal 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 Reasons: ID is entire message 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-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 Reactions: 14 SIGALRM Timer signal Terminate Yes 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

  3. Another view of signal handlers as optional optional Signals handlers as concurrent flows concurrent flows Signal handlers run concurrently with main program (in same process). Process A Process B Signal delivered user code (main) I curr context switch kernel code Process A Process A Process B user code (main) while (1) handler(){ ; … kernel code context switch } Signal received user code (handler) kernel code Time I next user code (main) Shells and Signals 9 Shells and Signals 10 optional optional Pending and blocked signals Process Groups Every process belongs to exactly one process group (default: parent's group) A signal is pending if sent but not yet received <= 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. Back- Fore- Back- pid=20 pid=32 pid=40 A process can block the receipt of certain signals ground ground ground pgid=20 pgid=32 pgid=40 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 getpgrp() pid=21 pid=22 Return process group of current process pgid=20 pgid=20 Foreground setpgid() process group 20 Change process group of a process Let's draw a picture... Shells and Signals 11 Shells and Signals 13

  4. optional optional Sending signals from the keyboard Signal demos Ctrl-C Shell: Ctrl-C sends SIGINT (Ctrl-Z sends SIGTSTP) 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 Shell pgid=10 kill Fore- Back- Back- pid=20 pid=32 pid=40 ground ground ground pgid=20 pgid=32 pgid=40 kill(pid, SIGINT); job #1 job job #2 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 internally A program that reacts to externally generated events (Ctrl-c) generated events #include <stdlib.h> #include <stdio.h> main() { #include <stdio.h> #include <signal.h> signal(SIGALRM, handler); #include <signal.h> 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); } <ctrl-c> 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 } internal.c DING DING! } > external.c Shells and Signals 26 Shells and Signals 27

  5. optional Signal summary 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend