Partial preview of the text
Download operating system memory heirarchy and more Summaries Computer science in PDF only on Docsity!
UNIT-1 Program vs Process : 1) A process is a program in execution 2) Its is an instance of a running program 3) A Program is not a Process by default. A program is a passive entity, i.e. a file containing a list of instructions stored on disk (secondary memory) (often called an executable file). 4) Aprogram becomes a Process when an executable file is loaded into main memory and when it’s PCB is created. 5) A process is an Active Entity, which requires resources like main memory, CPU time, registers, system buses etc. eth i e ile div a tnke \ sec tery Process Memory Layout A process is divided into different memory areas to store its code, data, and information required during execution. 1. Text Segment e It contains the executable instructions (program code) of the process. e It tells the CPU what operations have to be performed. e Itis generally read-only so that the program instructions are not accidentally modified. e The Program Counter (PC) keeps track of the instruction that is currently being executed. 2. Data Segment e Itcontains the global and static variables of the process. e These variables remain allocated throughout the lifetime of the process. e tis used for storing data that is required by the program for a long period of time. 3. Heap The heap is used for dynamic memory allocation. Memory is allocated from the heap during the execution of the program, whenever required. e ts size can increase or decrease depending on the memory requirements of the process. e tis mainly used for data whose size or lifetime cannot be determined in advance. 4. Stack e The stack stores temporary information required during function execution. e It contains things such as: o Local variables o Function parameters © Return information © Function call information e Stack memory is automatically allocated when a function is called and released when the function finishes. e Therefore, it is mainly used for function calls and temporary data. | Stack | Temporary data | Function calls |—___—_- | | Heap | Dynamic memory | Data | Global/static | variables l Text | Program/Code | instructions ———$ 4. CPU-scheduling information: This information includes a process process state priority, pointers ta scheduling queues, and any other scheduling process number parameters program counter 5. Memory-management information: This information may include fegisters such items as the value of the base and limit regist and the page tables, or the segment tables, depending on the memory system used by the operating system. memory limits list of open files 6. Accounting information: This information includes the amount of CPU and real time used, time limits, account numbers, job or process numbers, and so on. 7.1/0 status information: This information includes the list of 1/0 devices allocated to the process, a list of open files, and so on. Process States A Process changes states as it executes. The state of a process is defined in parts by the current activity of that process. A process may be in one of the following states: New: The process is being created. admitted interrupt exit terminated Running: Instructions are being executed. S Vata i ready running Waiting (Blocked): The process is waiting for some event to occur (such as an I/O completion or reception of a signal). scheduler dispatch VO or event completion WO or event wait waiting Ready: The process is waiting to be assigned to a processor. Terminated: The process has finished execution. Inter Pr mmunication 1. Exchnage of data between 2 or more separate,independent processes. 2. Operating System provides the facilities for inter process communication. 3. It may be: a. Unicast : Communication is from 1 process to single other process. Eg : socket communication b. Multicast : Communication is from 1 process to group of processes Eg : publish/subscribe models There are 2 main models of Inter-Process Communication (IPC): 1. Shared Memory Model A region of memory is shared between two or more processes. Processes communicate by reading and writing data in this shared memory area. The operating system creates the shared memory, and processes can access it. Processes need synchronization mechanisms such as semaphores or mutexes to avoid conflicts when accessing shared data. It is generally faster because data does not need to be repeatedly copied between processes. 2. Message Passing Model Processes communicate by sending and receiving messages through the operating system. A process does not directly access another process's memory. The OS provides mechanisms such as send() and receive() for communication. It is useful when processes are running on different systems or machines. It is generally simpler to implement than shared memory. msgget() is used to create a new message queue or access an existing message queue. e key — uniquely identifies the message queue. e flag — specifies the permissions and creation options for the queue. e lItreturns a message queue identifier (ID) if successful. Thread A thread is the basic unit of CPU utilization within a process. It represents a single sequence of instructions that can be executed by the CPU. A thread consists of its own: Program Counter (PC) — keeps track of the next instruction to execute. Stack — stores function calls, local variables, and temporary data. Registers — store temporary data and execution information. Thread ID — uniquely identifies the thread. Single-threaded Process A traditional process has only one thread of control. Therefore, it has: One program counter One set of registers One stack One sequence of instructions being executed at a time Multithreaded Process A multithreaded process contains multiple threads within the same process. Each thread has its own: Program Counter Registers Stack Thread ID But all threads of the same process share: e Code e Data e Open files e Other process resources code data files | code data files registers stack registers ||| registers ||| registers stack stack stack thread ——> S S : Pa thread single-threaded process. multithreaded process Process vs Thread Processes are isolated from each other by separate address spaces. Threads within the same process can directly access shared memory. Communication between processes generally requires IPC mechanisms such as pipes, message queues, or shared memory. Threads can communicate through the shared memory/resources of their process, although synchronization is needed when accessing shared data. If one process is blocked, other independent processes can continue executing. If one thread is blocked, another thread of the same process can execute, provided it is runnable and the process is not otherwise prevented from executing. A process can contain multiple threads. A thread always belongs to a process. Thread Life Cycle / Thread States A thread life cycle represents the different states through which a thread passes from its creation until its completion. 1. New State e The thread is created but has not yet started execution. e When the thread is started, it is moved to the Ready state. 2. Ready State e The thread is ready to execute and is waiting for the CPU to be allocated. e The thread remains in the ready queue until the CPU scheduler selects it. e When the scheduler dispatches the thread, it moves to the Running state. 3. Running State e The thread is currently being executed by the CPU. e From the Running state, a thread can move to different states depending on the event that occurs. Running — Ready e When the time slice expires or the thread is preempted, it returns to the Ready state. Running — Waiting/Blocked e When the thread has to wait for an I/O operation, resource, event, or synchronization condition, it enters the Waiting/Blocked state. Running — Terminated e When the thread completes its execution, it enters the Terminated state. 4. Waiting/Blocked State e The thread is unable to continue execution because it is waiting for some event or resource. It does not compete for the CPU while waiting. Once the required event or resource becomes available, the thread moves back to the Ready state. 5. Terminated State e The thread has completed its execution or has been explicitly terminated. e It will not execute again. e The resources associated with the thread can then be released. + Thread is created but New: has not yet started execution. | Ready: || + Thread is ready to execute and waiting for CPU, «= I is in the ready queue. 1/0 done, time slice resouree available or natifyO/notifyAMlQ, Blocked / Waiting: * ‘Thread is waiting for some 1/0 operation, resource, ete. @: edoes nat compete for the CEU. © When the required event/resource becomes available, it moves to Ready 1/0 request or wait() expires (preemption) R | completion or stop() Terminated (Dead) ‘Terminated / Dead: junning: ‘© Thread is currently executing ‘on the CPU. + Temay move t faiting/Blocked or Terminated g on the event. ** Thread has completed execution | en explicitly terminated, Disadvantages use seve 2 y pa A blocking system call may block the entire process in a many-to-one implementation. In a many-to-one model, true parallel execution on multiple CPUs is not possible for the user threads. The kernel cannot schedule individual user threads independently. If the process is blocked, all its user-level threads may be unable to execute. ead _{t! — 2. Kernel-Level Threads (KLT) A Kernel-Level Thread is a thread that is directly managed by the operating-system kernel. Characteristics A PYWON> 2 KLTs are managed directly by the operating-system kernel. The kernel is aware of each individual thread. The kernel performs thread scheduling. The kernel can block, wake, suspend, and terminate individual threads. If one thread blocks for I/O, the kernel can schedule another runnable thread of the same process. Kernel-level threads can be scheduled on multiple CPU cores, allowing parallel execution when multiple threads are runnable. Thread management generally involves kernel operations, resulting in greater overhead than ULT management. Advantages e If one thread blocks, other runnable threads of the same process can continue executing. Supports parallel execution on multiple processors/cores. The kernel can schedule individual threads according to system scheduling policies. Better integration with I/O and system calls because the kernel directly manages the threads. Disadvantages Higher overhead because thread management involves the kernel. Thread creation and termination are generally slower than ULTs. Context switching between kernel-managed threads can have greater overhead than switching between user-level threads. e More kernel resources are required to maintain information about threads. Many-to-One Threading Model The many-to-one model is a thread mapping model in which many user-level threads are mapped to a single kernel-level thread. Characteristics 1. Multiple user-level threads are mapped to one kernel-level thread. 2. The user-level threads are managed by a thread library, while the kernel sees only one kernel thread. Since there is only one kernel thread, only one user thread can execute at a time. 4. The model does not support parallel execution of the individual user threads on multiple CPU cores. 5. If one user thread performs a blocking system call, the single kernel thread becomes blocked, causing the entire process to block. 6. The model has low thread-management overhead because the kernel does not manage each user-level thread separately. 2 Advantages Fast thread management because threads are managed in user space. Low overhead compared with models where every user thread has a kernel thread. Thread switching can be performed without requiring the kernel for every switch. Simple to implement. Disadvantages 1. Higher overhead Every user thread requires a corresponding kernel thread, so the kernel has to maintain information about each thread. 2. More resource consumption Creating and maintaining kernel threads requires more system resources. 3. Thread creation may be slower Since kernel involvement is required, creating and managing threads generally has more overhead than user-level thread management. 4. System may limit the number of threads Because kernel threads consume system resources, operating systems may impose limits on how many threads can be created. +— user thread Many-to-Many Threading Model The many-to-many model maps multiple user-level threads to an equal or smaller number of kernel-level threads. It combines advantages of both the many-to-one and one-to-one models. Characteristics 1. Multiple user-level threads are mapped to multiple kernel-level threads. 2. The number of kernel threads is equal to or smaller than the number of user threads. 3. The user can create many user-level threads, while the operating system manages a suitable number of kernel threads. 4. If one user thread performs a blocking system call, other threads can continue executing through other available kernel threads. 5. The process can be executed on multiple CPU cores simultaneously. 6. The operating system can use different numbers of kernel threads depending on factors such as the number of available CPUs and system resources. Advantages 1. Blocking does not necessarily stop the entire process If one thread is blocked, other user threads can still execute using other kernel threads. 2. Supports parallelism Multiple kernel threads can be scheduled on different CPUs Therefore, the process can take advantage of multiple processors/cores. 3. Better scalability than one-to-one A large number of user threads does not necessarily require the same number of kernel threads, reducing kernel resource requirements. 4. Combines advantages of ULT and KLT e User-level threads provide lower management overhead and flexibility. e Kernel-level threads provide parallelism and better handling of blocking operations. Disadvantages The implementation is more complex than many-to-one or one-to-one models. The operating system and thread library must coordinate the mapping between user threads and kernel threads. e Managing the appropriate number of kernel threads adds complexity. Working Suppose many programs are waiting on disk: Disk / Job Pool: P1 P2 P3 P4 P5 P6 The long-term scheduler selects some processes: P1 P3 P5 Ready Queue These processes are then available for CPU scheduling. Characteristics Controls the degree of multiprogramming. Determines which processes are admitted into memory. Runs less frequently than the short-term scheduler. The selection process can take relatively more time. It focuses on long-term system goals such as maximizing throughput. Tries to maintain a good balance between CPU-bound and I/O-bound processes. 2. Short-Term Scheduler The Short-Term Scheduler (STS) is also called the CPU Scheduler. It selects a process from the ready queue and allocates the CPU to it. Working Suppose: Ready Queue: P1 P2 P3 The short-term scheduler selects one process: P1— CPU After some time, another process may be selected according to the CPU scheduling algorithm. Characteristics ° oo00 Selects a process from the ready queue. Allocates the CPU to the selected process. Runs very frequently. Must be very fast because CPU scheduling occurs frequently. Uses CPU scheduling algorithms such as: FCFS SJF SRTF Priority Scheduling Round Robin 5. Medium-Term Scheduler The Medium-Term Scheduler (MTS) is responsible for temporarily removing processes from main memory and later bringing them back. This process is called swapping. Characteristics Responsible for swapping processes in and out of memory. Helps manage memory utilization. Can reduce the degree of multiprogramming temporarily. Runs less frequently than the short-term scheduler. ls particularly associated with systems that use swapping. Dispatcher in Operating System The dispatcher is an OS component that gives control of the CPU to the process selected by the short-term scheduler. The short-term scheduler decides "Which process should run next?" The dispatcher then makes it happen: "Give the CPU to that process."