cps 310 unix process model
play

CPS 310 Unix Process Model Jeff Chase Duke University - PowerPoint PPT Presentation

D D u k e S y s t t e m s CPS 310 Unix Process Model Jeff Chase Duke University h>p://www.cs.duke.edu/~chase/cps310 Operating Systems: The Classical View Each process Programs has a


  1. D D u k e S y s t t e m s CPS ¡310 ¡ Unix ¡Process ¡Model ¡ Jeff ¡Chase ¡ Duke ¡University ¡ ¡ h>p://www.cs.duke.edu/~chase/cps310 ¡

  2. Operating Systems: The Classical View Each process Programs has a private run as data data virtual address independent space and one processes. or more threads. Protected ...and upcalls (e.g., signals) system calls Protected OS Threads kernel enter the mediates kernel for access to OS shared services. resources. The kernel code and data are protected from untrusted processes.

  3. Today • We understand threads and concurrency now. • Add a kernel and address spaces à “vanilla Unix” • Flesh out Unix process model and syscall API • Kernel interactions • Using the process model • Imagine : – Your thread library is “really” a kernel. – Each thread is a classical process. – i.e., Each thread has its own VAS. – i.e., Threads/processes share data only within/via the kernel.

  4. Thread states and transitions We will presume that these transitions occur only in kernel mode. This is true in classical Unix and in systems with pure kernel-based threads. Before a thread can sleep , it Before a thread can yield , it must enter must first enter the kernel via the kernel, or the core must take an trap (syscall) or fault. interrupt to return control to the kernel. running On entry to the running yield state, kernel code decides preempt if/when/how to enter user sleep mode, and sets up a dispatch suitable context wakeup E.g., for initial start, return blocked ready from fault or syscall, or to deliver a signal. STOP wait

  5. Kernel Stacks and Trap/Fault Handling System calls and Threads execute faults run in kernel data user code on a user mode on a kernel stack in user space stack for the (the process virtual stack stack current thread. address space). Each thread has a Kernel code second kernel stack running in P’s syscall in kernel space (VM process context dispatch stack accessible only in stack has access to P’s table kernel mode). virtual memory. The syscall (trap) handler makes an indirect call through the system call dispatch table to the handler registered for the specific system call.

  6. Upcall example: Unix signals • Signals are asynchronous notifications to a process that some event of interest to it has occurred. • A process may register signal handlers for various events relating to the process. The signal handlers are procedures in user space. • To deliver a signal, the kernel redirects a user thread to execute a selected registered signal handler in user mode. • Unix signals take a default action if no handler is registered. – E.g., segmentation fault à die. Other actions: ignore, stop data data ...and upcalls Protected (e.g., signals ) system calls

  7. The kernel must be bulletproof Secure kernels handle system calls verrry carefully. User program / user space Syscalls indirect through Kernel copies all user buffers syscall dispatch table arguments into by syscall number. No kernel space and direct calls to kernel trap validates them. copyout copyin routines from user space! Kernel interprets pointer arguments in What about references context of the user read () { … } to kernel data objects VAS, and copies the passed as syscall data in/out of kernel write () { … } arguments (e.g., file to space (e.g., for read kernel read or write)? and write syscalls). Use an integer index into a kernel table that points at the data object. The value is called a handle or descriptor . No direct pointers to kernel data from user space!

  8. Unix: 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 image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again. DEC PDP-11/24 The UNIX Time-Sharing System* D. M. Ritchie and K. Thompson 1974 http://histoire.info.online.fr/pdp11.html

  9. Process management • OS offers system call APIs for managing processes. – Create processes (children) – Control processes – Monitor process execution – “Join”: wait for a process to exit and return a result – “Kill”: send a signal to a process – Establish interprocess communication (IPC: later) – Launch a program within a process • We study the Unix process abstraction as an example. – Illustrative and widely used for 40+ years! – Optional: Use it to build your own shell.

  10. Example: Unix fork The Unix fork() system call creates/launches a new thread, in its own fresh virtual address space: it creates a new process. (Thread + VAS == Process.) Strangely, the new (“child”) process is an exact clone of the calling (“parent”) process. fork Oh Ghost of Walt, please don’t sue me.

  11. Unix fork/exit syscalls int pid = fork(); Create a new process that is a clone of its parent. Return child process ID ( pid ) parent to parent, return 0 to child. fork parent child exit(status); Exit with status, destroying the process. Status is returned to the parent. Note: this is not the only way for a process to exit! time data data exit exit p pid: 5587 pid: 5588

  12. fork The fork syscall returns twice: int pid; It returns a zero in int status = 0; the context of the new child process. if (pid = fork ()) { /* parent */ It returns the new … .. child process ID } else { /* child */ (pid) in the context of the parent. … .. exit(status); }

  13. A simple program: sixforks … int How many processes are main(int argc, char* argv) created by these six forks? { fork(); fork(); fork(); fork(); fork(); fork(); printf("Process %d exiting.\n", getpid()); chase$ cc –o sixforks sixforks.c } chase$ ./sixforks getpid syscall: ??? Get processID of chase$ current process.

  14. Process 15220 exiting. Process 15209 exiting. A simple program: sixforks Process 15232 exiting. Process 15219 exiting. Process 15233 exiting. Process 15223 exiting. chase$ cc –o sixforks sixforks.c Process 15210 exiting. chase$ ./sixforks … Process 15234 exiting. Process 15191 exiting. Process 15228 exiting. Process 15200 exiting. int Process 15195 exiting. Process 15192 exiting. Process 15194 exiting. main(int argc, char* argv) Process 15230 exiting. Process 15197 exiting. Process 15211 exiting. { Process 15202 exiting. Process 15227 exiting. Process 15193 exiting. Process 15239 exiting. fork(); Process 15198 exiting. Process 15231 exiting. Process 15215 exiting. Process 15242 exiting. fork(); Process 15217 exiting. Process 15243 exiting. Process 15218 exiting. fork(); Process 15240 exiting. Process 15203 exiting. Process 15236 exiting. Chase$ Process 15212 exiting. fork(); Process 15241 exiting. Process 15196 exiting. Process 15244 exiting. fork(); Process 15222 exiting. Process 15213 exiting. Process 15247 exiting. fork(); Process 15221 exiting. Process 15235 exiting. Process 15224 exiting. Process 15245 exiting. printf("Process %d exiting.\n", Process 15206 exiting. Process 15250 exiting. Process 15216 exiting. Process 15248 exiting. getpid()); Process 15205 exiting. Process 15249 exiting. Process 15207 exiting. } Process 15204 exiting. Process 15201 exiting. Process 15238 exiting. Process 15214 exiting. Process 15251 exiting. Process 15225 exiting. Process 15237 exiting. Process 15199 exiting. Process 15226 exiting. Process 15252 exiting. Process 15208 exiting. Process 15253 exiting. Process 15229 exiting. Process 15246 exiting. Process 15254 exiting.

  15. sixforks: some questions • What if I want to create six children, but I don’t want my children to have children of their own? • What if I want the program to print the total number of processes created? How? (Other than by having the program do the math.) • How much memory does this program use? How many pages? • How does this test system assign process IDs? • Why do the process IDs print out of order?

  16. fork (original concept)

  17. fork in action today Fork is conceptually difficult but void syntactically clean and simple. dofork() { I don’t have to say anything about what the new child process “looks like”: it is an int cpid = fork() ; exact clone of the parent! if (cpid < 0) { perror("fork failed: "); The child has a new thread executing at exit(1); the same point in the same program. } else if (cpid == 0) { The child is a new instance of the running program: it has a “copy” of the child(); entire address space. The “only” change } else { is the process ID and return value cpid ! parent(cpid); } The parent thread continues on its way. } The child thread continues on its way.

  18. wait syscall Parent uses wait to sleep until the child int pid; exits; wait returns child int status = 0; pid and status. if (pid = fork()) { Wait variants allow wait /* parent */ on a specific child, or … .. notification of stops and pid = wait(&status); other signals. } else { Recommended: use waitpid() . /* child */ … .. exit(status); } Warning : the uses of the terms wait and signal should not be confused with the monitor/CV primitives of the same names.

Recommend


More recommend