signals and jumps
play

Signals and Jumps arg0 , , argn argv[0] , , argv[n] Argument list - PDF document

Recall: Running a New Program int execl(char *path, char *arg0, , char *argn, char *null) Loads & runs executable: path is the complete path of an executable arg0 becomes the name of the process Signals and Jumps arg0 ,


  1. Recall: Running a New Program int execl(char *path, char *arg0, …, char *argn, char *null) – Loads & runs executable: • path is the complete path of an executable • arg0 becomes the name of the process Signals and Jumps • arg0 , …, argn → argv[0] , …, argv[n] • Argument list terminated by a NULL argument – Returns -1 if error, otherwise doesn ’ t return! CSAPP2e, Chapter 8 if (fork() == 0) execl("/usr/bin/cp", "cp", "foo", "bar", NULL); else printf("hello from parent\n"); CIS 330 W9 Signals and Jumps Interprocess CommunicaLon The World of MulLtasking ✧ SynchronizaLon allows very limited communicaLon ✧ System Runs Many Processes Concurrently – Process: execuLng program • State consists of memory image + register values + program counter ✧ Pipes: – ConLnually switches from one process to another – One-way communicaLon stream that mimics a file in each process: • Suspend process when it needs I/O resource or Lmer event occurs one output, one input • Resume process when I/O available or given scheduling priority – See man 7 pipe – Appears to user(s) as if all processes execuLng simultaneously ✧ Sockets: • Even though most systems can only execute one process at a Lme – A pair of communicaLon streams that processes connect to • Except possibly with lower performance than if running alone – See man 7 socket CIS 330 W9 Signals and Jumps CIS 330 W9 Signals and Jumps UNIX Startup: 1 Programmer ’ s Model of MulLtasking ✧ Pushing reset bu_on loads the PC with the address of a small bootstrap program ✧ Basic FuncLons Bootstrap program loads the boot block (disk block 0) ✧ Boot block program loads kernel from disk – fork() spawns new process ✧ Boot block program passes control to kernel • Called once, returns twice ✧ Kernel handcrabs the data structures for process 0 – exit() terminates own process ✧ • Called once, never returns • Puts process into “ zombie ” status – wait() and waitpid() wait for and reap terminated children – execl() and execve() run a new program in an exisLng process • Called once, (normally) never returns ✧ Programming Challenge Process 0: handcrafted kernel process [0] – Understanding the nonstandard semanLcs of the funcLons – Avoiding improper use of system resources • E.g., “ Fork bombs ” can disable a system Process 1: user mode process init [1] fork() and exec(/sbin/init) CIS 330 W9 Signals and Jumps CIS 330 W9 Signals and Jumps

  2. UNIX Startup: 2 UNIX Startup: 3 [0] [0] init forks new processes as per init [1] the /etc/inittab file init [1] Daemons Forks getty (get tty or get terminal) Daemons getty execs a login program getty login e.g., sshd for the console e.g., sshd CIS 330 W9 Signals and Jumps CIS 330 W9 Signals and Jumps UNIX Startup: 4 Shell Programs ✧ A shell is an applicaLon program that runs programs on behalf of user [0] – sh – Original Unix Bourne Shell – csh – BSD Unix C Shell, tcsh – Enhanced C Shell – bash – Bourne-Again Shell init [1] – ksh – Korn Shell int main(void) { login gets user ’ s uid & password char cmdline[MAXLINE]; Read-evaluate loop: Daemons while (true) { shell • If OK, it execs appropriate shell e.g., sshd /* read */ • If not OK, it execs getty an interpreter! printf("> "); Fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0); /* evaluate */ eval(cmdline); } CIS 330 W9 Signals and Jumps CIS 330 W9 Signals and Jumps } Simple Shell eval FuncLon Problem with Simple Shell Example void eval(char *cmdline) { char *argv[MAXARGS]; /* argv for execve() */ ✧ Correctly waits for & reaps foreground jobs bool bg; /* should the job run in bg or fg? */ pid_t pid; /* process id */ int status; /* child status */ ✧ But what about background jobs? bg = parseline(cmdline, argv); – Will become zombies when they terminate if (!builtin_command(argv)) { if ((pid = Fork()) == 0) { /* child runs user job */ – Will never be reaped because shell (typically) will not terminate if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); – Creates a process leak that will eventually prevent the forking of new exit(0); processes } } if (!bg) { /* parent waits for fg job to terminate */ if (waitpid(pid, &status, 0) < 0) ✧ SoluLon: Reaping background jobs requires a mechanism unix_error("waitfg: waitpid error"); called a signal } else /* otherwise, don ’ t wait for bg job */ printf("%d %s", pid, cmdline); } } CIS 330 W9 Signals and Jumps CIS 330 W9 Signals and Jumps

  3. Signals Signals: Sending ✧ A signal is a small message that noLfies a process that an event of some type has occurred in the system ✧ OS kernel sends a signal to a desLnaLon process by updaLng – Kernel abstracLon for excepLons and interrupts some state in the context of the desLnaLon process – Sent from the kernel (someLmes at the request of another process) to a process ✧ Reasons: – Different signals are idenLfied by small integer ID ’ s – Typically, the only informaLon in a signal is its ID and the fact that it arrived – OS detected an event – Another process used the kill system call to explicitly request the kernel to send a signal to the desLnaLon process ID Name Default Action Corresponding Event 2 SIGINT Terminate Keyboard interrupt ( ctrl-c ) 9 SIGKILL Terminate Kill program 11 SIGSEGV Terminate & Dump Segmentation violation 14 SIGALRM Terminate Timer signal 18 SIGCHLD Ignore Child stopped or terminated CIS 330 W9 Signals and Jumps CIS 330 W9 Signals and Jumps Signals: Receiving Signals: Pending & Blocking ✧ DesLnaLon process receives a signal when it is forced by the ✧ Signal is pending if sent, but not yet received kernel to react in some way to the delivery of the signal – At most one pending signal of any parLcular type – Important: Signals are not queued • If process has pending signal of type k, then process discards subsequent ✧ Three ways to react: signals of type k – Ignore the signal – A pending signal is received at most once – Terminate the process (& opLonally dump core) – Catch the signal with a user-level signal handler ✧ Process can block the receipt of certain signals – Blocked signals can be delivered, but will not be received unLl the signal is unblocked CIS 330 W9 Signals and Jumps CIS 330 W9 Signals and Jumps Signals: Pending & Blocking Process Groups Each process One group in foreground belongs to exactly ✧ Kernel maintains pending & blocked bit vectors in each one process group pid=10 process context Shell pgid=10 ✧ pending – represents the set of pending signals Back- – Signal type k delivered → kernel sets kth bit pid=20 Fore- Back- pid=32 pid=40 ground pgid=20 ground ground pgid=32 pgid=40 – Signal type k received → kernel clears kth bit job job #1 job #2 ✧ blocked – represents the set of blocked signals Background Background process group 32 process group 40 – ApplicaLon sets & clears bits via sigprocmask() Child Child getpgrp() – Return process group of pid=21 pid=22 current process pgid=20 pgid=20 setpgid() – Change process group of a process Foreground process group 20 CIS 330 W9 Signals and Jumps CIS 330 W9 Signals and Jumps

Recommend


More recommend