S C I E N C E P A S S I O N T E C H N O L O G Y
Systemnahe Programmierung KU
A6,A7Daniel Gruß, IAIK, Graz University of Technology
www.iaik.tugraz.at
1. Virtual Memory
www.iaik.tugraz.at
Course Overview
A1, A2, A3
Git, Compiler, C, C++
A4, A5, A6
Multithreading, Processes, Virtual Memory
A7
Thread Interaction and Synchronization
A8, A9
www.iaik.tugraz.at
What is a pointer?
Address in memory? Index in RAM?
www.iaik.tugraz.at
What is a pointer?
Address in memory
? Index in RAM?
www.iaik.tugraz.at
What is a pointer?
Address in memory? Index in RAM
www.iaik.tugraz.at
What is a pointer?
Address in memory? Index in RAM?
www.iaik.tugraz.at
Segmentation fault
Common scenario: User program accesses “invalid” memory location
The OS invokes a fault handler
This fault handler can abort the user program orfix the situationby “making” the address valid
Efficient: Don’t assign memory until necessary Are pointers addresses in physical memory?
www.iaik.tugraz.at
Segmentation fault
Common scenario: User program accesses “invalid” memory location
The OS invokes a fault handler
This fault handler can abort the user program orfix the situationby “making” the address valid
Efficient: Don’t assign memory until necessary Are pointers addresses in physical memory?
www.iaik.tugraz.at
Segmentation fault
Common scenario: User program accesses “invalid” memory location
The OS invokes a fault handler
This fault handler can abort the user program orfix the situationby “making” the address valid
Efficient: Don’t assign memory until necessary
Are pointers addresses in physical memory?
www.iaik.tugraz.at
Segmentation fault
Common scenario: User program accesses “invalid” memory location
The OS invokes a fault handler
This fault handler can abort the user program orfix the situationby “making” the address valid
Efficient: Don’t assign memory until necessary Are pointers addresses in physical memory?
www.iaik.tugraz.at
Virtual Memory
Pointers are not addresses in physical memory
Pointers are virtual addresses
Addresses are translated from virtual addresses to real physical addresses transparently
Therefore, memory is split into parts called “pages” Operating system can “map” pages
Operating system can maintain this mapping per process
→ different processes can use the same addresses, but “see” different things there
www.iaik.tugraz.at
Virtual Memory
Pointers are not addresses in physical memory
Pointers are virtual addresses
Addresses are translated from virtual addresses to real physical addresses transparently
Therefore, memory is split into parts called “pages” Operating system can “map” pages
Operating system can maintain this mapping per process
→ different processes can use the same addresses, but “see” different things there
www.iaik.tugraz.at
Virtual Memory
Pointers are not addresses in physical memory
Pointers are virtual addresses
Addresses are translated from virtual addresses to real physical addresses transparently
Therefore, memory is split into parts called “pages” Operating system can “map” pages
Operating system can maintain this mapping per process
→ different processes can use the same addresses, but “see” different things there
www.iaik.tugraz.at
Virtual Memory
Pointers are not addresses in physical memory
Pointers are virtual addresses
Addresses are translated from virtual addresses to real physical addresses transparently
Therefore, memory is split into parts called “pages” Operating system can “map” pages
Operating system can maintain this mapping per process
→ different processes can use the same addresses, but “see” different things there
www.iaik.tugraz.at
Virtual Memory
Pointers are not addresses in physical memory
Pointers are virtual addresses
Addresses are translated from virtual addresses to real physical addresses transparently
Therefore, memory is split into parts called “pages” Operating system can “map” pages
Operating system can maintain this mapping per process
→ different processes can use the same addresses, but “see” different things there
www.iaik.tugraz.at
Virtual Memory - It’s a map!
Pseudo code:
// in software:
int a = *(virt_address);
// in hardware:
std::map<virt*,phys*> addr_map;
auto fetch_memory(virt* virt_address) {
phys* phys_address = addr_map[virt_address]; return *phys_addr;
www.iaik.tugraz.at
Virtual Memory - It’s a map!
Pseudo code:
// in software:
int a = *(virt_address);
// in hardware:
std::map<virt*,phys*> addr_map;
auto fetch_memory(virt* virt_address) {
phys* phys_address = addr_map[virt_address]; return *phys_addr;
www.iaik.tugraz.at
A6 - Memory Layout and Demand Paging
Experiment with different kinds of variables, which addresses do they get?
Observe memory usage in practice, when does it really increase?
Write many small test programs (else you won’t find the right answers).
www.iaik.tugraz.at
Task Overview
Resources: FoodTrays, FoodCounters, Tables Actors: Employees and Customers
Customers acquire FoodTray, get food from the FoodCounter, and then eat at the table
Resources may only be used/cleaned/renewed by one actor at a time
www.iaik.tugraz.at
First Steps
Pull upstream
Trymakeand execute the program It will not work ;)
www.iaik.tugraz.at
A7 - Fast-Food Restaurant
main() Restaurant FoodCounter Table FoodTray Thread Customer Employeewww.iaik.tugraz.at
Classes to touch
main() Restaurant FoodCounter Table FoodTray Thread Customer Employeewww.iaik.tugraz.at
Classes?
Yes, but still it’s C ;)
www.iaik.tugraz.at
What do we need?
Locks: Semaphores Mutexes Condition Variableswww.iaik.tugraz.at
Semaphores
s e m _ t sem ;
// int s e m _ i n i t ( s e m _ t * sem , int pshared , // u n s i g n e d int v a l u e ); s e m _ i n i t (& sem , 0 , 1); // ... s e m _ w a i t (& sem ); // c r i t i c a l r e g i o n s e m _ p o s t (& sem ); // ... s e m _ d e s t r o y (& sem );
www.iaik.tugraz.at
Mutexes
p t h r e a d _ m u t e x _ t m u t e x ; // int p t h r e a d _ m u t e x _ i n i t ( p t h r e a d _ m u t e x _ t * // mutex , p t h r e a d _ m u t e x a t t r _ t * attr ); p t h r e a d _ m u t e x _ i n i t (& mutex , 0); // ... p t h r e a d _ m u t e x _ l o c k (& m u t e x ); // c r i t i c a l r e g i o n p t h r e a d _ m u t e x _ u n l o c k (& m u t e x ); // ... p t h r e a d _ m u t e x _ d e s t r o y (& m u t e x );www.iaik.tugraz.at
Condition Variables
p t h r e a d _ c o n d _ t cond ; // int p t h r e a d _ c o n d _ i n i t ( p t h r e a d _ c o n d _ t * // cond , p t h r e a d _ c o n d a t t r _ t * attr ); p t h r e a d _ c o n d _ i n i t (& cond , 0); // ... p t h r e a d _ m u t e x _ l o c k (& m u t e x ); // c r i t i c a l r e g i o np t h r e a d _ c o n d _ w a i t (& cond , & m u t e x );
// r e l e a s e m u t e x u n t i l c o n d i t i o n is true // s t i l l c r i t i c a l r e g i o n
p t h r e a d _ m u t e x _ u n l o c k (& m u t e x ); // ...
www.iaik.tugraz.at
Typical Errors
if ( p a r k i n g _ l o t . e m p t y ()) { p a r k i n g _ l o t . d r i v e I n ( m y _ c a r ); // go s h o p p i n g p a r k i n g _ l o t . l e a v e ( m y _ c a r ); }www.iaik.tugraz.at
Typical Errors
Something is not locked.
www.iaik.tugraz.at
Typical Errors
LOCK ( p a r k i n g _ l o t ); if ( p a r k i n g _ l o t . e m p t y ()) { U N L O C K ( p a r k i n g _ l o t ); p a r k i n g _ l o t . d r i v e I n ( m y _ c a r ); // go s h o p p i n g p a r k i n g _ l o t . l e a v e ( m y _ c a r ); } else U N L O C K ( p a r k i n g _ l o t );www.iaik.tugraz.at
Typical Errors
Correct locking at first glance, but ...
www.iaik.tugraz.at
Typical Errors - Thread 1
LOCK ( bike );
bike . d i s m o u n t (); // go s h o p p i n g bike . m o u n t (); U N L O C K ( bike );
www.iaik.tugraz.at
Typical Errors - Thread 2
bike . m o u n t (); // do s o m e t h i n g
www.iaik.tugraz.at
Typical Errors
Correct locking, but ...
... some other thread accessed the resource without
using the lock.
www.iaik.tugraz.at
Typical Errors - Thread 1
a c q u i r e A l l L o c k s I n T h e S y s t e m (); // do s o m e t h i n g
www.iaik.tugraz.at
Typical Errors - Thread 2
s o m e L o c k . a c q u i r e (); // wait a w h i l e // ...
www.iaik.tugraz.at
Typical Errors
Everything is locked. Several times. Just to be sure.
Will result in a fatal performance. Maybe nothing can
happen simultaneously because of the way it is
locked.
www.iaik.tugraz.at
Typical Errors
LOCK ( p a r k i n g _ l o t ); if ( p a r k i n g _ l o t . e m p t y ()) { p a r k i n g _ l o t . d r i v e I n ( m y _ c a r ); U N L O C K ( p a r k i n g _ l o t ); // go s h o p p i n g p a r k i n g _ l o t . l e a v e ( m y _ c a r ); } // no u n l o c k ...www.iaik.tugraz.at
Typical Errors
Not unlocking the critical region in some situations.
www.iaik.tugraz.at
Typical Errors - Thread 1
LOCK ( h a r d d i s k ); LOCK ( f l o p p y );
c o p y S o m e t h i n g ( floppy , h a r d d i s k ); U N L O C K ( f l o p p y );
www.iaik.tugraz.at
Typical Errors - Thread 2
LOCK ( f l o p p y ); LOCK ( h a r d d i s k );
c h e c k S o m e t h i n g ( floppy , h a r d d i s k ); U N L O C K ( h a r d d i s k );
www.iaik.tugraz.at
Typical Errors
www.iaik.tugraz.at