81
Microprocessor systems 3

3 Microprocessor systems

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Microprocessor systems

3

3

Memory

Memory management

– If only one process in memory?

– If more than one, do everyone get even

share?

– Do we share memory to partitions?

– Does the memory has to be continues for

a process?

4

Memory

Main memory – memory, where

currently used data is kept

Secondary memory – discs, tapes…

bigger, cheaper but slower

Secondary memory is about 10 6 slower

than main memory

5

Hierarchy

SRAM

SRAM

DRAM

SRAM

Hard disc

CD

DVD

Registers

L1 cache

L2 cache

Main memory

Secondary

memory

price speed Internal

memory

6

Memory management

Strategies

– fetch ( decided when to bring new program or data from secondary

memory to main memory)

• demand fetch ( only when data is requested, it is transferred to

main memory. Takes time when requested)

• anticipatory fetch ( it is predicted what data is needed and it is

brought into main memory before it is requested. Data that is not

needed may also be transferred – waste)

– placement ( where data is put in memory )

• First fit

• Best fit

• Worst fit

– replacement ( if memory is full, it has to be decided, what is going to

be replaced)

7

Logical vs physical address

Logical (virtual) address – generated by CPU

Physical address - memory address that is represented in the form of a binary number on the address bus circuitry in order to enable the data

bus to access a particular storage cell of main memory

Memory addresses bind during compiling and loading are the same

Binding during execution may differ

Memory management unit (MMU) deals with address generation

8

15.02.2016 11:45

9

Address generation

15.02.2016 11:33

CPU

MEMORY

Limit register Relocation

register

< +

Error in generation

Logical

address

Physical

address yes

no

10

What is the difference between

physical and logical memory address?

How to generate addresses with limit

and relocation register?

11

Answers

Logical (virtual) address is generated by

CPU. Physical address is real address in

physical memory (memory cell)

Limit register is used to check if request is

valid. Limit’s register base + offset is used

to get address.

13

Memory placement strategies

Multiprogramming system with

varying segments’ size it is possible to

choose where information is stored

Therefore, strategy is needed to decide

how to place data and program into

main memory

14

First fit

Job is placed in the first free slot in

main memory. Fast decision.

List of free memory

Start aadress

Size

a 16 MB

d MB 5

c MB 14

g 30 MB

13 MB memory

OS

16 MB slot

In use

MB slot 14

In use

MB slot 5

In use

MB slot 30

15

Best fit

System puts input data into slot in main

memory where smallest slot of memory is

left over – tries to achieve high

concentration. Takes time to find best slot

List of free memory

Start aadress

Size

a MB 16

d MB 5

c 14 MB

g 30 MB

13 MB memory

OS

16 MB slot

In use

14 MB slot

In use

5 MB slot

In use

30 MB slot

16

Worst fit

Input is placed in slot in memory

where largest slot is left over as a

result

Weird? But - > if we put memory into

big slot, it could be assumed that the

slot that is left over, will also be big

Takes time – suitable slot has to be

found

17

Worst fit

List of free memory

Start aadress

Size

a MB 16

d MB 5

c MB 14

g 30 MB

13 MB memory

OS

MB slot 16

In use

MB slot 14

In use

5 MB slot

In use

MB slot 30

18

Memory placement

FF and BF are „better“ than WF.

FF - could cause external

fragmentation

Memory management

2 strategies for placement :

– First fit

– Best fit

– Worst fit

According to method, end result or

step by step process has to be

described

21

Memory management

T – time (step)

Id – process id

– letters are used to mark those

– Or if – that means that process now frees

memory previously allocated

N – how much memory is required by

the process

Example

In the beginning: -- -- -- -- --

1 . step: a, n =1

now: a - -- -- -- --

. step: b, n =3 2

now: ab bb -- -- --

3 . step: - , a (free a)

Now: - b bb -- -- --

4 . step: d, n =3

Now: - b bb dd d - --

15 /02/2016 1:15 PM

23

Example

5 . step - ,b (free b)

Now -- -- dd d - --

. step f, n =2 6

Now -- -- dd df f -

7 . step g, n =3

Now gg g - dd df f -

8 . step free f

Now gg g - dd d - --

15 /02/2016 1:16 PM 22

Example

9 . step: free d

Now gg g - -- -- --

. step: j, n =1 10

Now gg gj -- -- --

11 . step: free g

Final answer: -- - j -- -- --

15 /02/2016 1:17 PM

25

Example

1 . step: a, n =3

Now: aa a - -- -- -

2 . step : b, n =2

Now: aa ab b - -- -

. step : c, n =1 3

Now: aa ab bc -- -

4 . step : a, -

Now: -- - b bc -- -

5 . step : e, n =2

Now: ee - b bc -- -

Example

6 . step : c, -

Now: ee - b - -- -

. step : g, n=1 ( 7 WF )

Now: ee - b b g -- -

8 . step : h, n =3

Now: ee - b bg hh h

9 . step : b, -

Now: ee -- - g hh h

27

Example

a a

1 . step: a, n =2

28

Example

a a

a a b b

2 . step: b, n =2

Copy previous row and add

two b - s

29

Example

a a

a a b b

b b

. step: a, 3 -

Copy previous row and

remove a - s

30

Example

a a

a a b b

b b

b b c

4 . step: c,n =1

Copy previous row.

Because BF method, then

best spot is found - >

therefore write c in 5.th cell

31

Example

a a

a a b b

b b

b b c

c

5 . step: b, -

Copy previous row.

Remove b - s.

And the table is full.

33

Virtual memory Everything else so far has had fixed size

If we need more memory -> virtual memory

- method, that allows to execute a process even if not fully in main memory

Logical memory=huge

Physical memory is much smaller

34

Programmer does not have to worry about memory: it is left for OS

35

Memory organization history

One user system

Physical memory for

multiprogramming systems

Virtual memory for multiprogramming systems

Physical memory Virtual memory

Fixed partitions

Variable partitions

Absolute addresses

Logical address

Pure paging

Pure segment

ation

Combination of

segmentation and paging

36

Addresses

Main memory Virtual memory

Address translator

37

Addresses

Solution -> blocks

Information is stored where every block in virtual memory is located in physical memory

If the block has fixed size -> it is called page and its management is called paging

38

If with varying size, then those are called segments and its management is segmentation

Page tables exercise

Goal: translating addresses

from virtual memory to physical

memory using page table

37

Virtuaalne mälu=Virtual memory

Füüsiline mälu= Physical memory

Example

Deal with one page at the time. Take first

page (page 0)

We need to find match for virtual memory

page 0 in physical memory

We use page table

Look at the table

Virtual memory page 0 is physical memory

page 4

We copy all data from virtual memory to

physical memory page 4 (in the same order)

Example

Example

Next page - > page 1 from virtual memory

Find match from the page table (page 2 in physical

memory)

Carry data over

Example

The same way, we will translate page 2 and 3 from

virtual to physical memory

Copy the data

Done

Example

But all is not

that easy..

Given page’s nr

and offset in

the page

Addresses have

to be translated

Sisu=data

Lk nr=Page nr

Nihe lk - s=Offset in a page

Example

Let’s take the first row:

Page nr is 2.

Lets use the page table to translate the page:

Page 2 corresponds to page 1

Every page has size 4

Therefore, the address:

r=p s *p+d => r=4*1+2=6

We write data to the physical memory to

location 6:

Example

Next row:

Page 3. Using page table, it

corresponds to page 6 in physical

memory

Translate the address: 6*4+0=24

We write data into slot 24:

Example

We continue until all data from virtual

memory has been „copied“ to physical

memory

Final result:

51

Paging exercise

15.02.2016 11:33

You have to assign how many slots of

memory process is going to get. There are 6 slots that has to be divided between 3 process (write how you

divide the memory into colorful boxes).

If there is no free spot in the memory,

something has to be replaced.

Rn means reading from n

Wn mean writing to n

Goal: minimize swamping

Cost=swamp in + swamp out

The extra table is for your own use – it

is not graded

52

Paging exercise

16/02/2016 11:52

53

Swamping

16.02.2016 10:50

OS

User’s space

Main memory

Process 1

Process 2

1 . Swamp out

2 . Swamp in

“Storage”

Paging

You have to decide how many slots of

memory every process is going to get

You try to minimize the cost (swamping)

There is no golden rule

Sometimes you just have to try multiply

options and find the best

Use the extra table for planning – it is not

graded but you can stimulate the processes

Hints

Look the commend: less different resources, less slots needed

Less writing -> less slots needed as with reading you do not have to do swamp out

Start with processes you can directly see how many pages they need. Then distribute what has been left

Example

1 st process: 3 resources

(1 ,2, 3)

2 nd process : also 3

resources(4,5,6)

3 rd process: 3 resources

(8 ,9, 7)

Nothing is visible from

that.

Therefore: try equally

giving everyone 2 slots

Example

Everyone gets 2

slots

Now we need to

calculate swamp ins

and swamp outs

Use the help table

Example

R1 W1 R2 W2 R3

After you have swamped page

in and changed it (write) you

just cannot delete it - > you have

to write changes back to storage

( swamp out )

R1 – 1 slot of memory

W1 – exists in memory already

R2 – need to swamp in

W2 – exists in memory - > mark

it has been changed

R3. Needs to be brought in but

no space. Have to replace

something. Bot has been

changed - one swamp out >

anyway. Does not matter as last

order

Example

R5 W5 R6 R4 R5

Only one writing. Good. Less

swamp outs needed as we will

replace slots where data has not

been changed

R5: swamp in

W5: mark it has been changed

R6: swamp in

R4: Swamp in needed but no

free space. Something has to be

replaced. 6 has not been

changed and is not needed

later - > replace it

R5: 5 is already in memory. NO

swamp in needed

Example

R8 W8 R9 R7 R8

Only one writing

R8: swamp in in 8

W8: mark that changed

R9: swamp 9 in

R7: Memory full. 8 has been

changed, 9 has not. Replace 9

R8: 8 already in memory

End result: 9 swamp in, 1

swamp out and cost is 10

Now we should try other

distributions (give process 1 3

slots to avoid swamp out?) but

therefore we would take away

slot from other process and that

would result need for swamp

out - we cannot win by this. >

It is the most optimal solution

Example

9 resources but..

1 st process - > only reading. No

need for more than 1 slot. No

swamps out anyway

nd process 2 - > one writing. 2

slots should be ok

3 rd process - > two writing. We

have 3 slots left. So we give all

of it to 3rd process.

Fill the table

9 swamps in is minimum (it

cannot be less than nr of

different resources)

Most optimal solution

Example

What if we would have chosen 2

slots per process?

We would get one swamp out

due to process 3: R7 needs to

bring in (swamp in) 7 but both

slots full and both changed.

Example

First: divide slots equally

Swamp out with first process

We could get rid of it?

Let’s try other ways how to

divide the slots by

considering process’ types

Example

2 nd and 3rd process have

writing in the end – > we could

give them less slots (after

writing, no need to replace

them)

2 nd process: it can manage with

one slot as when file is brought

in, it is only needed in

sequential order

3 rd could manage also with 1

page (without any swamp outs)

but we also would like to

minimize swamp ins. If 2 slots,

then optimal (no double swamp

ins)

1 st process writes twice and

then reads third resource

Inode

Data structure in Unix - like file

systems

Holds base information about fails,

folders and file system:

– UID, GID, file type, last access time,

modification time, size, access rights,

links to data parts…

15.02.2016 11:33 63

Inode

16.02.2016 11:10 64

file1 2231

file2 4571

file3 33312

Folder Inode table

2231

4571

33312

Data block

Inode task Data blocks and inode table is given

List of files to be read in given

For answer, you have to give list of data blocks read in correct order

– File that was requested first, is read first

– Data blocks with smaller ids are read before other data blocks

Goal:

– Understand how inode Works and why it is useful

Inode exercise

Given: files in the folder and data blocks. You have to find the

sequence how the data blocks are read when the files are

requested in the following order: file2:file3:file1:file4 . Data blocks with lower ID are read first. For answer, write down data

block IDs in the reading order separating them with „ : “. For example: „1001:1002:1003“

Kaust=folder

Andmeblokk=data block

Fail=file

Inode exercise

We need to read the files in following

order:

file2:file3:file1:file4

Let’s start with fail 2.

We can see from folder table that it

corresponds to inode 104. it means that from inode table cell 104 we will find

links to data blocks that make up file 2.

From the graphic, we see that there is only one arrow from inode table slot

104: brown arrow to data block 1006. Therefore we know that file 2 consist

of one data block and it is with ID 1006.

Done first fist file.

Current answer: „1006“

Inode exercise

We need to read the files in following

order:

file2:file3:file1:file4

Next file: file 3. Folder table gives us corresponding inode: 101. From inode

table we see it has three arrows (blue).

It means file 3 consists of 3 data blocks

But how to read them? The task description says: smaller ID’s first.

Therefore we read 1002 first, then continue with data block 1005 and 1008 is the last one.

Current answer: „1006:1002:1005:1008“

Doing the same with next two files, the final answer will be:

1006:1002:1005:1008:1004:1003:1007:1009:1010

Stack

Data type that Works in LIFO

principle

Two main operations

– Push – Move SP. Puts data into slot

where SP points.

– Pop – data from where SP points is

removed. SP is moved.

SP=stack pointer

Stack

Where stack is used?

– In many everyday situations (not only

OS)

Stack exercise

SP

First pop() return 0A and SP now points to 4th slot

Second pop() return B2 and points to slot 3

Third pop() returns AC and points to 2nd slot

Answer: AC

Stack exercise

Grey area in memory is needed for program. Rest can be used. We want to

call f() multiply time and it takes 32 blocks on memory. How much free

memory do we have? Knowing that, we will divide free memory with f()’s size

and full part of the answers shows us how many times we can call the

program

(224 - 53)=171, 171/32=5,343 … - > therefore answer is 5

Memory is given where there is stack at the end of the memory and program's code is loaded at the start. It holds 53 slots of memory. Pointer points to last used slot of memory

Stack exercise

Stack is given. SP location is known. We have three commands:

After pop(): SP points to slot 8

After 2. pop(): SP points to slot 6

After push: SP=SP+1, 7. slot. Data FC is put there.

SP