1 / 53

Today

Today. Welcome Friday lecture is at 12, but will also be streamed ; final? Reading: Chapter 2 MOS P1: Processes and Threads Break: Meet your classmates again, form a team 2-3 P2: Thread implementation models, Scheduler Activations Questions from last time?. CSci 5103 Operating Systems.

gino
Download Presentation

Today

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today Welcome Friday lecture is at 12, but will also be streamed; final? Reading: Chapter 2 MOS P1: Processes and Threads Break: Meet your classmates again, form a team 2-3 P2: Thread implementation models, Scheduler Activations Questions from last time?

  2. CSci 5103Operating Systems Process/Thread Basics

  3. data data Threads vs. Processes • 1. The process is a kernel abstraction for an independent executing program. • includes at least one “thread of control” • also includes a private address space (VAS) • - this requires OS kernel support • often the unit of resource ownership in kernel • - e.g., memory, open files, CPU usage • 2. Threads may share an address space. • Threads have “context” just like vanilla processes. • - thread context switch vs. process context switch • Processes may be “multithreaded” with thread primitives supported by a library or the kernel.

  4. Multiprogramming vs. Multithreading • Difference?

  5. Multiprogramming vs. Multithreading • Multiprogramming means OS has several processes in memory at a time and can execute any of them (provided they are ready-to-run) • processes are address-space disjoint! (separate VAS) • Multithreading means process can have multiple threads • threads share address-space • Processes provide better isolation – why?

  6. Issues with Processes • A process is an execution of a program within a private virtual address space (VAS). • 1. What are the system calls to operate on processes? 4061 • 2. How does the kernel maintain the state of a process? • Processes are the “basic unit of resource grouping”. • 3. How is the process virtual address space laid out? • What is the relationship between the program and the process? • We’ll get to this shortly in MM • 4. How does the kernel create a new process? • How to allocate physical memory for processes? • How to create/initialize the virtual address space?

  7. stack thread virtual address space user ID process ID parent PID sibling links children Process Internals process descriptor + + resources kernel For now, this is a set of legal memory for the process (virtual->physical mapping later …) Each process has >= 1 thread bound to the VAS. Process context switch is more expensive. Why? register sets, kernel state (accounting), flush caches …

  8. Process State Information associated with each process. • Program counter, stack ptr • CPU registers • CPU scheduling information • Memory-management information • Accounting information • I/O status information • Open files, signals (if UNIX)

  9. 0 0x0 text data data BSS/heap user stack args/env 2n-1 kernel text and kernel data 2n-1 0xffffffff The Virtual Address Space • A typical process VAS space includes: • user regions in the lower half • kernel regions in upper half • shared by all processes • directly accessible only to kernel code A VAS for a private address space system (e.g., Unix) executing on a typical 32-bit architecture.

  10. data data data data data The Birth of a Program myprogram.c myprogram.o int j; const char* s = “hello\n”; int p() { j = write(1, s, 6); return(j); } object file assembler libraries and other objects linker ….. p: store this store that push jsr _write ret etc. compiler program myprogram.s myprogram (executable file)

  11. What’s in an Object File or Executable? Header “magic number” indicates type of image. header text program instructions p Section table an array of (offset, len, startVA) immutable data (constants) “hello\n” data idata wdata program sections writable global/static data j, s MIPS/COFF binary int j = 327; char* s = “hello\n”; char sbuf[512]; int p() { int k = 0; j = write(1, s, 6); return(j); } Q: where is the stack and heap?

  12. text data BSS user stack args/env kernel The Program and the Process VAS: Unix example BSS “Block Started by Symbol” (uninitialized global data) e.g., heap and sbuf go here. Process text segment is initialized directly from program text section. header text data idata data segments sections wdata Process BSS segment may be expanded at runtime with a system call (e.g., Unix sbrk) called by the heap manager routines. program Process data segment(s) are initialized from idata and wdata sections. process VAS Process stack and BSS (e.g., heap) segment(s) are zero-filled. Text and idata segments may be write-protected. Args/env strings copied in by kernel when the process is created.

  13. text data BSS user stack args/env kernel Virtual Addressing virtual memory (big) physical memory (small) User processes address memory through virtual addresses. Who controls the virtual-physical translations in effect for each space? 0 data The kernel and the machine collude to translate virtual addresses to physical addresses. Who complains if a user process accesses illegal memory? Who defines what is legal for a process? virtual-to-physical translations The specific mechanisms for memory management and address translation are machine-dependent.

  14. Processes in Unix • The 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 (VAS). • - if copy-on-write, memory is shared, until one of them updates • Child is running the same program as the parent. • Child inherits open file descriptors from the parent. • 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.

  15. Unix Fork/Exec/Exit/Wait Example • int 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. fork parent fork child initialize child context exec wait exit Corner cases: orphans and zombies

  16. Example: Process Creation in Unix The fork syscall returns twice: it returns a zero to the child and the child process ID (pid) to the parent. int 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 child, or notification of stops and other signals.

  17. Process States

  18. CSci 5103Operating Systems Threads

  19. Threads: What are they? • A thread is a basic unit of CPU utilization or execution • defined by CPU register values (PC, SP) • each thread must have its own stack • suspend: save register values in memory • resume: restore registers from memory • Multiple threads can execute independently: • They can run in parallel on multiple CPUs • - physical concurrency • … or arbitrarily interleaved on a single CPU • - logical concurrency

  20. Threads and Data • Threads inside a process share state • When is data shared? • local variables are private: remember each thread gets its own stack • global variables are shared • dynamic variables or other heap data are shared • Problem? • Races can occur when threads operate on shared data • A race condition arises when the outcome of a set of operations depends on who “won the race” (got there first) • Solution ? • Synchronization is the solution

  21. The Benefit of Threads • 1. Concurrency • when one thread is blocked, another may run (e.g. on I/O) • great for multi-threaded servers (Web servers, file servers) • 2. Parallelism • multiple threads can run in parallel on a multiprocessor • 3. Sharing • threads share resources of the process (e.g. buffer caches) • 4. Overhead • cheaper than processes (creating/destroying them, context switches) • 5. Modularity • unit of program activity

  22. Two Threads Sharing a CPU concept reality context switch

  23. your data code library your program A Peek Inside a Running Program CPU x R0 Rn x PC heap SP y registers y stack “memory”

  24. code library your data y stack stack A Program With Two Threads ready to run x running Your program R0 Rn x PC SP y registers “memory”

  25. your data code library your program common runtime Thread Context Switch switch out switch in x R0 Rn 1. save registers x PC SP y registers y stack stack 2. load registers “memory”

  26. I want to see the code … /* Symbolic register names */ #define z $0 /* zero register */ #define a0 $4 /* argument registers */ #define a1 $5 #define s0 $16 /* callee saved */ #define s1 $17 #define s2 $18 #define s3 $19 #define s4 $20 #define s5 $21 #define s6 $22 #define s7 $23 #define sp $29 /* stack pointer */ #define fp $30 /* frame pointer */ #define ra $31 /* return address */ You asked for it (MIPS thread):

  27. SWITCH: #a0,a1 is stack ptrmem address of t1, t2 respectively sw sp, 0(a0) # save new stack pointer sw s0, 4(a0) # save all the callee-save registers sw s1, 8(a0) sw s2, 12(a0) sw s3, 16(a0) … sw s7, 32(a0) swfp, 36(a0) # save frame pointer swra, 68(a0) # save return address lw sp, 0(a1) # load the new stack pointer lw s0, 4(a1) # load the callee-save registers lw s1, 8(a1) lw s2, 12(a1) … lwfp, 36(a1) lwra, 68(a1) # load the return address j ra .end SWITCH

  28. Context Switches: Voluntary and Involuntary • On a uniprocessor, the set of possible execution schedules depend on when context switches can occur. • Voluntary: one thread explicitly yields the CPU to another. E.g. a thread can suspend itself with Yield It may also block to wait for some event with Sleep • Involuntary: the system scheduler suspends an active thread and switches control to a different thread. Thread scheduler tries to share CPU fairly by timeslicing. Why would a thread yield?

  29. General Thread Operations • Create • allocate memory for stack, perform bookkeeping • associate entry-point (called function) • parent thread creates child threads • Destroy/Cancel • release memory (or recycle), perform bookkeeping • Suspend/Resume/Yield/Sleep

  30. data readyList Thread Scheduling • When a thread blocks or yields or is de-scheduled by the system, which one is picked to run next? • Preemptive scheduling: preempt a running thread • Non-preemptive: thread runs until it yields or blocks • Priorities? All threads may not be equal when gets de-scheduled …

  31. Thread Scheduling (cont’d) • The scheduler makes a sequence of “moves” that determines the interleaving of threads. • Programs use synchronization to prevent “bad moves”. • …but otherwise scheduling choices appear (to the program) to be nondeterministic. • The scheduler’s moves are dictated by a scheduling policy • Applies to process scheduling as well

  32. Thread Scheduling (cont’d) • Priority scheduling • threads have a priority • scheduler selects thread with highest priority to run • preemptive or non-preemptive • Priority inversion • 3 threads, t1, t2, and t3 (priority order – low to high) • t1 is holding a resource (lock) that t3 needs • t3 is obviously blocked • t2 keeps on running! • How did t1 get lock before t3?

  33. How would you solve it? One solution involves temporarily raising priority level of T1>T2 until it releases the lock. Requires that the system be checking for this condition!

  34. BREAK

  35. CSci 5103Operating Systems User/Kernel Threads

  36. Other Thread Issues Thread-safe vs. re-entrant Difference? Thread-safe: multiple threads may execute same code concurrently, if shared state, must be protected Why is this a big issue in systems programming? Re-entrant: can re-enter a function or code block – no shared variables – all state external When can this happen? Interrupts, signals, … recursive calls re-entrant for 1 thread, multi-threads, etc.

  37. Pop-Up Threads • Create cheaper than restore; still have to save state of prior thread • Idea: if you are already in the kernel a new message comes in, create new thread

  38. Implementing Threads in User Space A user-level threads package OS has no knowledge of threads; all mgmt done by RT library

  39. Implementing Threads in the Kernel A threads package managed by the kernel All thread mgmt done in the kernel, usually preemptive

  40. Pros of kernel threads: Pros of user threads:

  41. Hybrid Implementations – best of both … Multiplexing user-level threads onto kernel- level threads Solaris is one example – kernel thread blocks, pick another from same process

  42. Cost Comparison • Comparing user-level threads, kernel threads, and processes. What does this suggest to you in terms of hybrid approaches?

  43. Scheduler Activations Want best of both worlds User-space: custom-scheduling, fast Kernel-space: integrated (multiprocessing), blockable

  44. SA Idea: Create a structure that allows information to flow between: user-space (thread library) and kernel One-way is common … system call Other way is uncommon (sort of) …. Upcall

  45. SA Cont’d Two new things: Activation: structure that allows information/events to flow (holds key information, e.g. stacks) Virtual processor: abstraction of a physical machine; gets “allocated” to an application -- means any threads attached to it will run on that processor -- want to run on multiple processors – ask OS for > 1 VP

  46. Example • Kernel provides two processors to the application, user library picks two threads to run …. • Now, suppose T1 blocks ….

  47. T1 blocks in the kernel • - kernel creates a SA; makes upcall on the processor running T1 • User-level scheduler picks another thread (T3) to run on that processor • T1 put on blocked list

  48. I/O for (T1) completes • Notification requires a processor; kernel preempts one of them (B – T2), does upcall • Problem : suppose no processors! – must wait until kernel gives one • Two threads back on the ready list! (which two?)

More Related