Lecture 9 VM & Threads. Review through VAX/VMS The VAX-11 architecture comes from DEC 1970’s The OS is known as VAX/VMS (or VMS) One primary architect.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Background Virtual memory – separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution. Logical.
CSC 501 Operating Systems Principles
Page 15/4/2015 CSE 30341: Operating Systems Principles Allocation of Frames  How should the OS distribute the frames among the various processes?  Each.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 38 Frame Allocation Read.
Chapter 101 Cleaning Policy When should a modified page be written out to disk?  Demand cleaning write page out only when its frame has been selected.
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  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement.
Module 9: Virtual Memory
Module 10: Virtual Memory Background Demand Paging Performance of Demand Paging Page Replacement Page-Replacement Algorithms Allocation of Frames Thrashing.
1 Thursday, July 06, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
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.
Segmentation and Paging Considerations
Virtual-Memory Management
Gordon College Stephen Brinton
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Virtual Memory.
Paging and Virtual Memory. Memory management: Review  Fixed partitioning, dynamic partitioning  Problems Internal/external fragmentation A process can.
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Revisiting Virtual Memory.
Computer Organization and Architecture
1 Chapter 4 Threads Threads: Resource ownership and execution.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
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.
Introduction to Threads CS240 Programming in C. Introduction to Threads A thread is a path execution By default, a C/C++ program has one thread called.
Chapter 9: Virtual Memory Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
Chapter 4 Memory Management Virtual Memory.
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.
CS333 Intro to Operating Systems Jonathan Walpole.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 10: Virtual Memory Background Demand Paging Page Replacement Allocation of.
#include pthread_mutex_t sem_mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t cond_mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 9: Virtual Memory.
Lecture 10 Locks.
Lecture 9 VM & Threads. Virtual Memory Approaches Time Sharing, Static Relocation, Base, Base+Bounds Segmentation Paging Too slow – TLB Too big – smaller.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Processes and Virtual Memory
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Virtual Memory Virtual memory – separation of user logical memory from physical 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 Background Virtual memory – separation of user logical memory.
SLC/VER1.0/OS CONCEPTS/OCT'99
Processes and threads.
Chapter 9: Virtual Memory
CS399 New Beginnings Jonathan Walpole.
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Chapter 9: Virtual-Memory Management
5: Virtual Memory Background Demand Paging
Chapter 9: Virtual Memory
Operating Systems.
Lecture 3: Main Memory.
CS510 Operating System Foundations
Lecture 41 Syed Mansoor Sarwar
Module 9: Virtual Memory
Chapter 9: Virtual Memory
Virtual Memory.
CSE 542: Operating Systems
Presentation transcript:

Lecture 9 VM & Threads

Review through VAX/VMS The VAX-11 architecture comes from DEC 1970’s The OS is known as VAX/VMS (or VMS) One primary architect later led Windows NT VAX-11 has different implementations

Address Space 32-bit virtual address space, 512-byte pages 0-2^31: process space; remaining: system space 23-bit VPN, upper two for segment User page table in kernel virtual memory Page 0 is invalid Kernel virtual address space is part of each user address space, and kernel appears as library Kernel space is protected

Page Replacement PTE: a valid bit, a protection field (4 bits), a modify (or dirty) bit, a field reserved for OS use (5 bits), and finally PFN, but no reference bit! Segmented FIFO Each process has a limit on page numbers Second-chance FIFO with a global clean-page free list and dirty-page list Page Clustering Groups batches of pages from the global dirty list

Other Neat VM Tricks Demand zeroing Zero one page only if it is accessed Copy-on-write (COW) Copy one page only if it is written For copying pages that are rarely changed On process creation Not everything discussed is implemented in VMS Everything discussed could have alternatives

Hashed Page Tables Common in address spaces > 32 bits The virtual page number is hashed into a page table. This page table contains a chain of elements hashing to the same location. Virtual page numbers are compared in this chain searching for a match. If a match is found, the corresponding physical frame is extracted.

Inverted Page Table One entry for each real page of memory Entry consists of the virtual address of the page stored in that real memory location, with information about the process that owns that page Decreases memory needed to store each page table, but increases time needed to search the table when a page reference occurs

Working Set Locality of Reference – a process references only a small fraction of its pages during any particular phase of its execution. The set of pages that a process is currently using is called the working set. Locality model Process migrates from one locality to another Localities may overlap

Thrashing If a process does not have “enough” frames, the page-fault rate is very high This leads to low CPU utilization Thrashing: a process is busy swapping pages in & out Why does thrashing occur? Total size of locality > total memory size

Working-Set Model ∆: working-set window, a fixed number of page references. E.g. 10,000 instructions WSi (working set of Process Pi) = number of pages referenced in the most recent ∆ if ∆ too small will not encompass entire locality if ∆ too large will encompass several localities if ∆ = ∞ will encompass entire program D = ∑ WSi : total demand frames if D > m => Thrashing Policy if D > m, then suspend one of the processes

Working-Set Algorithm The working set algorithm is based on determining a working set and evicting any page that is not in the current working set upon a page fault.

Prepaging So, what happens in a multiprogramming environment as processes are switched in and out of memory? Do we have to take a lot of page faults when the process is first started? It would be nice to have a particular processes working set loaded into memory before it even begins execution. This is called prepaging.

CPU Trends The future: same speed more cores Faster programs => concurrent execution Write applications that fully utilize many CPUs …

Strategy 1 Build applications from many communicating processes like Chrome (process per tab) communicate via pipe() or similar Pros/cons? don’t need new abstractions cumbersome programming copying overheads expensive context switching

Strategy 2 New abstraction: the thread. Threads are just like processes, but they share the address space Same page table Same code segment, but different IP Same heap Thread control block Different stacks, also used for thread-local storage Different registers

Threads vs. Processes Advantages of multi-threading over multi-processes Far less time to create/terminate thread than process Context switch is quicker between threads of the same process Communication between threads of the same process is more efficient Through shared memory

Shared and Not-Shared All threads of a process share resources Memory address space: global data, code, heap … Open files, network sockets, other I/O resources User-id IPC facilities Private state of each thread: Execution state: running, ready, blocked, etc.. Execution context: Program Counter, Stack Pointer, other user-level registers Per-thread stack

Process Address Space single threaded address space kernel space code data heap stack kernel space code data heap thread 1 stack thread 2 stack thread 3 stack shared among threads multi-threaded address space

Thread In single threaded systems, a process is: Resource owner: memory address space, files, I/O resources Scheduling/execution unit: execution state/context, dispatch unit Multithreaded systems Separation of resource ownership & execution unit A thread is unit of execution, scheduling and dispatching A process is a container of resources, and a collection of threads

When to, and not to use threads? Applications Multiprocessor machines Handle slow devices Background operations Windowing systems Server applications to handle multiple requests No threads cases When each unit of execution require different authentication/user-id E.g., secure shell server

#include #include "mythreads.h" #include void *mythread(void *arg) { printf("%s\n", (char *) arg); return NULL; } Int main(int argc, char *argv[]){ if (argc != 1) { fprintf(stderr, "usage: main\n"); exit(1); } pthread_t p1, p2; printf("main: begin\n"); Pthread_create(&p1, NULL, mythread, "A"); Pthread_create(&p2, NULL, mythread, "B"); // join waits for the threads to finish Pthread_join(p1, NULL); Pthread_join(p2, NULL); printf("main: end\n"); return 0; }

#include #include "mythreads.h" #include int max; // shared global variable volatile int counter = 0; void * mythread(void *arg) { char *letter = arg; int i; // stack printf("%s: begin\n", letter); for (i = 0; i < max; i++) { counter = counter + 1; } printf("%s: done\n", letter); return NULL; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usage:...\n"); exit(1); } max = atoi(argv[1]); pthread_t p1, p2; printf("main: begin [counter = %d] [%x]\n", counter, (unsigned int) &counter); Pthread_create(&p1, NULL, mythread, "A"); Pthread_create(&p2, NULL, mythread, "B"); // join waits for the threads to finish Pthread_join(p1, NULL); Pthread_join(p2, NULL); printf("main: done\n [counter: %d]\n [should: %d]\n", counter, max*2); return 0; }

Scheduling Control: Mutex Basic Condition variable pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&lock); x = x + 1; // or whatever your critical section is pthread_mutex_unlock(&lock); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_timedlock(pthread_mutex_t *mutex, struct timespec *abs_timeout);

Scheduling Control: Condition Variable Initilization Wait side Signal side pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t init = PTHREAD_COND_INITIALIZER; Pthread_mutex_lock(&lock); while (initialized == 0) Pthread_cond_wait(&init, &lock); Pthread_mutex_unlock(&lock); Pthread_mutex_lock(&lock); initialized = 1; Pthread_cond_signal(&init); Pthread_mutex_unlock(&lock);

Debugging Concurrency leads to non-deterministic bugs Whether bug manifests depends on CPU schedule! Passing tests means little How to program: imagine scheduler is malicious

Next: lock