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,A7 www.iaik.tugraz.at
1. Virtual Memory 2. A7 - Fast-Food Restaurant 2
Course Overview A8, A9 System Programming A7 Thread Interaction and Synchronization A4, A5, A6 Multithreading, Processes, Virtual Memory A1, A2, A3 Git, Compiler, C, C++ 3
What is a pointer? 4
What is a pointer? Address in memory 4
What is a pointer? Address in memory? Index in RAM 4
What is a pointer? Address in memory? Index in RAM? 4
Segmentation fault 5
Segmentation fault Common scenario: User program accesses invalid memory location 5
Segmentation fault Common scenario: User program accesses invalid memory location The OS invokes a fault handler This fault handler can abort the user program or fix the situation by making the address valid Efficient: Don t assign memory until necessary 5
Segmentation fault Common scenario: User program accesses invalid memory location The OS invokes a fault handler This fault handler can abort the user program or fix the situation by making the address valid Efficient: Don t assign memory until necessary Are pointers addresses in physical memory? How can addresses in memory be invalid? 5
Virtual Memory 6
Virtual Memory Pointers are not addresses in physical memory 6
Virtual Memory Pointers are not addresses in physical memory Pointers are virtual addresses Addresses are translated from virtual addresses to real physical addresses transparently 6
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 6
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 6
Virtual Memory - It s a map! 7
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; } 7
A6 - Memory Layout and Demand Paging www.iaik.tugraz.at 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). Answer questions from the test system questionnaire! 8
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 9
First Steps Pull upstream Try make and execute the program It will not work ;) Fix it! 10
A7 - Fast-Food Restaurant main() Restaurant Thread FoodCounter Customer Table Employee FoodTray 11
Classes to touch main() Restaurant Thread FoodCounter Customer Table Employee FoodTray 12
Classes? Yes, but still it s C ;) You will be able to run it on SWEB! 13
What do we need? Locks: Semaphores Mutexes Condition Variables 14
Semaphores sem_t sem ; // int sem_init ( sem_t * sem, int pshared, // unsigned int value ); sem_init (& sem, 0, 1); //... sem_wait (& sem ); // critical region sem_post (& sem ); //... sem_destroy (& sem ); 15
Mutexes pthread_mutex_t mutex ; // int pthread_mutex_init ( pthread_mutex_t * // mutex, pthread_mutexattr_t * attr ); pthread_mutex_init (& mutex, 0); //... pthread_mutex_lock (& mutex ); // critical region pthread_mutex_unlock (& mutex ); //... pthread_mutex_destroy (& mutex ); 16
Condition Variables pthread_cond_t cond ; // int pthread_cond_init ( pthread_cond_t * // cond, pthread_condattr_t * attr ); pthread_cond_init (& cond, 0); //... pthread_mutex_lock (& mutex ); // critical region pthread_cond_wait (& cond, & mutex ); // release mutex until condition is true // still critical region pthread_mutex_unlock (& mutex ); //... pthread_cond_destroy (& cond ); 17
Typical Errors if ( parking_lot. empty ()) { parking_lot. drivein ( my_car ); // go shopping parking_lot. leave ( my_car ); } 18
Typical Errors Something is not locked. Random behaviour, race conditions, strange crashes. 19
Typical Errors LOCK ( parking_lot ); if ( parking_lot. empty ()) { UNLOCK ( parking_lot ); parking_lot. drivein ( my_car ); // go shopping parking_lot. leave ( my_car ); } else UNLOCK ( parking_lot ); 20
Typical Errors Correct locking at first glance, but...... actually the critical part is not locked. 21
Typical Errors - Thread 1 LOCK ( bike ); bike. dismount (); // go shopping bike. mount (); UNLOCK ( bike ); 22
Typical Errors - Thread 2 bike. mount (); // do something 23
Typical Errors Correct locking, but...... some other thread accessed the resource without using the lock. 24
Typical Errors - Thread 1 acquir ealllocksi nthesystem (); // do something releas ealllocksi nthesystem (); 25
Typical Errors - Thread 2 somelock. acquire (); // wait a while //... 26
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. 27
Typical Errors LOCK ( parking_lot ); if ( parking_lot. empty ()) { parking_lot. drivein ( my_car ); UNLOCK ( parking_lot ); // go shopping parking_lot. leave ( my_car ); } // no unlock... 28
Typical Errors Not unlocking the critical region in some situations. Other threads will wait forever. 29
Typical Errors - Thread 1 LOCK ( harddisk ); LOCK ( floppy ); copysomething ( floppy, harddisk ); UNLOCK ( floppy ); UNLOCK ( harddisk ); 30
Typical Errors - Thread 2 LOCK ( floppy ); LOCK ( harddisk ); checksomething ( floppy, harddisk ); UNLOCK ( harddisk ); UNLOCK ( floppy ); 31
Typical Errors Deadlock. 32
Typical Errors void setuppaging () { LOCK ( pagedirectory ); insertpagetable (); insertpage (); fillpagewithdata (); UNLOCK ( pagedirectory ); } 33 // in some other file (!): void insertpagetable () { LOCK ( pagedirectory ); // do the actual work UNLOCK ( pagedirectory ); }
Thanks for your attention!