L5: Writing Your Own Unix Shell October 16, 2006L5: Tiny Shell (tsh)Related Background Review (& Preview)Process Controlfork: Creating New Processesexec: Loading and Running Programswaitpid(): Waiting for a Specific ProcessSignalsSignals : How it actually worksSlide 10L5: Shell LabYour taskOverviewSlide 14Reaping Child ProcessRace HazardsRace Hazards (Solution?)Pop QuizL5: Writing Your Own Unix Shell October 16, 2006L5: Writing Your Own Unix Shell October 16, 2006TopicsTopicsL5: Shell LabProcessesSignalsRemindersRemindersShell Lab Due Oct 25, 2006 (wed)Section A (Donnie Kim) recitation6.ppt (some slides courtesy of Kun Gao(S05) and Minglong Shao(F04)15-213“The course that gives CMU its Zip!”– 2 –15-213, F’06L5: Tiny Shell (tsh)L5: Tiny Shell (tsh)Things to learn from this labThings to learn from this lab::Process Control (Ch 8)Process ID (PID) and Process Group IDParent and Child processLoading and running programfork(), execve(), waitpid()Signals (Ch 8)Sending and receiving signalsPending signalBlocking/unblocking signal (avoiding race hazards)I/O redirection (Ch 11) dup2()– 3 –15-213, F’06Related Background Review (& Preview) Related Background Review (& Preview)– 4 –15-213, F’06Process ControlProcess ControlProcess ID, Process Group ID and Parent Process IDProcess ID, Process Group ID and Parent Process IDEach process has its own, unique process IDpid_t getpid(void); // returns my pidEvery process belong to exactly one process grouppid_t getpgrp(void); // returns my prg idProcess creates process (parent – child)pid_t getppid(void); // returns my parent’s pid[dhjkim@bluefish tshlab-handout]$ ps -jfUID PID PPID PGID SID C STIME TTY TIME CMDdhjkim 5469 5465 5469 5469 0 00:17 pts/7 00:00:00 -tcshdhjkim 6284 5469 6284 5469 99 00:54 pts/7 02:58:42 ./testdhjkim 10139 5469 10139 5469 0 03:53 pts/7 00:00:00 ps -jf– 5 –15-213, F’06fork: Creating New Processesfork: Creating New Processesint fork(void)int fork(void)creates a new process (child process) that is identical to the calling process (parent process)returns 0 to the child processreturns child’s pid to the parent process if (fork() == 0) { printf("hello from child\n");} else { printf("hello from parent\n");}Fork is interesting(and often confusing)because it is calledonce but returns twiceAny Scheduling order is Possible!Any Scheduling order is Possible!First parent then child or first child then parent can be executed depending on how OS scheduler decides– 6 –15-213, F’06exec: Loading and Running Programsexec: Loading and Running Programsmain() { if (fork() == 0) { execve("/usr/bin/ls", NULL, NULL); } wait(NULL); exit();}int execint execveve(char *(char *fnamefname, char *arg, char *argv[]v[], char , char *envp[]*envp[]))New Program (*fname) overwrites its state and takes over the process’ PID– 7 –15-213, F’06waitpid(): Waiting for a Specific Process waitpid(): Waiting for a Specific Process waitpid(pid, &status, options)Can wait for specific process, and reap terminated child processVarious optionspid > 0: wait for process with PID=pid -1: wait for any processpid < -1: wait for any process from group abs(pid)By default, waitpid blocks until at least one zombie process becomes available.options:WNOHANG: return immediately if no zombies availableWUNTRACED: also return if some process has been stoppedWNOHANG|WUNTRACED combination is very useful in the shell lab:it detects all the necessary events, and doesn’t block if no ‘’events’’– 8 –15-213, F’06Signals Signals How to send signals How to send signals To a single processint kill(pid_t pid, int sig)To every process in group abs(gid)int kill(pid_t gid, int sig) // gid < 0pid_t getpid(void); // returns my pidHow to receive signals How to receive signals Signal handlerhandler_t *signal (int signum, handler_t *handler)How to block and unblock signalsHow to block and unblock signalsExplicitly Blocking Signalsint sigprocmask(int how, …, sigset_t *oldset)– 9 –15-213, F’06Signals : How it actually works Signals : How it actually works OS Kernelblockedpending1Process 1Process 2other eventsOS signal managerkill(pid, SIGINT)• divide by zero: SIGFPE• ctrl-c: SIGINT• child process exit: SIGCHLD– 10 –15-213, F’06Signals : How it actually works Signals : How it actually works OS KernelblockedpendingProcess 2OS signal manager0Process 2 first checks pending/blocked vector when it gets scheduled1– 11 –15-213, F’06L5: Shell LabL5: Shell Lab– 12 –15-213, F’06Your taskYour taskeval()eval() : Main routine that parses and interprets the : Main routine that parses and interprets the command line command line [300 lines, including helper functions][300 lines, including helper functions]sigchld_handlersigchld_handler: Catches SIGCHILD signals [15 lines]: Catches SIGCHILD signals [15 lines]sigint_handlersigint_handler: Catches SIGINT(ctrl-c) signals [15 lines]: Catches SIGINT(ctrl-c) signals [15 lines]sigint_handlersigint_handler: Catches SIGSTP(ctrl-z) signals [15 lines]: Catches SIGSTP(ctrl-z) signals [15 lines]– 13 –15-213, F’06Overview Overview eval() :eval() :Fore-groundjobBack-groundjob #1Back-groundjob #2ShellChild Childpid=10pgid=10Foregroundprocess group 20Backgroundprocess group 32Backgroudprocess group 40pid=20pgid=20pid=32pgid=32pid=40pgid=40pid=21pgid=20pid=22pgid=20Each job should have a unique process group idint setpgid(pid_t pid, pid_t pgid);setpgid(0, 0);– 14 –15-213, F’06Overview Overview eval() :eval() :Fore-groundjobBack-groundjob #1Back-groundjob #2tshChild Childpid=10pgid=10Foregroundprocess group 20Backgroundprocess group 32Backgroudprocess group 40pid=20pgid=20pid=32pgid=32pid=40pgid=40pid=21pgid=20pid=22pgid=20UNIXshellpid=5pgid=5Foreground jobreceives SIGINT, SIGTSTP, when you type ctrl-c, ctrl-zForward signalsint kill(pid_t pid, int sig)pid > 0: send sig to process with PID=pidpid = 0: send sig to all processes in my grouppid = -1: send sig to all processes with PID>1pid < -1: send sig to group abs(pid)– 15 –15-213, F’06Reaping Child ProcessReaping Child ProcessWhen fg or bg job is finished (child process When fg or bg job is finished (child process terminated) shell (parent proces) has to reap the terminated) shell (parent proces) has to reap the child,
View Full Document