DOC PREVIEW
Duke CPS 210 - The Classical OS Model in Unix

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

The Classical OS Model in UnixNachos Exec/Exit/Join ExampleUnix Fork/Exec/Exit/Wait ExampleElements of the Unix Process and I/O ModelUnix File DescriptorsUnix File Descriptors IllustratedThe Concept of ForkExample: Process Creation in UnixWhat’s So Cool About ForkProducer/Consumer PipesUnix as an Extensible SystemThe ShellLimitations of the Unix Process ModelProcess InternalsMode Changes for Exec/ExitA Typical Unix File TreeFilesystemsQuestionsThe Classical OS Model in UnixThe Classical OS Model in UnixNachos Exec/Exit/Join ExampleNachos Exec/Exit/Join ExampleExec parent Exec childJoinExitSpaceID pid = Exec(“myprogram”, 0);Create a new process running the program “myprogram”. int status = Join(pid);Called by the parent to wait for a child to exit, and “reap” its exit status. Note: child may have exited before parent calls Join!Exit(status);Exit with status, destroying process. Note: this is not the only way for a proess to exit!.Unix Unix Fork/Exec/Exit/WaitFork/Exec/Exit/Wait Example Examplefork 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. int pid = wait*(&status);Wait for exit (or other status change) of a child. exec initialize child contextElements of the Unix Process and I/O ModelElements of the Unix Process and I/O Model1. 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 implement3. general support for combining small simple programs to perform complex tasksstandard I/O and pipelines: good programs don’t know/care where their input comes from or where their output goesUnix File DescriptorsUnix File DescriptorsUnix processes name I/O and IPC objects by integers known as file descriptors. •File descriptors 0, 1, and 2 are reserved by convention for standard input, standard output, and standard error.“Conforming” Unix programs read input from stdin, write output to stdout, and errors to stderr by default.•Other descriptors are assigned by syscalls to open/create files, create pipes, or bind to devices or network sockets.pipe, socket, open, creat•A common set of syscalls operate on open file descriptors independent of their underlying types.read, write, dup, closeUnix File Descriptors IllustratedUnix File Descriptors Illustrateduser spaceFile descriptors are a special case of kernel object handles.pipefilesocketprocess filedescriptortablekernelsystem open file tablettyDisclaimer: this drawing is oversimplified.The binding of file descriptors to objects is specific to each process, like the virtual translations in the virtual address space.The Concept of ForkThe Concept of ForkThe Unix system call for process creation is called fork().The fork system call creates a child process that is a clone of the parent.•Child has a (virtual) copy of the parent’s virtual memory.•Child is running the same program as the parent.•Child inherits open file descriptors from the parent.(Parent and child file descriptors point to a common entry in the system open file table.)•Child begins life with the same register values as parent.The child process may execute a different program in its context with a separate exec() system call.Example: Process Creation in UnixExample: Process 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.What’s So Cool About What’s So Cool About ForkFork1. fork is a simple primitive that allows process creation without troubling with what program to run, args, etc.Serves some of the same purposes as threads.2. fork gives the parent program an opportunity to initialize the child process…especially the open file descriptors.Unix syscalls for file descriptors operate on the current process.Parent program running in child process context may open/close I/O and IPC objects, and bind them to stdin, stdout, and stderr.Also may modify environment variables, arguments, etc.3. Using the common fork/exec sequence, the parent (e.g., a command interpreter or shell) can transparently cause children to read/write from files, terminal windows, network connections, pipes, etc.Producer/Consumer PipesProducer/Consumer Pipesoutputinputchar inbuffer[1024];char outbuffer[1024];while (inbytes != 0) { inbytes = read(stdin, inbuffer, 1024); outbytes = process data from inbuffer to outbuffer; write(stdout, outbuffer, outbytes);}Pipes support a simple form of parallelism with built-in flow control.e.g.: sort <grades | grep Dan | mail sprenkleUnix as an Extensible SystemUnix as an Extensible System“Complex software systems should be built incrementally from components.”•independently developed•replaceable, interchangeable, adaptableThe power of fork/exec/exit/wait makes Unix highly flexible/extensible...at the application level.•write small, general programs and string them togethergeneral stream model of communication•this is one reason Unix has survivedThese system calls are also powerful enough to implement powerful command interpreters (shell).The 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


View Full Document

Duke CPS 210 - 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?