1
play

1 Other application programs sh nroff who a.out cpp Kernel - PDF document

A 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 expensiveit can run on hardware costing as little as


  1. A 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 Classical OS Model in Unix The Classical OS Model in Unix The UNIX Time-Sharing System* D. M. Ritchie and K. Thompson DEC PDP-11/24 http://histoire.info.online.fr/pdp11.html Elements of the Unix Elements of the Unix A Typical Unix File Tree A Typical Unix File Tree 1. rich model for IPC and I/O: “ everything is a file ” Each volume is a set of directories and files; a host’s file tree is the set of file descriptors : most/all interactions with the outside world are directories and files visible to processes on a given host. through system calls to read/write from file descriptors , with a unified set of syscalls for operating on open descriptors of / File trees are built by grafting different types. volumes from different volumes or from network servers. 2. simple and powerful primitives for creating and bin etc tmp usr vmunix initializing child processes In Unix, the graft operation is fork : easy to use, expensive to implement the privileged mount system call, ls sh project users and each volume is a filesystem . Command shell is an “application” (user mode) packages 3. general support for combining small simple programs to mount point mount (coveredDir, volume) perform complex tasks (volume root) coveredDir: directory pathname volume : device specifier or network volume standard I/O and pipelines volume root contents become visible at pathname coveredDir tex emacs The Shell Using the shell The Shell Using the shell The Unix command interpreters run as ordinary user • Commands: ls , cat , and all that processes with no special privilege. • Current directory: cd and pwd This was novel at the time Unix was created: other systems • Arguments: echo viewed the command interpreter as a trusted part of the OS. • Signals: ctrl-c Users may select from a range of interpreter programs • Job control, foreground, and background: &, ctrl-z, bg , fg available, or even write their own (to add to the confusion). • Environment variables: printenv and setenv csh, sh, ksh, tcsh, bash : choose your flavor...or use perl . • Most commands are programs: which , $PATH, and /bin • Shells are commands: sh , csh , ksh , tcsh , bash Shells use fork/exec/exit/wait to execute commands composed of program filenames, args, and I/O redirection symbols. • Pipes and redirection: ls | grep a • Files and I/O : open, read, write, lseek, close Shells are general enough to run files of commands ( scripts ) for more complex tasks, e.g., by redirecting shell’s stdin . • stdin , stdout, stderr Shell’s behavior is guided by environment variables . • Users and groups: whoami, sudo, groups 1

  2. Other application programs sh nroff who a.out cpp Kernel date comp Hardware cc wc as ld grep vi ed Other application programs Questions about Processes Questions about Processes Process Internals Process Internals A process is an execution of a program within a private virtual address space thread process descriptor (PCB) virtual address space (VAS). user ID 1. What are the system calls to operate on processes? process ID + + parent PID sibling links 2. How does the kernel maintain the state of a process? stack children resources The address space is Processes are the “basic unit of resource grouping”. Each process has a thread represented by page bound to the VAS. Process state includes 3. How is the process virtual address space laid out? table , a set of a file descriptor table, translations to physical links to maintain the The thread has a saved user What is the relationship between the program and the process? memory allocated from a context as well as a system process tree, and a 4. How does the kernel create a new process? kernel memory manager . place to store the exit context. status. How to allocate physical memory for processes? The kernel must The kernel can manipulate initialize the process How to create/initialize the virtual address space? the user context to start the memory with the thread in user mode program image to run. wherever it wants. Process Creation Process Creation Process Creation Process Creation Two ways to create a process Option 2: Clone existing process and change • Build a new empty process from scratch • Example: Unix fork() and exec() • Copy an existing process and change it appropriately Fork(): Clones calling process Option 1: New process from scratch Exec(char *file): Overlays file image on calling process • Fork() • Steps Load specified code and data into memory; Stop current process and save its state Create empty call stack Make copy of code, data, stack, and PCB Create and initialize PCB (make look like context-switch) Add new PCB to ready list Put process on ready list Any changes needed to PCB? • Advantages: No wasted work • Exec(char *file) • Disadvantages: Difficult to setup process correctly and to express Replace current data and code segments with those in specified file all possible options • Advantages: Flexible, clean, simple Process permissions, where to write I/O, environment variables • Disadvantages: Wasteful to perform copy and then overwrite of Example: WindowsNT has call with 10 arguments memory [Remzi Arpaci-Dusseau] [Remzi Arpaci-Dusseau] 2

  3. Process Creation in Unix Process Creation in Unix Unix Fork/Exec/Exit/Wait Unix Fork/Exec/Exit/Wait Example Example int pid = fork(); Create a new process that is a clone of The fork syscall returns its parent. fork parent fork child twice: it returns a zero to the child and the child process exec*(“program” [, argvp, envp] ); ID (pid) to the parent. Overlay the calling process virtual int pid; initialize memory with a new program, and int status = 0; exec child context transfer control to it. Parent uses wait to sleep until the child exits; wait returns if (pid = fork()) { child pid and status. exit(status); /* parent */ Exit with status, destroying the process. ….. Wait variants allow wait on a Note: this is not the only way for a pid = wait(&status); specific child, or notification of stops and other signals. process to exit! } else { /* child */ wait exit ….. int pid = wait*(&status); exit(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! How are Unix shells implemented? How are Unix shells implemented? The Concept of Fork The Concept of Fork wh i l e (1 ) { Char * cmd = ge t cmd( ) ; fork creates a child process that is a clone of the parent. i n tr e t va l = f o r k ( ) ; • Child has a (virtual) copy of the parent’s virtual memory. i f ( r e t va l == 0 ) { / / Th i s i s t he ch i l d p rocess • Child is running the same program as the parent. / / Se tup t he ch i l d ’ s p rocess env i ronmen t he re • Child inherits open file descriptors from the parent. / / E .g . , whe re i s s tanda rd I /O , how to hand le s i gna l s? exec (cmd) ; (Parent and child file descriptors point to a common entry in the / / exec does no t r e tu rn i f i t succeeds system open file table.) p r i n t f ( “ERROR: Cou ld no t execu te %s \n ” , cmd) ; • Child begins life with the same register values as parent. ex i t ( 1 ) ; } e l se { The child process may execute a different program in its / / Th i s i s t he pa ren t p rocess ; W ai t f o r ch i l d t o f i n i sh context with a separate exec() system call. i n tp i d = re t va l ; wa i t ( p i d ) ; } } [Remzi Arpaci-Dusseau] What’ ’s So Cool About s So Cool About Fork Fork Unix File Descriptors What Unix File Descriptors 1. fork is a simple primitive that allows process creation Unix processes name I/O and IPC objects by integers without troubling with what program to run, args, etc. known as file descriptors . Serves the purpose of “lightweight” processes (like threads?). • File descriptors 0, 1, and 2 are reserved by convention for standard input, standard output , and standard error . 2. fork gives the parent program an opportunity to initialize the child process… e.g., the open file descriptors. “Conforming” Unix programs read input from stdin , write output to stdout , and errors to stderr by default. Unix syscalls for file descriptors operate on the current process. • Other descriptors are assigned by syscalls to open/create Parent program running in child process context may open/close files, create pipes, or bind to devices or network sockets. I/O and IPC objects, and bind them to stdin , stdout , and stderr . pipe , socket , open , creat Also may modify environment variables, arguments, etc. • A common set of syscalls operate on open file 3. Using the common fork/exec sequence, the parent (e.g., a command descriptors independent of their underlying types. interpreter or shell) can transparently cause children to read/write from files, terminal windows, network connections, pipes, etc. read, write, dup, close 3

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