1 / 141

Understanding Processes and Threads in Operating Systems

Learn about processes, threads, scheduling, resource ownership, and more in operating systems. Understand process creation, termination, states, threads, models, and key concepts like mutual exclusion and interprocess communication. Explore process hierarchies, states, and transitions. Dive into essential elements of a process, their creation, termination, execution context, and resource allocation. Discover various OS mechanisms for process management and resource efficiency.

carlas
Download Presentation

Understanding Processes and Threads in Operating Systems

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. CSE306 Operating Systems Lecture #2 Processes & Threads Prepared & Presented byAsst. Prof. Dr. Samsun M. BAŞARICI

  2. Topics covered • Processes • What is a process? • Elements of a process • Process modeling • Process creation, termination • Process states • Threads • Thread modeling • Single vs. multithreads • Interprocess communication (IPC) • Race & Critical Regions • Mutual exclusion • Semaphors • Monitors • Sleeping Barber • Dining philosophers • Scheduling

  3. OS Management of Application Execution • Resources are made available to multiple applications • The processor is switched among multiple applications so all will appear to be progressing • The processor and I/O devices can be used efficiently

  4. Scheduling/Execution Processes and Threads Resource Ownership Process includes a virtual address space to hold the process image The OS performs a protection function to prevent unwanted interference between processes with respect to resources Follows an execution path that may be interleaved with other processes • A process has an execution state (Running, Ready, etc.) and a dispatching priority, and is the entity that is scheduled and dispatched by the OS

  5. Processes and Threads (cont.) • The unit of dispatching is referred to as a thread or lightweight process • The unit of resource ownership is referred to as a process or task • Multithreading - The ability of an OS to support multiple, concurrent paths of execution within a single process

  6. Processes • Process is a program in execution • Program code • Data section • Execution context: Program counter, registers, stack • Process has thread(s) of control • Many processes “run” concurrently: Process scheduling • Fair allocation of CPU and I/O bound processes • Context switch

  7. Process Elements • Two essential elements of a process are: • when the processor begins to execute the program code, we refer to this executing entity as a process

  8. Process Elements (cont.) While the program is executing, this process can be uniquely characterized by a number of elements, including:

  9. The Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant

  10. The Process Model (1) Figure 2-1. (a) Multiprogramming of four programs (T)

  11. The Process Model (2) Figure 2-1. (b) Conceptual model of four independent, sequential processes.

  12. The Process Model (3) Figure 2-1. (c) Only one program is active at once.

  13. Process Creation • Creating new processes is expensive • Resource allocation issue • Fork mechanism: UNIX, Windows NT • Duplicate the parent process • Shares file descriptors, memory is copied • Exec to create different process • Various optimizations to avoid copying the entire parent context (Copy on write (COW), etc..) • Exec mechanism: VMS, Windows NT • New process is specifically loaded

  14. Process Creation Four principal events that cause processes to be created: • System initialization. • Execution of a process creation system call by a running process. • A user request to create a new process. • Initiation of a batch job.

  15. Process Creation

  16. Process Termination Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)

  17. Process Creation • Once the OS decides to create a new process it:

  18. Process Termination • There must be a means for a process to indicate its completion • A batch job should include a HALT instruction or an explicit OS service call for termination • For an interactive application, the action of the user will indicate when the process is completed (e.g. log off, quitting an application)

  19. Process Hierarchies • Parent creates a child process, child processes can create its own process • Forms a hierarchy • UNIX calls this a "process group" • Windows has no concept of process hierarchy • all processes are created equal

  20. Process States

  21. Process States (1) Three states a process may be in: • Running (actually using the CPU at that instant). • Ready (runnable; temporarily stopped to let another process run). • Blocked (unable to run until some external event happens).

  22. Two-State Process Model

  23. Process States (1) • Possible process states • running • blocked • ready • Transitions between states shown

  24. Process States (2) • Lowest layer of process-structured OS • handles interrupts, scheduling • Above that layer are sequential processes

  25. Process States (3)

  26. Suspended Processes • Swapping • Involves moving part of all of a process from main memory to disk • When none of the processes in main memory is in the Ready state, the OS swaps one of the blocked processes out on to disk into a suspend queue • This is a queue of existing processes that have been temporarily kicked out of main memory, or suspended • The OS then brings in another process from the suspend queue or it honors a new-process request • Execution then continues with the newly arrived process • Swapping, however, is an I/O operation and therefore there is the potential for making the problem worse, not better. Because disk I/O is generally the fastest I/O on a system, swapping will usually enhance performance

  27. Characteristics of a Suspended Process • The process may or may not be waiting on an event • The process may not be removed from this state until the agent explicitly orders the removal • The process is not immediately available for execution • The process was placed in a suspended state by an agent: either itself, a parent process, or the OS, for the purpose of preventing its execution

  28. Reasons for Process Suspension

  29. Memory Tables • Used to keep track of both main (real) and secondary (virtual) memory • Processes are maintained on secondary memory using some sort of virtual memory or simple swapping mechanism

  30. I/O Tables • Used by the OS to manage the I/O devices and channels of the computer system • At any given time, an I/O device may be available or assigned to a particular process

  31. File Tables • Information may be maintained and used by a file management system • In which case the OS has little or no knowledge of files • In other operating systems, much of the detail of file management is managed by the OS itself

  32. Process Tables • Must be maintained to manage processes • There must be some reference to memory, I/O, and files, directly or indirectly • The tables themselves must be accessible by the OS and therefore are subject to memory management

  33. Process Control Structures

  34. Process Attributes Process Control Structures (cont.) Process Location A process must include a program or set of programs to be executed A process will consist of at least sufficient memory to hold the programs and data of that process The execution of a program typically involves a stack that is used to keep track of procedure calls and parameter passing between procedures • Each process has associated with it a number of attributes that are used by the OS for process control • The collection of program, data, stack, and attributes is referred to as the process image • Process image location will depend on the memory management scheme being used

  35. Typical Elements of a Process Image

  36. Typical Elements of a Process Control Block (page 1 of 2) (Table is located on page 125 in the textbook (Stallings))

  37. Typical Elements of a Process Control Block (page 2 of 2)

  38. Process Identification Each process is assigned a unique numeric identifier Otherwise there must be a mapping that allows the OS to locate the appropriate tables based on the process identifier Many of the tables controlled by the OS may use process identifiers to cross-reference process tables • Memory tables may be organized to provide a map of main memory with an indication of which process is assigned to each region • Similar references will appear in I/O and file tables • When processes communicate with one another, the process identifier informs the OS of the destination of a particular communication • When processes are allowed to create other processes, identifiers indicate the parent and descendents of each process

  39. Processor State Information

  40. Implementation of Processes (1) Fields of a process table entry

  41. Implementation of Processes (2) Skeleton of what lowest level of OS does when an interrupt occurs

  42. Process Control Block

  43. Contains the process elements It is possible to interrupt a running process and later resume execution as if the interruption had not occurred Created and managed by the operating system Key tool that allows support for multiple processes Process Control Block

  44. Role of the Process Control Block • The most important data structure in an OS • Contains all of the information about a process that is needed by the OS • Blocks are read and/or modified by virtually every module in the OS • Defines the state of the OS • Difficulty is not access, but protection • A bug in a single routine could damage process control blocks, which could destroy the system’s ability to manage the affected processes • A design change in the structure or semantics of the process control block could affect a number of modules in the OS

  45. System Mode Modes of Execution User Mode Less-privileged mode User programs typically execute in this mode • More-privileged mode • Also referred to as control mode or kernel mode • Kernel of the operating system

  46. ProcessContext Switch

  47. Typical Functions of an OS Kernel

More Related