Download presentation
Presentation is loading. Please wait.
Published byMarshall Washington Modified over 9 years ago
1
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Implementing Processes, Threads, and Resources 6
2
Slide 6-2 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Announcements Extension til Friday 11 am for HW #1 Previous lectures online Program Assignment #1 online later today, due 2 weeks from today Homework Set #2 online later today, due a week from today Read chapter 6
3
Slide 6-3 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 What is a Process? A software program consist of a sequence of code instructions and data –for now, let a simple app = a program –CPU executes the instructions line by line in fetch-execute cycle from RAM –code instructions operate on data –A program is a passive entity A process is a program actively executing from main memory Code Data Program P1
4
Slide 6-4 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Code Data Main Memory OS Loader Program P1 binary Loading and Executing a Program Code Data Code Data P1 binary P2 binary Disk Process CPU Execution Program Counter (PC) Registers ALU Fetch Code and Data Write Data
5
Slide 6-5 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 What is a Process? A process is a program actively executing from main memory –has a Program Counter (PC) and execution state associated with it CPU registers keep state OS keeps process state in memory it’s alive! –has an address space associated with it a limited set of (virtual) addresses that can be accessed by the executing code Code Data Main Memory Program P1 binary CPU Execution Program Counter (PC) Registers ALU Fetch Code and Data Write Data Process
6
Slide 6-6 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 What is a Process? –2 processes may execute the same program code, but they are considered separate execution sequences Code Data Main Memory Program P1 binary CPU Execution Program Counter (PC) Registers ALU Fetch Code and Data Write Data Process
7
Slide 6-7 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 How is a Process Structured in Memory? float f4=3.0; main() { char* ptr; ptr = malloc(); foo1(); } foo1() { int a1;.... } Code Data Main Memory Process P1 global variables functions local variables dynamically allocated variables
8
Slide 6-8 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 How is a Process Structured in Memory? float f4=3.0; main() { char* ptr; ptr = malloc(); foo1(); } foo1() { int a1;.... } Code Data Main Memory Process P1 global variables functions local variables dynamically allocated variables Heap Stack
9
Slide 6-9 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 How is a Process Structured in Memory? Run-time memory image Essentially code, data, stack, and heap Code and data loaded from executable file Stack grows downward, heap grows upward User stack Heap Read/write.data,.bss Read-only.init,.text,.rodata Unallocated Run-time memory address 0 max address
10
Slide 6-10 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Why Allocate Local Variables on a Stack? Strawman approach: pre-allocate all local variables a priori before a process starts executing, just like global variables What’s wrong with this strawman? –if a function is never called, then you’ve wasted space allocating its local variables –don’t know a priori how many instances of a local variable to allocate if a function calls itself, i.e. recursion
11
Slide 6-11 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Why Allocate Local Variables on a Stack? So allocate local variables only on an as- needed basis A stack provides a simple way to allocate local variables as needed –When a function is called, allocate its local variables on top of the stack - push them on the stack –when a function completes, deallocate these local variables - pop them off the stack
12
Slide 6-12 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Why Allocate Dynamic Variables on a Heap in the Process’s Address Space? Strawman II: could ask the OS to allocate dynamic variables anywhere in memory –very complex keeping tracking of all the different locations in memory Keeping the dynamic variables in one area (the process’s heap) associated with the process’s address space simplifies memory management
13
Slide 6-13 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 A Process Executes in its Own Address Space OS tries to provide the illusion or abstraction that the process executes in its own address space, on its own CPU Code Data Main Memory Process P1 Heap Stack
14
Slide 6-14 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 OS Address Space OS Address Space Implementing the Process Abstraction Control Unit OS interface … Machine Executable Memory ALU CPU P i Address Space P i Address Space P i CPU P i Executable Memory P i Executable Memory P k Address Space P k Address Space … P k CPU P k Executable Memory P k Executable Memory P j Address Space P j Address Space P j CPU P j Executable Memory P j Executable Memory
15
Slide 6-15 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 OS Process Management: External View Hardware Application Process Application Process Device MgrProcess MgrMemory Mgr File Mgr UNIX Device MgrProcess MgrMemory Mgr File Mgr Windows CreateThread() CreateProcess() CloseHandle() WaitForSingleObject() fork() exec() wait()
16
Slide 6-16 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Manager Responsibilities Define & implement the essential characteristics of a process and thread –Algorithms to define the behavior –Data structures to preserve the state of the execution Define what “things” threads in the process can reference – the address space (most of the “things” are memory locations) Manage the resources used by the processes/threads Tools to create/destroy/manipulate processes & threads Tools to time-multiplex the CPU – Scheduling the (Chapter 7) Tools to allow threads to synchronization the operation with one another (Chapters 8-9) Mechanisms to handle deadlock (Chapter 10) Mechanisms to handle protection (Chapter 14)
17
Slide 6-17 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Manager Overview Program Process Abstract Computing Environment File Manager Memory Manager Device Manager Protection Deadlock Synchronization Process Description Process Description CPU Other H/W Scheduler Resource Manager Resource Manager Resource Manager Resource Manager Resource Manager Resource Manager Memory Devices
18
Slide 6-18 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 OS Process Management OS keeps a Process Control Block (PCB) for each process: –Process state: new, running, waiting, ready, terminated –Program counter –CPU registers –CPU-scheduling information, e.g. priority –memory management info: value of base and limit registers, page tables, segment tables –I/O info: open files, etc. Code Data Main Memory Process P1 Heap Stack
19
Slide 6-19 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Multiple Processes Main Memory Code Data Process P1 Heap Stack Code Data Process P2 Heap Stack Each process is in memory Only one process at a time executes on the CPU OS provides the mechanisms to switch between processes –this is called a context switch
20
Slide 6-20 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Context Switching Process Manager Interrupt Handler P1P1 P2P2 PnPn Executable Memory Initialization 1 2 3 4 5 7 Interrupt 8 9 6 Each time a process is switched out, its context must be saved, e.g. on the stack Each time a process is switched in, its context is restored This usually requires copying of registers
21
Slide 6-21 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Context Switches A context switch can occur because of –a system call –an I/O interrupt, e.g. disk has finished reading –a timer interrupt this is how you implement multitasking Set a timer in the CPU for periodic interrupt, say every 1 ms On an interrupt, go to the timer interrupt handler, e.g. the scheduler, and switch to another process in the ready queue Context switch time is pure overhead –it is the price you pay for multiprogramming –typically a few milliseconds
22
Slide 6-22 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Multiple Processes: State Diagram Ready Blocked Running Start Schedule Request Done Request Allocate
23
Slide 6-23 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Communicating Between Processes Inter-Process Communication or IPC –would like two processes to share information between them shared file split a single application into multiple processes to speed up execution by allowing overlapped I/O split an application into multiple processes for modularity of coding –if address spaces are completely isolated from one another, then how do we share data? Two types –shared memory - OS provides constructs that allow –message passing - OS provides constructs that allow communication via buffers
24
Slide 6-24 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Communicating Between Processes Shared access to the same memory is dangerous –need to synchronize access –Producer-Consumer example producer writes data to the shared buffer producer writes flag that data is changed consumer checks flag in buffer consumer reads data
25
Slide 6-25 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6
26
Slide 6-26 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 How is a Process Structured in Memory? Run-time memory image Essentially code, data, stack, and heap Code and data loaded from executable file Stack grows downward, heap grows upward User stack Heap Read/write.data,.bss Read-only.init,.text,.rodata Unallocated Run-time memory address 0 max address
27
Slide 6-27 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 External View of the Process Manager Hardware Application Program Application Program Device MgrProcess MgrMemory Mgr File Mgr UNIX Device MgrProcess MgrMemory Mgr File Mgr Windows CreateThread() CreateProcess() CloseHandle() WaitForSingleObject() fork() exec() wait()
28
Slide 6-28 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Manager Responsibilities Define & implement the essential characteristics of a process and thread –Algorithms to define the behavior –Data structures to preserve the state of the execution Define what “things” threads in the process can reference – the address space (most of the “things” are memory locations) Manage the resources used by the processes/threads Tools to create/destroy/manipulate processes & threads Tools to time-multiplex the CPU – Scheduling the (Chapter 7) Tools to allow threads to synchronization the operation with one another (Chapters 8-9) Mechanisms to handle deadlock (Chapter 10) Mechanisms to handle protection (Chapter 14)
29
Slide 6-29 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Modern Processes and Threads OS interface … … … P i CPU Thrd j in P i Thrd k in P i …
30
Slide 6-30 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Processes &Threads Address Space Map Stack State Program Static data Resources Stack State Map
31
Slide 6-31 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 The Address Space Process Address Space Address Binding Executable Memory Other objects Files
32
Slide 6-32 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Building the Address Space Some parts are built into the environment –Files –System services Some parts are imported at runtime –Mailboxes –Network connections Memory addresses are created at compile (and run) time
33
Slide 6-33 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Tracing the Hardware Process Bootstap Loader Process Manager Interrupt Handler P1P1 P,2 PnPn … Machine is Powered up Initialization Load the kernel Service an interrupt Hardware process progress Execute a thread Schedule
34
Slide 6-34 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 The Abstract Machine Interface User Mode Instructions User Mode Instructions Application Program User Mode Instructions Abstract Machine Instructions Trap Instruction Supervisor Mode Instructions Supervisor Mode Instructions fork() create() open() OS
35
Slide 6-35 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Context Switching Process Manager Interrupt Handler P1P1 P2P2 PnPn Executable Memory Initialization 1 2 3 4 5 7 Interrupt 8 9 6
36
Slide 6-36 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Descriptors OS creates/manages process abstraction Descriptor is data structure for each process –Register values –Logical state –Type & location of resources it holds –List of resources it needs –Security keys –etc. (see Table 6.1 and the source code of your favorite OS)
37
Slide 6-37 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 EPROCESS … void *UniqueProcessId; … NT Executive Windows NT Process Descriptor KPROCESS … uint32 KernelTime; uint32 UserTime; … Byte state; NT Kernel
38
Slide 6-38 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Process Descriptor (2) Kernel process object including: Pointer to the page directory Kernel & user time Process base priority Process state List of the Kernel thread descriptors that are using this process
39
Slide 6-39 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Process Descriptor (3) Parent identification Exit status Creation and termination times. Memory status Security information executable image Process priority class used by the thread scheduler. A list of handles used by this process A pointer to Win32-specific information
40
Slide 6-40 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 ETHREAD Windows NT Thread Descriptor EPROCESS KPROCESS NT Kernel KTHREAD NT Executive
41
Slide 6-41 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Creating a Process in UNIX pid = fork(); UNIX kernel … Process Table Process Descriptor
42
Slide 6-42 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Creating a Process in NT CreateProcess(…); Win32 Subsystem ntCreateProcess(…); … ntCreateThread(…); NT Executive NT Kernel … Handle Table Process Descriptor
43
Slide 6-43 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Handles
44
Slide 6-44 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Simple State Diagram Ready Blocked Running Start Schedule Request Done Request Allocate
45
Slide 6-45 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 UNIX State Transition Diagram Runnable Uninterruptible Sleep Running Start Schedule Request Done I/O Request Allocate zombie Wait by parent Sleeping Traced or Stopped Request I/O Complete Resume
46
Slide 6-46 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Thread States Initialized CreateThread Ready Activate Select Standby Running Terminated Waiting Transition Reinitialize Exit Preempt Dispatch Wait Wait Complete Dispatch
47
Slide 6-47 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Resources R = {R j | 0 j < m} = resource types C = {c j 0 | R j R (0 j < m)} = units of R j available Reusable resource: After a unit of the resource has been allocated, it must ultimately be released back to the system. E.g., CPU, primary memory, disk space, … The maximum value for c j is the number of units of that resource Consumable resource: There is no need to release a resource after it has been acquired. E.g., a message, input data, … Notice that c j is unbounded. Resource: Anything that a process can request, then be blocked because that thing is not available.
48
Slide 6-48 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Using the Model There is a resource manager, Mgr(R j ) for every R j Mgr(R j ) Process p i can only request n i c j units of reusable R j p i can request unbounded # of units of consumable R j Process p i can request units of R j if it is currently running request Mgr(R j ) can allocate units of R j to p i allocate
49
Slide 6-49 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 A Generic Resource Manager Process Resource Manager Process Blocked Processes Resource Pool request() release() Policy
50
Slide 6-50 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Hierarchies Parent-child relationship may be significant: parent controls children’s execution Ready-Active Blocked-Active Running Start Schedule Request Done Request Allocate Ready-Suspended Blocked-Suspended Suspend Yield Allocate Suspend Activate
51
Slide 6-51 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Process Manager Overview Program Process Abstract Computing Environment File Manager Memory Manager Device Manager Protection Deadlock Synchronization Process Description Process Description CPU Other H/W Scheduler Resource Manager Resource Manager Resource Manager Resource Manager Resource Manager Resource Manager Memory Devices
52
Slide 6-52 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 UNIX Organization System Call Interface File Manager Memory Manager Device Manager Protection Deadlock Synchronization Process Description Process Description CPU Other H/W Scheduler Resource Manager Resource Manager Resource Manager Resource Manager Resource Manager Resource Manager Memory Devices Libraries Process Monolithic Kernel
53
Slide 6-53 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Windows NT Organization Processor(s)Main MemoryDevices Libraries Process Subsystem User Subsystem Hardware Abstraction Layer NT Kernel NT Executive I/O Subsystem T T T T T T TT T
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.