ASSIGNMENT BY NIKHIL GUPTA 2022A1R090 3RD SEM CSE DEPARTMENT Model Institute of Engineering & Technology (Autonomous) (Permanently Affiliated to the University of Jammu, Accredited by NAAC with “A” Grade) Jammu, India 2023 ASSIGNMENT COM-302 Operating Systems Max Marks: 20 Submission Deadline: 4th December 2023 Questions Course Outcome Skill Bloom’s level Task-1 CO1, CO2 & CO5 Apply & Analyzing 3,4 Task-2 CO3, CO4 Apply & Analyzing 3,4 Assignment Objectives: • • • Design and implement Round Robin algorithm for process scheduling. Create processes, vary quantum times, and analyze algorithm performance. Implement IPC methods (message passing, shared memory) for process communication. Learning Outcomes: • Deep understanding of Round Robin scheduling and its application. • Proficiency in process scheduling and analytical performance evaluation skills. • Mastery of IPC concepts, including message passing and shared memory. INDEX: Q.No. 1 2 Questions BL CO Page No. Total Marks Design a program that implements Round Robin scheduling Algorithm. Create a set of processes with specified quantum time and demonstrate how the operating system schedules these processes. Implement and analyze the algorithm with at least 3 different specified quantum time. 3, 4 CO1, CO2, CO5 10 Design and implement various methods for IPC, such as message passing or shared memory, to facilitate communication between processes in the operating system. 3, 4 CO3, CO4 10 ASSIGNMENT (COM-302 Operating System) Name: Nikhil Gupta Roll NO.: 2022A1R090 Branch: CSE Section: A3 Programming Language Used: Python Submitted to: Dr. Mekhla Sharma GROUP E: 2022A1R087 to 2022A1R094 Questions Task-1 Course Outcome CO1, CO2 & CO5 Skill Applying & analyzing Bloom’s Level 3,4 Task-2 CO3, CO4 Applying & analyzing 3,4 Task 1: Write a program that simulates page replacement algorithms like FIFO, LRU, and Optimal Page Replacement. Create a memory management system that swaps pages in and out to demonstrate the effectiveness of these algorithms in different scenarios. Task 2: Implement a program that simulates the Reader-Writer problem, allowing multiple readers or a single writer to access a shared resource. Use semaphores or another synchronization mechanism to maintain data consistency. Explain the differences between reader and writer processes in terms of synchronization. GROUP DETAILS & TEAM MEMBER’S NAME Kinjal Parihar Nikhil Gupta Mitali Kotwal Adnan Malik Riya Warikoo Laksh Heer ROLL NO 2022A1R087 2022A1R090 2022A1R091 2022A1R092 2022A1R093 2022A1R094 Task 1 Algorithm Explanation: FIFO Algorithm: 1. Description: o FIFO (First-In-First-Out) is a straightforward page replacement algorithm. o It maintains a queue of pages in memory and replaces the oldest page when a page fault occurs. 2. Implementation: o The algorithm uses a list (frames) to represent the memory frames. o When a page fault occurs: If the page is not in the frames, it is added to the frames. If the frames are full, the oldest page (first one that entered) is removed, and the new page is added. 3. Execution Sequence: o The algorithm iterates through the page reference string, maintaining the frames and replacing pages when necessary. LRU Algorithm: 1. Description: o LRU (Least Recently Used) prioritizes pages based on their recency of use. o It replaces the least recently used page when a page fault occurs. 2. Implementation: o The algorithm maintains an order (page_order) of page usage, representing the recency. o When a page fault occurs: If the page is not in the frames, it is added to the frames along with its order. If the frames are full, it identifies the least recently used page, removes it, and adds the new page with updated order. 3. Execution Sequence: o The algorithm iterates through the page reference string, updating the order and replacing pages based on recency. Optimal Algorithm: 1. Description: o The Optimal algorithm is an idealized page replacement strategy. o It makes replacement decisions based on perfect knowledge of future page accesses. 2. Implementation: o The algorithm looks ahead in the page reference string to identify the page that will not be used for the longest time. o When a page fault occurs: If the page is not in the frames, it replaces the page that will not be used for the longest time. 3. Execution Sequence: o The algorithm iterates through the page reference string, making replacement decisions based on future page accesses. Overall: Each algorithm provides a unique approach to handling page replacements in a memory management system. FIFO is straightforward but may not adapt well to changing access patterns. LRU prioritizes recently used pages, allowing for better adaptation to dynamic scenarios. Optimal serves as a benchmark, showcasing the theoretical maximum efficiency by considering future page accesses. Execution Sequence: Simulating Page Replacement Algorithms Execution Sequence for FIFO Algorithm: Execution Sequence for LRU Algorithm: Execution Sequence for Optimal Algorithm: CODE EXPLANATION 1. Class Definition: PageReplacementSimulator Overview: The PageReplacementSimulator class serves as a framework for simulating various page replacement algorithms. 2. Initialization Method: __init__ Purpose: Initialize the simulator with a page reference string and frame size. This sets up the initial parameters for the simulation. page_reference_string: Stores the sequence of page accesses. frame_size: Represents the number of frames in the memory. frames: Keeps track of the current state of memory frames. page_faults: Counts the number of page faults during simulation. 3. FIFO Algorithm Simulation: fifo Overview: Simulates the First-In-First-Out (FIFO) page replacement algorithm. The method iterates through the page reference string. If a page is not in memory (self.frames), it is added. If frames are full, the oldest page is removed, and the new page is added. Page faults are incremented, and the current state is printed. 4. LRU Algorithm Simulation: lru Overview: Simulates the Least Recently Used (LRU) page replacement algorithm. The method maintains a page_order list to track the recency of page usage. If a page is not in memory, it is added along with its order. If frames are full, the least recently used page is replaced. Page faults are incremented, and the current state is printed. 5. Optimal Algorithm Simulation: optimal Overview: Simulates the Optimal page replacement algorithm. The method looks ahead in the page reference string to identify the page that will not be used for the longest time. If a page is not in memory, it is added. If frames are full, a replacement decision is made based on future page accesses. Page faults are incremented, and the current state is printed. Example Usage Section: 1. FIFO Algorithm: o o o Initialize a simulator with a page reference sequence and frame size. Simulate the First-In-First-Out (FIFO) page replacement algorithm. Display the memory state and page faults at each step. 2. LRU Algorithm: o o o Create a new simulator instance for a fresh simulation. Simulate the Least Recently Used (LRU) page replacement algorithm. Display the memory state and page faults at each step. 3. Optimal Algorithm: o o o Create another simulator instance for a fresh simulation. Simulate the Optimal page replacement algorithm. Display the memory state and page faults at each step. Program source code TASK 2 Algorithm Explanation: Reader Process Algorithm: 1. Acquiring Mutex: o The mutex semaphore is a synchronization mechanism that ensures exclusive access to the critical section where the readers count variable is updated. o This step prevents multiple readers from simultaneously modifying the count, ensuring data consistency. 2. Incrementing Readers Count: o The readers count variable is incremented, representing the arrival of a reader. o This count keeps track of how many readers are currently accessing the shared resource. 3. Blocking Writers: o If the current reader is the first one (readers count == 1), it acquires the resource access semaphore. o The resource access semaphore ensures that no writers can access the shared resource when readers are present. o This step prevents the introduction of inconsistencies that might arise from concurrent read and write operations. 4. Releasing Mutex: o The mutex semaphore is released, allowing other readers to enter the critical section and update the readers count variable. o This release is crucial for achieving concurrent access by multiple readers. 5. Reading Shared Resource: o The reader now has access to the shared resource, and it performs the read operation. o The shared resource represents a piece of data that multiple readers can access concurrently without conflicts. 6. Acquiring Mutex (Again): o After reading, the reader reacquires the mutex semaphore to update the readers count variable. 7. Decrementing Readers Count: The readers count variable is decremented, indicating the departure of a reader. o This count ensures that the system is aware of the number of active readers. 8. Releasing Resource Access for Writers: o If the current reader is the last one (readers count == 0), it releases the resource access semaphore. o Releasing this semaphore allows writers to access the shared resource, ensuring that they have exclusive access. 9. Releasing Mutex: o The mutex semaphore is released again, allowing other readers to update the readers count variable. o This completion of the reader's operation ensures a balanced and synchronized access pattern. o Writer Process Algorithm: 1. Acquiring Resource Access: o The writer acquires the resource_access semaphore, which ensures exclusive access to the shared resource. o By acquiring this semaphore, the writer blocks other writers and readers from accessing the resource concurrently. 2. Writing to Shared Resource: o With exclusive access, the writer performs a write operation on the shared resource. o The shared resource could represent a critical piece of data that needs to be updated atomically. 3. Releasing Resource Access: o After writing, the writer releases the resource_access semaphore, allowing other writers and readers to access the shared resource. o Releasing this semaphore is crucial to maintaining a balance between read and write operations. EXECUTION SEQUENCE: Unraveling the Reader-Writer Ballet 1. Class: ReaderWriterProblem ##__init__(self)## Purpose: Initializes the ReaderWriterProblem class. Attributes: o o o o shared_resource: Represents the shared resource that readers and writers access. readers_count: Keeps track of the number of active readers. mutex: Semaphore to control access to readers_count. resource_access: Semaphore to control access to the shared resource. 2.Method: reader(self, reader_id) Purpose: Represents the behavior of a reader thread. Steps: 1. Acquiring Mutex: o self.mutex.acquire(): Ensures exclusive access to update readers_count. 2. Incrementing Readers Count: o self.readers_count += 1: Marks the arrival of a reader. 3. Blocking Writers: o If it's the first reader (self.readers_count == 1), acquires resource_access to block writers. 4. Releasing Mutex: o self.mutex.release(): Allows other readers to update readers_count. 5. Reading Shared Resource: o Prints the current value of the shared resource. 6. Acquiring Mutex (Again): o self.mutex.acquire(): Ensures exclusive access to update readers_count. 7. Decrementing Readers Count: o self.readers_count -= 1: Marks the departure of a reader. 8. Releasing Resource Access for Writers: o If it's the last reader (self.readers_count == 0), releases resource_access to allow writers. 9. Releasing Mutex: o self.mutex.release(): Allows other readers to update readers_count. 3. Method: writer(self, writer_id) Purpose: Represents the behavior of a writer thread. Steps: 1. Acquiring Resource Access: o self.resource_access.acquire(): Blocks other writers and readers from accessing the shared resource. 2. Writing to Shared Resource: o Increments the value of the shared resource. 3. Releasing Resource Access: o self.resource_access.release(): Allows other writers and readers to access the shared resource. 4. Function: main() Purpose: Initializes the ReaderWriterProblem class and starts reader and writer threads. Steps: 1. Initialization: o Creates an instance of ReaderWriterProblem named problem. 2. Creating Threads: Creates reader threads (reader_threads) and writer threads (writer_threads). o Threads are initiated with the target set to the respective reader or writer method. 3. Starting Threads: o Starts all reader and writer threads using the start() method. 4. Waiting for Threads to Finish: o Uses the join() method to wait for all threads to complete. o ##SOURCE CODE##