Duke CPS 196 - The Classical OS Model in Unix

Unformatted text preview:

The Classical OS Model in UnixThe Classical OS Model in UnixA Lasting Achievement?A Lasting Achievement?“Perhaps the most important achievement of Unix is to demonstrate that a powerful operating system for interactive use need not be expensive…it can run on hardware costing as little as $40,000.”The UNIX Time-Sharing System* D. M. Ritchie and K. ThompsonDEC PDP-11/24http://histoire.info.online.fr/pdp11.htmlElements of the UnixElements of the Unix1. rich model for IPC and I/O: “everything is a file”file descriptors: most/all interactions with the outside world are through system calls to read/write from file descriptors, with a unified set of syscalls for operating on open descriptors of different types.2. simple and powerful primitives for creating and initializing child processesfork: easy to use, expensive to implementCommand shell is an “application” (user mode)3. general support for combining small simple programs to perform complex tasksstandard I/O and pipelinesA Typical Unix File TreeA Typical Unix File Tree/tmp usretcFile trees are built by graftingvolumes from different volumesor from network servers.Each volume is a set of directories and files; a host’s file tree is the set ofdirectories and files visible to processes on a given host.bin vmunixls shproject userspackages(volume root)tex emacsIn Unix, the graft operation isthe privileged mount system call,and each volume is a filesystem.mount pointmount (coveredDir, volume)coveredDir: directory pathnamevolume: device specifier or network volumevolume root contents become visible at pathname coveredDirThe ShellThe ShellThe Unix command interpreters run as ordinary user processes with no special privilege.This was novel at the time Unix was created: other systems viewed the command interpreter as a trusted part of the OS.Users may select from a range of interpreter programs available, or even write their own (to add to the confusion).csh, sh, ksh, tcsh, bash: choose your flavor...or use perl.Shells use fork/exec/exit/wait to execute commands composed of program filenames, args, and I/O redirection symbols.Shells are general enough to run files of commands (scripts) for more complex tasks, e.g., by redirecting shell’s stdin.Shell’s behavior is guided by environment variables.Using the shellUsing the shell• Commands: ls, cat, and all that• Current directory: cd and pwd• Arguments: echo• Signals: ctrl-c• Job control, foreground, and background: &, ctrl-z, bg, fg• Environment variables: printenv and setenv• Most commands are programs: which, $PATH, and /bin• Shells are commands: sh, csh, ksh, tcsh, bash• Pipes and redirection: ls | grep a• Files and I/O: open, read, write, lseek, close• stdin, stdout, stderr• Users and groups: whoami, sudo, groupsOther application programsccOther application programsHardwareKernel shwho a.out datewcgrepedvildascompcppnroffQuestions about ProcessesQuestions about ProcessesA process is an execution of a program within a private virtual address space (VAS).1. What are the system calls to operate on processes?2. How does the kernel maintain the state of a process?Processes are the “basic unit of resource grouping”.3. How is the process virtual address space laid out?What is the relationship between the program and the process?4. How does the kernel create a new process?How to allocate physical memory for processes?How to create/initialize the virtual address space?Process InternalsProcess Internals++user IDprocess IDparent PIDsibling linkschildrenvirtual address space process descriptor (PCB)resourcesthreadstackEach process has a thread bound to the VAS.The thread has a saved user context as well as a system context.The kernel can manipulate the user context to start the thread in user mode wherever it wants.Process state includes a file descriptor table, links to maintain the process tree, and a place to store the exit status.The address space is represented by page table, a set of translations to physical memory allocated from a kernel memory manager.The kernel must initialize the process memory with the program image to run.Process CreationProcess CreationTwo ways to create a process• Build a new empty process from scratch• Copy an existing process and change it appropriatelyOption 1: New process from scratch•StepsLoad specified code and data into memory; Create empty call stackCreate and initialize PCB (make look like context-switch)Put process on ready list• Advantages: No wasted work• Disadvantages: Difficult to setup process correctly and to express all possible options Process permissions, where to write I/O, environment variablesExample: WindowsNT has call with 10 arguments[Remzi Arpaci-Dusseau]Process CreationProcess CreationOption 2: Clone existing process and change• Example: Unix fork() and exec()Fork(): Clones calling processExec(char *file): Overlays file image on calling process• Fork() Stop current process and save its stateMake copy of code, data, stack, and PCBAdd new PCB to ready listAny changes needed to PCB?• Exec(char *file)Replace current data and code segments with those in specified file• Advantages: Flexible, clean, simple• Disadvantages: Wasteful to perform copy and then overwrite of memory[Remzi Arpaci-Dusseau]Process Creation in UnixProcess Creation in Unixint pid;int status = 0;if (pid = fork()) {/* parent */…..pid = wait(&status);} else {/* child */…..exit(status);}Parent uses wait to sleep until the child exits; wait returns child pid and status.Wait variants allow wait on a specific child, or notification of stops and other signals.The fork syscall returns twice: it returns a zero to the child and the child process ID (pid) to the parent.Unix Unix Fork/Exec/Exit/WaitFork/Exec/Exit/WaitExampleExamplefork parent fork childwaitexitint pid = fork();Create a new process that is a clone of its parent.exec*(“program” [, argvp, envp]);Overlay the calling process virtual memory with a new program, and transfer control to it.exit(status);Exit with status, destroying the process. Note: this is not the only way for a process to exit!int pid = wait*(&status);Wait for exit (or other status change) of a child, and “reap” its exit status. Note: child may have exited before parent calls wait!execinitialize child contextHow are Unix shells implemented?How are Unix shells implemented?while (1) {Char *cm d = getcmd();intretval = fork();if (retval == 0) {// This is the child process// Setup


View Full Document

Duke CPS 196 - The Classical OS Model in Unix

Download The Classical OS Model in Unix
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The Classical OS Model in Unix and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The Classical OS Model in Unix 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?