1 / 74

Implementing Processes, Threads, and Resources

6. Implementing Processes, Threads, and Resources. Processes. Process Concept Process Scheduling Operating on Processes Threads Internet Links to Multi-threads: IBM Tom Wagner Site. Process Concept. An operating system executes a variety of programs: Batch systems -- Jobs

long
Download Presentation

Implementing Processes, Threads, and Resources

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. Operating Systems: A Modern Perspective, Chapter 6

  2. 6 Implementing Processes, Threads, and Resources Operating Systems: A Modern Perspective, Chapter 6

  3. Processes • Process Concept • Process Scheduling • Operating on Processes • Threads • Internet Links to Multi-threads: • IBM • Tom Wagner Site Operating Systems: A Modern Perspective, Chapter 6

  4. Process Concept • An operating system executes a variety of programs: • Batch systems -- Jobs • Time-Shared system -- user programs or tasks • A Process -- a program in execution • A Process -- a schedulable unit of computation • There may be several processes executing the same program at the same time. E.g. several users running vi at the same time: • Each instance of vi creates a separate process. Operating Systems: A Modern Perspective, Chapter 6

  5. Pi CPU Pj CPU Pk CPU … Pi Executable Memory Pj Executable Memory Pk Executable Memory Pi Address Space Pk Address Space Pj Address Space Implementing the Process Abstraction OS interface OSAddress Space CPU ALU Machine Executable Memory Control Unit … Operating Systems: A Modern Perspective, Chapter 6

  6. CreateThread() CreateProcess() CloseHandle() WaitForSingleObject() Device Mgr Process Mgr Memory Mgr File Mgr Windows External View of the Process Manager Application Program fork() wait() exec() Device Mgr Process Mgr Memory Mgr File Mgr UNIX Hardware Operating Systems: A Modern Perspective, Chapter 6

  7. Process Manager • In a multi-programmed OS, several processes can be “executed at the same time”. • The Process Manager is that part of the OS that is responsible for managing all the processes on the system. • When the computer is powered on, there is only one program in execution: the initial process. • The initial process creates the OS, which can then create other processes as needed. • A process can create another process with a system call (e.g. fork in UNIX). • The created process is called a child process Operating Systems: A Modern Perspective, Chapter 6

  8. Process Manager • When a process is created, it specifies to the Process Manager its resource needs (e.g. memory requirements, files etc.) • The Process Manager allocates the needed resources and causes the process to be executed. • The process manager is responsible • for monitoring the state of each process executing on the system • process scheduling on CPU • process synchronization and deadlock • protection & security Operating Systems: A Modern Perspective, Chapter 6

  9. 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) Operating Systems: A Modern Perspective, Chapter 6

  10. Operating Systems: A Modern Perspective, Chapter 6

  11. Process Model • A process is composed of the following elements: • a program (code) • The data operated on by the process • A set of resources to provide an environment for execution • A Process Descriptor: a record kept by the OS to keep track of the progress of each process. Operating Systems: A Modern Perspective, Chapter 6

  12. Thrdj in Pi Thrdk in Pi … Modern Processes and Threads … Pi CPU … … OS interface Operating Systems: A Modern Perspective, Chapter 6

  13. State Stack Stack Map Processes &Threads State Map Address Space Program Static data Resources Operating Systems: A Modern Perspective, Chapter 6

  14. Process Address Space Stack Section • A process address space includes: • program code • data section • stack section • Process Descriptor is kept in OS space. Data Section Program text Main Memory Operating Systems: A Modern Perspective, Chapter 6

  15. Files Other objects The Address Space Executable Memory Address Space Address Binding Process Operating Systems: A Modern Perspective, Chapter 6

  16. 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 Operating Systems: A Modern Perspective, Chapter 6

  17. Load the kernel Initialization Execute a thread Schedule Service an interrupt Tracing the Hardware Process Machine is Powered up Bootstap Process Manager Interrupt Handler P1 P,2 Pn Loader … Hardware process progress Operating Systems: A Modern Perspective, Chapter 6

  18. Trap Instruction fork() open() create() OS Supervisor Mode Instructions The Abstract Machine Interface Application Program Abstract Machine Instructions User Mode Instructions User Mode Instructions Operating Systems: A Modern Perspective, Chapter 6

  19. Context Switch Operating Systems: A Modern Perspective, Chapter 6

  20. 1 Initialization 7 8 Interrupt 2 3 4 9 5 6 Context Switching Executable Memory Process Manager Interrupt Handler P1 P2 Pn Operating Systems: A Modern Perspective, Chapter 6

  21. 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.3 and the source code of your favorite OS) Operating Systems: A Modern Perspective, Chapter 6

  22. Process Descriptor Contains information associated with a process: • PID • Process state • Owner • Parent process • List of child processes • list of allocated resources • list of open files • …. • Copy of CPU registers at the last time the process executed on the CPU Process Descriptor Table Operating Systems: A Modern Perspective, Chapter 6

  23. EPROCESS NT Executive … void *UniqueProcessId; … Windows NT Process Descriptor KPROCESS … uint32 KernelTime; uint32 UserTime; … Byte state; NT Kernel Operating Systems: A Modern Perspective, Chapter 6

  24. 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 Operating Systems: A Modern Perspective, Chapter 6

  25. 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 Operating Systems: A Modern Perspective, Chapter 6

  26. ETHREAD EPROCESS KPROCESS Windows NT Thread Descriptor KTHREAD NT Kernel NT Executive Operating Systems: A Modern Perspective, Chapter 6

  27. Creating a Process in UNIX pid = fork(); UNIX kernel Process Table … Process Descriptor Operating Systems: A Modern Perspective, Chapter 6

  28. Process Model CONTD Process creation/initialization: • Process Descriptor is created and initialized • Resources needed by the process are allocated (e.g. files, memory to store code, data, and stack). • Process may inherent some resources from its parent (e.g. open files, etc.) • Process Descriptor must reflect all allocated resources • Process is loaded in memory, into its Address Space, ready to begin execution • From then on, process competes for CPU and other resources with other processes. Operating Systems: A Modern Perspective, Chapter 6

  29. Process Creation Operating Systems: A Modern Perspective, Chapter 6

  30. Process Creation Operating Systems: A Modern Perspective, Chapter 6

  31. Process Termination • Process executes last statement and asks the operating system to delete it • process resources are de-allocated by the operating system • A process may be terminated by another process • A parent terminates the execution of its children • When a process exits what happens to its children? • do not allow a child to exist if its parent has terminated ==> cascaded termination (VMS) • allow children to exist after parent ==> orphan processes (UNIX ) Operating Systems: A Modern Perspective, Chapter 6

  32. Creating a Process in NT CreateProcess(…); Win32 Subsystem ntCreateProcess(…); … ntCreateThread(…); NT Executive Handle Table NT Kernel … Process Descriptor Operating Systems: A Modern Perspective, Chapter 6

  33. Windows NT Handles Operating Systems: A Modern Perspective, Chapter 6

  34. Simple State Diagram Request Done Running Request Schedule Start Allocate Ready Blocked Operating Systems: A Modern Perspective, Chapter 6

  35. Process Model CONTD Operating Systems: A Modern Perspective, Chapter 6

  36. Process Scheduling Queues Operating Systems: A Modern Perspective, Chapter 6

  37. UNIX State Transition Diagram Request Wait by parent Done Running zombie Schedule Request Sleeping I/O Request Start Allocate Runnable I/O Complete Resume Traced or Stopped Uninterruptible Sleep Operating Systems: A Modern Perspective, Chapter 6

  38. Windows NT Thread States CreateThread Terminated Initialized Reinitialize Activate Dispatch Exit Wait Waiting Ready Running Wait Complete Wait Complete Preempt Select Transition Dispatch Standby Operating Systems: A Modern Perspective, Chapter 6

  39. Process Model CONTD • Each process uses resources as it executes; main memory, I/O devices, files, and the CPU • The CPU is also a hardware resource • During execution a process may request other resources (e.g. more memory) and may release some of its resources ==> dynamic allocation/de-allocation • When a process can NOT get its requested resources it gets blocked in a queue waiting for that resource. • Multiprogramming: While one process uses the CPU, the remaining are using I/O resources or waiting for a resource (I/O or CPU) to be available. Operating Systems: A Modern Perspective, Chapter 6

  40. Resources Resource: Anything that a process can request, then be blocked because that thing is not available. R = {Rj | 0  j < m} = resource types C = {cj 0 |  RjR (0  j < m)} = units of Rj 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 cj 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 cj is unbounded. Operating Systems: A Modern Perspective, Chapter 6

  41. Process pi can request units of Rj if it is currently running pi can only request ni cj units of reusable Rj pi can request unbounded # of units of consumable Rj • Mgr(Rj) can allocate units of Rj to pi request allocate Using the Model • There is a resource manager, Mgr(Rj) for every Rj Mgr(Rj) Process Operating Systems: A Modern Perspective, Chapter 6

  42. Process Process Process A Generic Resource Manager Resource Manager Blocked Processes Policy request() Process release() Resource Pool Operating Systems: A Modern Perspective, Chapter 6

  43. Process Hierarchies • Parent-child relationship may be significant: parent controls children’s execution Request Done Running Yield Suspend Request Schedule Start Suspend Ready-Active Activate Ready-Suspended Allocate Allocate Suspend Blocked-Active Blocked-Suspended Activate Operating Systems: A Modern Perspective, Chapter 6

  44. Resource Manager Resource Manager Scheduler Resource Manager Process Manager Overview Program Process Abstract Computing Environment Deadlock Process Description File Manager Protection Synchronization Device Manager Memory Manager Devices Memory CPU Other H/W Operating Systems: A Modern Perspective, Chapter 6

  45. Resource Manager Resource Manager Scheduler Resource Manager UNIX Organization Process Libraries Process Process System Call Interface Deadlock Process Description File Manager Protection Synchronization Device Manager Memory Manager Monolithic Kernel Devices Memory CPU Other H/W Operating Systems: A Modern Perspective, Chapter 6

  46. Windows NT Organization Process Process T T Process T T T T Libraries T T T Subsystem Subsystem Subsystem User I/O Subsystem NT Executive NT Kernel Hardware Abstraction Layer Processor(s) Main Memory Devices Operating Systems: A Modern Perspective, Chapter 6

  47. UNIX Processes • Each process has its own address space • subdivided into code, data & stack • a.out file describes the address apace • OS creates a Process Descriptor to manage each process. The collection of all Process Descriptors is referred to as the Process Descriptor Table Operating Systems: A Modern Perspective, Chapter 6

  48. UNIX Processes • Each process is assigned a unique process ID (PID) • The PID is essentially a pointer into the Process Table of the OS. • A process can use the system call getpid() to obtain its own PID • Each process has one parent process (the process that created it), except for process 1 • Process 1 ( the initprocess) is the ancestor of all other processes • a process can use the system call getppid() to obtain the PID of it parent (i.e. PPID) Operating Systems: A Modern Perspective, Chapter 6

  49. UNIX Processes • When Unix is first started, it has only one process. The process is called "init", and its PID is 1. • The "init" process creates other operating system processes to do OS functions • For each port supporting user logins (e.g. a terminal), init creates a process running the getty program. • The getty process waits for a user to begin using the port. • When the port begins to be used, getty creates a new process to run the login program. Operating Systems: A Modern Perspective, Chapter 6

  50. UNIX Processes • The login process prompts the user for username and password, reads the username and password and verifies by looking up the /etc/passwd file. • If login successful, the login process changes directory to the user's directory and creates a new process running the shell program specified in the user's entry of the /etc/passwd file. • The shell process displays a "shell prompt" on the terminal and waits for the user to type a command. Operating Systems: A Modern Perspective, Chapter 6

More Related