excep onal control flow signals and nonlocal jumps 15 213
play

Excep&onal Control Flow: Signals and Nonlocal Jumps - PowerPoint PPT Presentation

Carnegie Mellon Excep&onal Control Flow: Signals and Nonlocal Jumps 15-213: Introduc;on to Computer Systems 15 th Lecture, Oct. 20, 2015 Instructors:


  1. Carnegie Mellon Excep&onal ¡Control ¡Flow: ¡ ¡ Signals ¡and ¡Nonlocal ¡Jumps ¡ ¡ 15-­‑213: ¡Introduc;on ¡to ¡Computer ¡Systems ¡ 15 th ¡Lecture, ¡Oct. ¡20, ¡2015 ¡ Instructors: ¡ ¡ Randal ¡E. ¡Bryant ¡and ¡David ¡R. ¡O’Hallaron ¡ 1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  2. Carnegie Mellon ECF ¡Exists ¡at ¡All ¡Levels ¡of ¡a ¡System ¡ ¢ Excep&ons ¡ § Hardware ¡and ¡opera;ng ¡system ¡kernel ¡soNware ¡ Previous ¡Lecture ¡ ¢ Process ¡Context ¡Switch ¡ § Hardware ¡;mer ¡and ¡kernel ¡soNware ¡ ¢ Signals ¡ This ¡Lecture ¡ § Kernel ¡soNware ¡and ¡applica;on ¡soNware ¡ Textbook ¡and ¡ ¡ ¢ Nonlocal ¡jumps ¡ supplemental ¡slides ¡ § Applica;on ¡code ¡ 2 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  3. Carnegie Mellon Today ¡ ¢ Shells ¡ ¢ Signals ¡ ¢ Nonlocal ¡jumps ¡ 3 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  4. Carnegie Mellon Linux ¡Process ¡Hierarchy ¡ [0] init [1] … ¡ Daemon Login shell Login shell e.g. httpd Child Child Child Note: ¡you ¡can ¡view ¡the ¡ Grandchild Grandchild hierarchy ¡using ¡the ¡Linux ¡ pstree ¡command ¡ 4 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  5. � � Carnegie Mellon Shell ¡Programs ¡ ¢ A ¡ shell ¡is ¡an ¡applica&on ¡program ¡that ¡runs ¡programs ¡on ¡behalf ¡ of ¡the ¡user. ¡ § sh ¡ ¡ ¡ ¡Original ¡Unix ¡shell ¡(Stephen ¡Bourne, ¡AT&T ¡Bell ¡Labs, ¡1977) ¡ § csh/tcsh BSD ¡Unix ¡C ¡shell ¡( ¡ § bash “ Bourne-­‑Again” ¡Shell (default ¡Linux ¡shell) ¡ int main() � Execu)on ¡is ¡a ¡ { � char cmdline[MAXLINE]; /* command line */ � sequence ¡of ¡read/ evaluate ¡steps ¡ while (1) { � /* read */ � printf("> "); � Fgets(cmdline, MAXLINE, stdin); � if (feof(stdin)) � exit(0); � /* evaluate */ � eval(cmdline); � } � } shellex.c 5 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  6. � � � Carnegie Mellon Simple ¡Shell ¡ eval ¡Func&on ¡ void eval(char *cmdline) � { � char *argv[MAXARGS]; /* Argument list execve() */ � char buf[MAXLINE]; /* Holds modified command line */ � int bg; /* Should the job run in bg or fg? */ � pid_t pid; /* Process id */ � strcpy(buf, cmdline); � bg = parseline(buf, argv); � if (argv[0] == NULL) � return; /* Ignore empty lines */ � if (!builtin_command(argv)) { � if ((pid = Fork()) == 0) { /* Child runs user job */ � if (execve(argv[0], argv, environ) < 0) { � printf("%s: Command not found.\n", argv[0]); � exit(0); � } � } � /* Parent waits for foreground job to terminate */ � � if (!bg) { � int status; � if (waitpid(pid, &status, 0) < 0) � unix_error("waitfg: waitpid error"); � } � else � printf("%d %s", pid, cmdline); � } � return; � } � 6 shellex.c Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  7. Carnegie Mellon Problem ¡with ¡Simple ¡Shell ¡Example ¡ ¢ Our ¡example ¡shell ¡correctly ¡waits ¡for ¡and ¡reaps ¡foreground ¡ jobs ¡ ¢ But ¡what ¡about ¡background ¡jobs? ¡ § Will ¡become ¡zombies ¡when ¡they ¡terminate ¡ § Will ¡never ¡be ¡reaped ¡because ¡shell ¡(typically) ¡will ¡not ¡terminate ¡ § Will ¡create ¡a ¡memory ¡leak ¡that ¡could ¡run ¡the ¡kernel ¡out ¡of ¡memory ¡ 7 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  8. Carnegie Mellon ECF ¡to ¡the ¡Rescue! ¡ ¢ Solu&on: ¡Excep&onal ¡control ¡flow ¡ § The ¡kernel ¡will ¡interrupt ¡regular ¡processing ¡to ¡alert ¡us ¡when ¡a ¡background ¡ process ¡completes ¡ § In ¡Unix, ¡the ¡alert ¡mechanism ¡is ¡called ¡a ¡ signal ¡ 8 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  9. Carnegie Mellon Today ¡ ¢ Shells ¡ ¢ Signals ¡ ¢ Nonlocal ¡jumps ¡ 9 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  10. Carnegie Mellon Signals ¡ ¢ A ¡ signal ¡is ¡a ¡small ¡message ¡that ¡no&fies ¡a ¡process ¡that ¡an ¡ event ¡of ¡some ¡type ¡has ¡occurred ¡in ¡the ¡system ¡ § Akin ¡to ¡excep;ons ¡and ¡interrupts ¡ § Sent ¡from ¡the ¡kernel ¡(some;mes ¡at ¡the ¡request ¡of ¡another ¡process) ¡to ¡a ¡ process ¡ § Signal ¡type ¡is ¡iden;fied ¡by ¡small ¡integer ¡ID’s ¡(1-­‑30) ¡ § Only ¡informa;on ¡in ¡a ¡signal ¡is ¡its ¡ID ¡and ¡the ¡fact ¡that ¡it ¡arrived ¡ ID ¡ Name ¡ Default ¡Ac)on ¡ Corresponding ¡Event ¡ 2 ¡ SIGINT ¡ Terminate ¡ User ¡typed ¡ctrl-­‑c ¡ ¡ 9 ¡ SIGKILL ¡ Terminate ¡ Kill ¡program ¡(cannot ¡override ¡or ¡ignore) ¡ 11 ¡ SIGSEGV ¡ Terminate ¡& ¡Dump ¡ Segmenta;on ¡viola;on ¡ 14 ¡ SIGALRM ¡ Terminate ¡ Timer ¡signal ¡ 17 ¡ SIGCHLD ¡ Ignore ¡ Child ¡stopped ¡or ¡terminated ¡ 10 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  11. Carnegie Mellon Signal ¡Concepts: ¡Sending ¡a ¡Signal ¡ ¢ Kernel ¡ sends ¡(delivers) ¡a ¡signal ¡to ¡a ¡ des)na)on ¡process ¡by ¡ upda&ng ¡some ¡state ¡in ¡the ¡context ¡of ¡the ¡des&na&on ¡process ¡ ¢ Kernel ¡sends ¡a ¡signal ¡for ¡one ¡of ¡the ¡following ¡reasons: ¡ § Kernel ¡has ¡detected ¡a ¡system ¡event ¡such ¡as ¡divide-­‑by-­‑zero ¡(SIGFPE) ¡or ¡the ¡ termina;on ¡of ¡a ¡child ¡process ¡(SIGCHLD) ¡ § Another ¡process ¡has ¡invoked ¡the ¡ kill ¡system ¡call ¡to ¡explicitly ¡request ¡ the ¡kernel ¡to ¡send ¡a ¡signal ¡to ¡the ¡des;na;on ¡process ¡ 11 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  12. Carnegie Mellon Signal ¡Concepts: ¡Receiving ¡a ¡Signal ¡ ¢ A ¡des&na&on ¡process ¡ receives ¡a ¡signal ¡when ¡it ¡is ¡forced ¡by ¡ the ¡kernel ¡to ¡react ¡in ¡some ¡way ¡to ¡the ¡delivery ¡of ¡the ¡signal ¡ ¢ Some ¡possible ¡ways ¡to ¡react: ¡ § Ignore ¡the ¡signal ¡(do ¡nothing) ¡ § Terminate ¡the ¡process ¡(with ¡op;onal ¡core ¡dump) ¡ § Catch ¡ the ¡signal ¡by ¡execu;ng ¡a ¡user-­‑level ¡func;on ¡called ¡ signal ¡handler ¡ § Akin ¡to ¡a ¡hardware ¡excep;on ¡handler ¡being ¡called ¡in ¡response ¡to ¡an ¡ asynchronous ¡interrupt: ¡ ¡ (1) Signal received (2) Control passes � by process � to signal handler � I curr � I next � (3) Signal handler runs � (4) Signal handler � returns to � next instruction � 12 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  13. Carnegie Mellon Signal ¡Concepts: ¡Pending ¡and ¡Blocked ¡Signals ¡ ¢ A ¡signal ¡is ¡ pending ¡if ¡sent ¡but ¡not ¡yet ¡received ¡ § There ¡can ¡be ¡at ¡most ¡one ¡pending ¡signal ¡of ¡any ¡par;cular ¡type ¡ § Important: ¡Signals ¡are ¡not ¡queued ¡ § If ¡a ¡process ¡has ¡a ¡pending ¡signal ¡of ¡type ¡k, ¡then ¡subsequent ¡signals ¡of ¡ type ¡k ¡that ¡are ¡sent ¡to ¡that ¡process ¡are ¡discarded ¡ ¢ A ¡process ¡can ¡ block ¡the ¡receipt ¡of ¡certain ¡signals ¡ § Blocked ¡signals ¡can ¡be ¡delivered, ¡but ¡will ¡not ¡be ¡received ¡un;l ¡the ¡signal ¡ is ¡unblocked ¡ ¢ A ¡pending ¡signal ¡is ¡received ¡at ¡most ¡once ¡ 13 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

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