#include pthread_mutex_t sem_mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t cond_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

Slides:



Advertisements
Similar presentations
Module 10: Virtual Memory
Advertisements

Chapter 10: Virtual Memory
Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
Chapter 9: Virtual Memory
Page 15/4/2015 CSE 30341: Operating Systems Principles Allocation of Frames  How should the OS distribute the frames among the various processes?  Each.
03/31/2004CSCI 315 Operating Systems Design1 Allocation of Frames & Thrashing (Virtual Memory)
9.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Virtual Memory OSC: Chapter 9. Demand Paging Copy-on-Write Page Replacement.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 15: Background Information for the VMWare ESX Memory Management.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Module 9: Virtual Memory
Advanced Operating Systems - Spring 2009 Lecture 17 – March 23, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:
Module 10: Virtual Memory Background Demand Paging Performance of Demand Paging Page Replacement Page-Replacement Algorithms Allocation of Frames Thrashing.
Chapter 9: Virtual-Memory Management. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 9: Virtual Memory Background Demand Paging.
Virtual Memory Background Demand Paging Performance of Demand Paging
Virtual Memory Introduction to Operating Systems: Module 9.
Virtual-Memory Management
Gordon College Stephen Brinton
Virtual Memory CS 3100 Virutal Memory.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 9: Virtual Memory Background.
Chapter 10: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 10: Virtual Memory.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 9: Virtual Memory.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 9: Virtual Memory Background.
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 9: Virtual Memory.
Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
Page 19/17/2015 CSE 30341: Operating Systems Principles Optimal Algorithm  Replace page that will not be used for longest period of time  Used for measuring.
Chapter 9: Virtual-Memory Management. 9.2Operating System Principles Chapter 9: Virtual-Memory Management Background Demand Paging Copy-on-Write Page.
CS212: OPERATING SYSTEM Lecture 6: Virtual-Memory Management 1 Computer Science Department.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Background Virtual memory –
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 9: Virtual Memory Background.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Chapter 9: Virtual Memory Background.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Virtual Memory.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples Operating.
Operating Systems CMPSC 473 Virtual Memory Management (3) November – Lecture 20 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
9.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts Chapter 9: Virtual-Memory Management Background Demand Paging Page Replacement Allocation.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Outline n Background n Demand.
Chapter 9: Virtual Memory. 9.2 Chapter 9: Virtual Memory Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 9: Virtual-Memory Management.
10.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Chapter 10: Virtual Memory Background Demand Paging Process Creation Page.
Silberschatz, Galvin and Gagne  Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical memory.
9.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 9: Virtual Memory.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 9: Virtual Memory.
9.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 9: Virtual Memory.
1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 9: Virtual Memory.
10.1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 9: Virtual Memory Background Demand Paging Process.
Chapter 9: Virtual Memory. 9.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Background Virtual memory – separation of user logical memory.
Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 9: Virtual Memory.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 9: Virtual-Memory Management.
Chapter 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual Memory
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
#include <pthread.h>
Module 9: Virtual Memory
Virtual Memory.
Presentation transcript:

#include pthread_mutex_t sem_mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t cond_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_t tip ; pthread_t tip1 ; int rander ; void *sender(void *) ; void *receiver(void *) ; sem_t sema_dude ; main() { rander = rand() % 10 ; //returns random num between printf("Rander is %d\n", rander) ; sleep(rander) ; sem_init(&sema_dude, 0, 0); pthread_create(&tip, NULL, sender, NULL) ; pthread_create(&tip1, NULL, receiver, NULL) ; pthread_join(tip, NULL); pthread_join(tip1, NULL) ; }

void *sender (void *param) {printf("Hello world!!\n") ; sem_wait(&sema_dude) ; printf("Im back\n") ; } void *receiver (void *param) {printf("Hello from ME!\n") ; sleep(2) ; sem_post(&sema_dude) ; }

gcc try_sem.c -lpthread -lposix4 //on gandalf (g)cc try_sem.c –lpthread //most Linux systems

Random Number Generator #include int rand() //returns a random integer, not double int my_rand = rand() % 20 ; //returns a random int between 0 and 19

Page 240: sem_t sem mutex incorrect. sem_t mutex ; while (true) { sleep(….) ; rand = rand() ; …… } while (true) {sleep_time = rand() % 10 ; sleep(sleep_time) ; ……………..}

Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical address space can therefore be much larger than physical address space. Allows address spaces to be shared by several processes. Allows for more efficient process creation. Virtual memory can be implemented via: Demand paging Demand segmentation

Shared Library Using Virtual Memory

Demand Paging Bring a page into memory only when it is needed. Less I/O needed Less memory needed Faster response More users (higher level of multiprogramming) Page is needed  reference to it invalid reference  abort not-in-memory  bring to memory

Page Table When Some Pages Are Not in Main Memory

Page Fault If there is ever a reference to a page, first reference will trap to OS  page fault OS decides: Invalid reference  abort. Just not in memory. Get empty frame.

Page Fault Get empty frame. Swap page into frame. Reset tables, validation bit = 1. Restart instruction

Steps in Handling a Page Fault

What happens if there is no free frame? Page replacement – find some page in memory, but not really in use, swap it out. algorithm performance – want an algorithm which will result in minimum number of page faults. Same page may be brought into memory several times.

Page Replacement Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk. Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory.

Basic Page Replacement 1. Find the location of the desired page on disk. 2. Find a free frame: - If there is a free frame, use it. - If there is no free frame, use a page replacement algorithm to select a victim frame. 3. Read the desired page into the (newly) free frame. Update the page and frame tables. 4. Restart the process.

Page Replacement Algorithms Want lowest page-fault rate. Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string. In examples, the reference string is: 7,0,1,2,0, 3,0,4,2,3, 0,3,2,1,2,0,1,7,0,1

Optimal Algorithm Replace page that will not be used for longest period of time. Used for measuring how well your algorithm performs.

Optimal Page Replacement

First-In-First-Out Throw out the page that has been in memory the longest. Good when talking about a set of pages for initialization. Bad when talking about heavily used variable.

FIFO Page Replacement

Least Recently Used (LRU) Algorithm Based on principal of “Locality of Reference”. A page that has been used in the near past is likely to be used in the near future. LRU: Determine the least recently used page in memory and evict it. Can be done but very expensive.

LRU Page Replacement

Second Chance Approximations Add a “reference” bit to page table. Set to 1 when page is accessed. Set of “second chance” algorithms that use the reference bit in page table entry to determine if it has been recently used. Example: Clock Page Replacement Algorithm.

Second-Chance (clock) Page-Replacement Algorithm

Other Software Approximations to LRU Not Frequently Used (NFU). Associate a software counter with each page. On timer interrupt, OS scans all pages in memory. For each page, the R bit (Referenced bit)is added to the counter. Page with lowest count is evicted.

Problem with NFU It never forgets. A page referenced often in earlier phases of the program may not be evicted long after it has been used. Would like to have an algorithm that “ages” the count. That is, the latest references should be the most important.

Aging Algorithm for Simulating LRU On each timer interrupt scan the pages to get the R bit. Shift right one bit of the counter. Place the R bit in the leftmost bit of the counter. Choose the page to evict that has the lowest count.

Example Assume all counters are currently 0. Consider the case when pages 0,2,4, and 5 are referenced between last interrupt.

Simulating LRU in Software The aging algorithm simulates LRU in software

Simulating LRU in Software The aging algorithm simulates LRU in software Assume references 0,1, and 4 next window.

Simulating LRU in Software The aging algorithm simulates LRU in software

Simulating LRU in Software The aging algorithm simulates LRU in software Assume 0,1,3,5

Simulating LRU in Software The aging algorithm simulates LRU in software

Allocation of Frames Each process needs minimum number of pages. Two major allocation schemes. fixed allocation Variable allocation. Replacement Scope can be: Local. Global.

Fixed Allocation, Local Scope Number of pages per process is fixed based on some criteria. Can use equal allocation or proportional allocation. Equal allocation – e.g., if 100 frames and 5 processes, give each 20 pages. What are the drawbacks of equal allocation?

Fixed Allocation, Local Scope Proportional Allocation Allocate number of pages based on the size of the process. Problem?

Fixed Allocation, Local Scope Equal allocation – e.g., if 100 frames and 5 processes, give each 20 pages. What are the drawbacks of this approach? Allocation may be too small causing significant paging. Allocation may too large reducing number of processes in memory and wasting memory that could be used by other processes.

Fixed Allocation, Local Scope Proportional Allocation Allocate pages based on the size of the process. Problem? Process needs will vary over its execution leading to the same problems as equal-size pages.

Problem with Fixed Allocation Schemes All processes treated the same. No priorities. Can use process priority rather than size to allocate frames.

Variable Allocation, Global Replacement When a page fault occurs, new page frame allocated to the process. Page replacement based on previous approaches: e.g., LRU, FIFO, etc. No consideration of which process should (or can best afford) to lose a page. Can lead to high page-fault rates.

Thrashing If a process does not have “enough” pages, the page-fault rate is very high. This leads to: low CPU utilization. operating system thinks that it needs to increase the degree of multiprogramming. another process added to the system. Thrashing  a process is busy swapping pages in and out.

Thrashing

Locality In A Memory-Reference Pattern

Working-Set Model: Local Scope, Variable Allocation   working-set window  a fixed number of page references Example: 10,000 instruction WSS i (working set of Process P i ) = total number of pages referenced in the most recent  (varies in time) if  too small will not encompass entire locality. if  too large will encompass several localities. if  =   will encompass entire program. D =  WSS i  total demand frames if D > m (memory)  Thrashing Policy if D > m, then suspend one of the processes.

Working-set model

Page-Fault Frequency Scheme Establish “acceptable” page-fault rate. If actual rate too low, process loses frame. If actual rate too high, process gains frame.

Other Considerations Prepaging: Predicting future page requests. Page size selection fragmentation table size I/O overhead

Other Considerations TLB Reach - The amount of memory accessible from the TLB. TLB Reach = (TLB Size) X (Page Size) Ideally, the working set of each process is stored in the TLB.

Increasing the Size of the TLB Increase the Page Size. This may lead to an increase in fragmentation as not all applications require a large page size. Provide Multiple Page Sizes. This allows applications that require larger page sizes the opportunity to use them without an increase in fragmentation.

Other Considerations I/O Interlock – Pages must sometimes be locked into memory. Consider I/O. Pages that are used for copying a file from a device must be locked from being selected for eviction by a page replacement algorithm.

Windows NT Uses demand paging with clustering. Clustering brings in pages surrounding the faulting page. Processes are assigned working set minimum and working set maximum. Working set minimum is the minimum number of pages the process is guaranteed to have in memory.

Windows NT A process may be assigned as many pages up to its working set maximum. When the amount of free memory in the system falls below a threshold, automatic working set trimming is performed to restore the amount of free memory. Working set trimming removes pages from processes that have pages in excess of their working set minimum.