1
play

1 The Concept of Fork The Concept of Fork Example: Process - PDF document

Nachos Exec/Exit/Join Example Nachos Exec/Exit/Join Example SpaceID pid = Exec(myprogram, 0); Create a new process running the Exec parent Exec child program myprogram. The Classical OS Model in Unix The Classical OS Model in Unix


  1. Nachos Exec/Exit/Join Example Nachos Exec/Exit/Join Example SpaceID pid = Exec(“myprogram”, 0); Create a new process running the Exec parent Exec child program “myprogram”. The Classical OS Model in Unix The Classical OS Model in Unix 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); Join Exit Exit with status, destroying process. Note: this is not the only way for a proess to exit!. Unix Fork/Exec/Exit/Wait Fork/Exec/Exit/Wait Example Unix Example Elements of the Unix Process and I/O Model Elements of the Unix Process and I/O Model 1. rich model for IPC and I/O: “ everything is a file ” int pid = fork(); Create a new process that is a clone of file descriptors : most/all interactions with the outside world are fork parent fork child its parent. through system calls to read/write 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 virtual memory 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 int pid = wait*(&status); wait exit perform complex tasks Wait for exit (or other status change) of standard I/O and pipelines : good programs don’t know/care a child. where their input comes from or where their output goes Unix File Descriptors Unix File Descriptors Unix File Descriptors Illustrated Unix File Descriptors Illustrated Unix processes name I/O and IPC objects by integers user space 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 table • Other descriptors are assigned by syscallsto open/create files, create pipes, or bind to devices or network sockets. system open file tty 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, dup, close Disclaimer: specific to each process, like the virtual this drawing is translations in the virtual address space. oversimplified. 1

  2. The Concept of Fork The Concept of Fork Example: Process Creation in Unix Example: Process Creation in Unix The Unix system call for process creation is called fork (). The fork syscall returns twice: it returns a zero to the The fork system call creates a child process that is a clone of 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; Parent uses wait to sleep until • Child is running the same program as the parent. the child exits; wait returns if (pid= fork()) { child pid and status. • Child inherits open file descriptors from the parent. /* parent */ ….. Wait variants allow wait on a (Parent and child file descriptors point to a common entry in the pid = wait(&status); specific child, or notification of system open file table.) stops and other signals. } else { /* child */ • Child begins life with the same register values as parent. ….. The child process may execute a different program in its exit(status); } context with a separate exec() system call. Fork Fork What’s So Cool About What’s So Cool About Producer/Consumer Pipes Producer/Consumer Pipes 1. fork is a simple primitive that allows process creation char inbuffer[1024]; char outbuffer[1024]; Pipes support a simple form of without troubling with what program to run,args, etc. 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); outbytes= process data frominbuffer to outbuffer; the child process… especially the open file descriptors. 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 . input output 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. e.g.: sort <grades |grep Dan | mail sprenkle Unix as an Extensible System Unix as an Extensible System The Shell The Shell “Complex software systems should be built incrementally The Unix command interpreters run as ordinary user processes with no special privilege. from components.” This was novel at the time Unix was created: other systems • independently developed 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, bash : choose your flavor...or use perl . • write small, general programs and string them together Shells use fork/exec/exit/wait to execute commands composed of program filenames,args, and I/O redirection symbols. general stream model of communication Shells are general enough to run files of commands ( scripts ) for • this is one reason Unix has survived more complex tasks, e.g., by redirecting shell’s stdin . These system calls are also powerful enough to implement environment variables . Shell’s behavior is guided by powerful command interpreters ( shell ). 2

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