• No results found

Systemnahe Programmierung KU

N/A
N/A
Protected

Academic year: 2021

Share "Systemnahe Programmierung KU"

Copied!
45
0
0

Loading.... (view fulltext now)

Full text

(1)

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

Daniel Gruß, IAIK, Graz University of Technology

(2)

www.iaik.tugraz.at

1. Virtual Memory

(3)

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

(4)

www.iaik.tugraz.at

What is a pointer?

Address in memory? Index in RAM?

(5)

www.iaik.tugraz.at

What is a pointer?

Address in memory

? Index in RAM?

(6)

www.iaik.tugraz.at

What is a pointer?

Address in memory? Index in RAM

(7)

www.iaik.tugraz.at

What is a pointer?

Address in memory? Index in RAM?

(8)

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?

(9)

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?

(10)

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?

(11)

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?

(12)

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

(13)

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

(14)

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

(15)

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

(16)

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

(17)

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;

(18)

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;

(19)

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).

(20)

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

(21)

www.iaik.tugraz.at

First Steps

Pull upstream

Trymakeand execute the program It will not work ;)

(22)

www.iaik.tugraz.at

A7 - Fast-Food Restaurant

main() Restaurant FoodCounter Table FoodTray Thread Customer Employee

(23)

www.iaik.tugraz.at

Classes to touch

main() Restaurant FoodCounter Table FoodTray Thread Customer Employee

(24)

www.iaik.tugraz.at

Classes?

Yes, but still it’s C ;)

(25)

www.iaik.tugraz.at

What do we need?

Locks: Semaphores Mutexes Condition Variables

(26)

www.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 );

(27)

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 );

(28)

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 n

p 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 ); // ...

(29)

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 ); }

(30)

www.iaik.tugraz.at

Typical Errors

Something is not locked.

(31)

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 );

(32)

www.iaik.tugraz.at

Typical Errors

Correct locking at first glance, but ...

(33)

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 );

(34)

www.iaik.tugraz.at

Typical Errors - Thread 2

bike . m o u n t (); // do s o m e t h i n g

(35)

www.iaik.tugraz.at

Typical Errors

Correct locking, but ...

... some other thread accessed the resource without

using the lock.

(36)

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

(37)

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 // ...

(38)

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.

(39)

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 ...

(40)

www.iaik.tugraz.at

Typical Errors

Not unlocking the critical region in some situations.

(41)

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 );

(42)

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 );

(43)

www.iaik.tugraz.at

Typical Errors

(44)

www.iaik.tugraz.at

Typical Errors

void s e t U p P a g i n g () { LOCK ( p a g e D i r e c t o r y ); i n s e r t P a g e T a b l e (); i n s e r t P a g e (); f i l l P a g e W i t h D a t a (); U N L O C K ( p a g e D i r e c t o r y ); } // in some o t h e r file (!): void i n s e r t P a g e T a b l e () { LOCK ( p a g e D i r e c t o r y ); // do the a c t u a l work U N L O C K ( p a g e D i r e c t o r y ); }

(45)

References

Related documents

○ If BP elevated, think primary aldosteronism, Cushing’s, renal artery stenosis, ○ If BP normal, think hypomagnesemia, severe hypoK, Bartter’s, NaHCO3,

The study was aim to investigate the effects of continuous PCBs exposure on optomotor response (OMR) and retinal photoreceptor cell development-related gene expression in

The market pull model is born with the investigation of the needs market, pass through the responsible department for the research and development (R&amp;D) that studies, analyses

The lift to drag ratio increases as the angle of attack increased on both wings, for rear wing the lift to drag ratio is reduced when compared to that of front wing due to

is marked with a combination voltage rating only, such as 480Y/277 V ac, provided a 3-pole breaker intended only for use in a single-phase multi-wire circuit includes in its

Procurando comprender el porqué de la elección de la carrera docente —el «querer ser» docente— encontramos una vía de abordaje entre los estudios sobre la cuestión de

In this cross-sectional analysis, we also showed that plasma hepcidin in patients with NTDT was more closely related to markers of iron turnover (TfSat and NTBI) than to iron

Therefore, players B must decide whether to return zero or half without knowing how much their player A counterpart transferred to them in the …rst stage.. Four sessions in total