<<

Nachos // Example

SpaceID pid = Exec(“myprogram”, 0); Exec parent Exec child Create a new running the program “myprogram”. The Classical OS Model in int status = Join(pid ); Called by the parent to for a child to exit, and “reap” its . Note: child may have exited before parent calls Join!

Join Exit Exit(status); Exit with status, destroying process. Note: this is not the only way for a proess to exit!.

Unix /Exec/Exit/Wait Example Elements of the Unix Process and I/O Model

int pid = fork(); 1. rich model for IPC and I/O: “ everything is a ” fork parent fork child Create a new process that is a clone of file descriptors: /all interactions with the outside world are its parent. through system calls to read/ from file descriptors, with a unified set of syscalls for operating on open descriptors of initialize exec*(“program” [, argvp , envp]); different types. child context exec Overlay the calling process with a new program, and 2. simple and powerful primitives for creating and transfer control to it. initializing child processes exit(status); fork: easy to use, expensive to implement Exit with status, destroying the process. 3. general support for combining small simple programs to wait exit int pid = wait*(&status); perform complex tasks Wait for exit (or other status change) of a child. standard I/O and pipelines: good programs don’t know/care where their input comes from or where their output goes

Unix File Descriptors Unix File Descriptors Illustrated

Unix processes name I/O and IPC objects by integers kernel known as file descriptors. file • File descriptors 0, 1, and 2 are reserved by convention for standard input,standard output, and standard error. pipe “Conforming” Unix programs read input from stdin, write process file output to stdout, and errors to stderr by default. descriptor socket • Other descriptors are assigned by syscallsto open/create table files, create pipes, or bind to devices or network sockets. system open file pipe, socket, open,creat File descriptors are a special table case of kernel object handles. • A common set ofsyscalls operate on open file descriptors independent of their underlying types. The binding of file descriptors to objects is read, write, , close specific to each process, like the virtual Disclaimer: this drawing is translations in the virtual address space. oversimplified.

1 The Concept of Fork Example: Process Creation in Unix

The Unix for process creation is called fork(). The fork syscall returns The fork system call creates a that is a clone of twice: it returns a zero to the child and the child process the parent. ID (pid) to the parent. int pid; • Child has a (virtual) copy of the parent’s virtual memory. int status = 0; • Child is running the same program as the parent. Parent uses wait to until if (pid= fork()) { the child exits; wait returns • Child inherits open file descriptors from the parent. /* parent */ child pid and status. ….. (Parent and child file descriptors point to a common entry in the Wait variants allow wait on a pid = wait(&status); specific child, or notification of system open file table.) } else { stops and other signals. • Child begins life with the same register values as parent. /* child */ ….. The child process may execute a different program in its exit(status); context with a separate exec() system call. }

What’s So Cool About Fork Producer/Consumer Pipes

1. fork is a simple primitive that allows process creation char inbuffer[1024]; without troubling with what program to run,args, etc. char outbuffer[1024]; Pipes support a simple form of parallelism with built-in flow control . Serves some of the same purposes as threads. while (inbytes != 0) { 2. fork gives the parent program an opportunity to initialize inbytes = read(stdin, inbuffer, 1024); the child process…especially the open file descriptors. outbytes = process frominbuffer to outbuffer; write(stdout, outbuffer, outbytes); 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. input output 3. Using the common fork/exec sequence, the parent (e.g., a command or ) can transparently cause children to read/write from files, terminal windows, network connections, pipes, etc. e.g.:

Unix as an Extensible System The Shell

“Complex software systems should be built incrementally The Unix command interpreters run as ordinary user from components.” processes with no special privilege. • independently developed This was novel the Unix was created: other systems viewed the command interpreter as a trusted part of the OS. • replaceable, interchangeable, adaptable Users may select from a range of interpreter programs The power of fork/exec/exit/wait makes Unix highly available, or even write their own (to add to the confusion). flexible/extensible...at the application level. csh, sh,ksh, tcsh, : choose your flavor...or use . • write small, general programs and string them together Shells use fork/exec/exit/wait to execute commands composed general stream model of communication of program filenames,args, and I/O symbols. • this is one reason Unix has survived Shells are general enough to run files of commands (scripts) for complex tasks, e.g., by redirecting shell’s stdin. These system calls are also powerful enough to implement Shell’s behavior is guided by environment variables. powerful command interpreters (shell).

2 Limitations of the Unix Process Model Process Internals

The pure Unix model has several shortcomings/limitations: virtual address space process descriptor • Any setup for a new process must be done in its context. user ID • Separated Fork/Execis slow and/or complex to implement. process ID parent PID + + sibling links A more flexible process abstraction would the ability stack children resources of a process to manage another externally. The address space is Each process has a thread This is a hallmark of systems that support multiple operating represented by page bound to the VAS. includes system “personalities” (e.g., NT) and “microkernel” systems table, a set of a table, (e.g., Mach). translations to physical The thread has a saved user links to maintain the memory allocated from a Pipes are limited to transferring linear byte streams between a context as well as a system process , and a kernel memory manager . context. place to store the exit pair of processes with a common ancestor. status. Richer IPC models are needed for complex software systems The kernel must The kernel can manipulate built as collections of separate programs. initialize the process the user context to start the memory with the thread in user mode program image to run. wherever it wants.

Mode Changes for Exec/Exit A Typical Unix File Tree

Syscall traps and “returns” are not always paired. Each volume is a set of directories and files; a ’s file tree is the set of directories and files visible to processes on a given host. Exec “returns” (to child) from a trap that “never happened”

Exit system call trap never returns File trees are built by grafting / volumes from different volumes system may switch processes between trap and return or from network servers. bin etc tmp usr vmunix In contrast, interrupts and returns are strictly paired. In Unix, the graft operation is the privileged system call, sh project users Exec Exec Join Join Exec enters the child by and each volume is a filesystem. parent call return call return doctoring up a saved user context to “return” through. packages mount point child mount ( coveredDir, volume) coveredDir: directory pathname (volume root) transition from user to kernel mode ( callsys) Exec Exit volume: devicespecifier or network volume entry to call transition from kernel to user mode ( retsys) volume root contents become visible at pathname coveredDir user space tex emacs

Filesystems Questions

Each file volume (filesystem) has a , determined by its A process is an execution of a program within a private disk layout or the network protocol used to access it. virtual address space (VAS). ufs (ffs), lfs, nfs, rfs, cdfs, etc. 1. What are the system calls to operate on processes? Filesystemsare administered independently. 2. How does the kernel maintain the state of a process? Modern systems also include “logical” pseudo-filesystems in Processes are the “basic unit of resource grouping”. the naming tree, accessible through the file syscalls. 3. How is the process virtual address space laid out? procfs: the /proc filesystem allows access to process internals. What is the relationship between the program and the process? mfs: the memory file system is a memory -based scratch store. 4. How does the kernel create a new process? Processes access filesystems through common system calls. How to allocate physical memory for processes? How to create/initialize the virtual address space?

3