CPS 310 Unix Process Model

CPS 310 Unix Process Model

D u k e S y s t e m s CPS 310 Unix Process Model Jeff Chase Duke University hp://www.cs.duke.edu/~chase/cps310 Operating Systems: The Classical View Programs Each process run as data data has a private independent virtual address processes. space and one or more threads. Protected ...and upcalls system calls (e.g., signals) 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. 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. 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 Kernel Stacks and Trap/Fault Handling System calls and Threads execute data faults run in kernel 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 syscall running in P’s in kernel space (VM dispatch process context accessible only in stack stack table has access to P’s 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. 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 The kernel must be bulletproof Secure kernels handle system calls verrry carefully. User program / user space Syscalls indirect through Kernel copies all syscall dispatch table user buffers arguments into by syscall number. No kernel space and direct calls to kernel trap copyout copyin validates them. 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 write() {…} data in/out of kernel arguments (e.g., file to kernel space (e.g., for read 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! 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 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. 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. Unix fork/exit syscalls int pid = fork(); Create a new process that is a clone of parent its parent. Return child process ID (pid) 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 time process to exit! data data exit exit p pid: 5587 pid: 5588 fork The fork syscall returns twice: int pid; It returns a zero in int status = 0; the context of the if (pid = fork()) { new child process. /* parent */ ….. It returns the new } else { child process ID /* child */ (pid) in the context ….. of the parent. exit(status); } A simple program: sixforks … int main(int argc, char* argv) How many processes are { created by these six forks? fork(); fork(); fork(); fork(); fork(); fork(); printf("Process %d exiting.\n", getpid()); } chase$ cc –o sixforks sixforks.c getpid syscall: chase$ ./sixforks Get processID of ??? current process. chase$ Process 15220 exiting. Process 15209 exiting. Process 15232 exiting. A simple program: sixforks Process 15219 exiting. Process 15233 exiting. chase$ cc –o sixforks sixforks.c Process 15223 exiting. chase$ ./sixforks Process 15210 exiting. … Process 15191 exiting. Process 15234 exiting. Process 15200 exiting. Process 15228 exiting. int Process 15195 exiting. Process 15192 exiting. Process 15194 exiting. Process 15230 exiting. main(int argc, char* argv) Process 15197 exiting. Process 15211 exiting. { Process 15202 exiting. Process 15227 exiting. Process 15193 exiting. Process 15239 exiting. Process 15198 exiting. fork(); Process 15231 exiting. Process 15215 exiting. Process 15242 exiting. fork(); Process 15217 exiting. Process 15218 exiting. Process 15243 exiting. fork(); Process 15203 exiting. Process 15240 exiting. Chase$ Process 15212 exiting. Process 15236 exiting. fork(); Process 15196 exiting. Process 15241 exiting. Process 15222 exiting. Process 15244 exiting. fork(); 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 15207 exiting. Process 15249 exiting. } Process 15201 exiting. Process 15204 exiting. Process 15214 exiting. Process 15238 exiting. Process 15225 exiting. Process 15251 exiting. Process 15199 exiting. Process 15237 exiting. Process 15226 exiting. Process 15252 exiting. Process 15208 exiting. Process 15253 exiting. Process 15229 exiting. Process 15246 exiting. Process 15254 exiting. 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? fork (original concept) fork in action today Fork is conceptually difficult but void syntactically clean and simple. dofork() { I don’t have to say anything about what int cpid = fork(); the new child process “looks like”: it is an if (cpid < 0) { exact clone of the parent! 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 child(); running program: it has a “copy” of the } else { entire address space. The “only” change is the process ID and return value cpid! parent(cpid); } The parent thread continues on its way. } The child thread continues on its way. 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()) { /* parent */ Wait variants allow wait ….. on a specific child, or pid = wait(&status); notification of stops and } else { other signals. /* child */ Recommended: use waitpid(). ….. exit(status); } Warning: the uses of the terms wait and signal should not be confused with the monitor/CV primitives of the same names. wait Process states (i.e., states of the main thread of the process) “wakeup” “sleep” Note: in modern Unix systems the wait syscall has many variants and options. A simple program: forkdeep int count = 0; main(int argc, char *argv[]) int level = 0; { count = atoi(argv[1]); void child() { dofork(); level++; output pid We’ll see later where arguments come from. output pids } if (level < count) dofork(); if (level == count) sleep(3); /* pause 3 secs */ } level==1 void parent(int childpid) { output pids level==2 wait for child to finish } chase$ ./forkdeep 4 chase$ ./forkdeep 3 30866-> 30867 11496-> 11498 30867 11498 30867-> 30868 11498-> 11499 30868 11499 30868-> 30869 11499-> 11500 30869 11500 30869-> 30870 11500 30870 11499 30870 11498 30869 11496 30868 chase$ 30867 30866 chase$ Example: Chrome browser [Google Chrome Comics] Processes in the browser Chrome makes an interesting choice here.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    52 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us