104
Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主主主 主主主

Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Embed Size (px)

Citation preview

Page 1: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Operating Systems PrinciplesMemory Management

Lecture 9: Sharing of Code and Data in Main Memory

主講人:虞台文

Page 2: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Content Single-Copy Sharing

– Reasons of Sharing – Requirements for Sharing

Static Linking and Sharing – Sharing in Systems w/o Segmentation or Paging – Sharing in Paging Systems – Sharing in Segmented Systems

Dynamic Linking and Sharing Principles of Distributed Shared Memory (DSM)

– The User's View of DSM Implementations of DSM

– Implementing Unstructured DSM – Implementing Structured DSM

Page 3: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Operating Systems PrinciplesMemory Management

Lecture 9: Sharing of Code and Data in Main Memory

Single-Copy Sharing

Page 4: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing

Reusing Software Modules– Individual software modules are constructed

separately.– Develop applications by linking with other well-

developed software modules.– Reduce software developing cost.– Each process owns private copy of shared objects

Single-Copy Sharing– Processes share a single copy of code or data in

memory– Why?– What?– How?

Page 5: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Why? Processes need to access common data, e.g.,

– Communication btw producer & consumer– Cooperation among divide-and-conquer processes– Competition on resources

Better utilization of memory (code & data)– Several active processes use the same code or data at the

same time, e.g., many users running the same editor or debugger in a time-sharing system

– Without sharing, memory requirement would increase dramatically and, thus,

reduce the number of login users. increase I/O overhead to load excess copies increase the page fault rate and, thus, the risk of thrashing.

Page 6: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

What?

OS kernel routines – I/O drivers– System services, e.g.,

memory management and file manipulation routines.

– System utilities, e.g., Compiler, linker, loader and debugger.

User-lever applications– A single copy of the same code applies to

different data sets.

Page 7: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

How?

How to express what is shared?– System Components

Designated at the time of system design or initialization– User-Level Applications

Shared code must be reentrant (read-only, “pure”)– Stack and heap must be replicated per process

Unix Win32

shmget()

CreateFileMapping()

Shmat() OpenFileMapping(), MapViewofFile()

Shmdt() OpenFileMapping(), UnmapViewofFile(), CloseHandle()

Shmctl()

Page 8: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

CompileCompile

Page 9: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

Page 10: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

...esp

Page 11: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

5

...esp

Page 12: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

57

...esp

Page 13: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

57

...

esp7

Page 14: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

57

...

esp

7

5

Page 15: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

57

ReturnAddress

...

esp

7

5

ReturnAddress

Page 16: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

esp

57

7

5

ReturnAddress

Page 17: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

esp

57

7

5

ReturnAddress

oldebp

Page 18: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

esp

57

7

5

ReturnAddress

oldebp

ebp

ebp+8

ebp+12i

j

Page 19: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

57

7

5

ReturnAddress

oldebp

ebp

ebp+8

ebp+12i

j

eax=5

Page 20: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

57

7

5

ReturnAddress

oldebp

ebp

ebp+8

ebp+12i

j

eax=12

Page 21: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

57

7

5

ReturnAddress

oldebp

ebp

ebp+8

ebp+12i

j

eax=24

Page 22: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

57

7

5

ReturnAddress

oldebp

ebp

ebp+8

ebp+12i

j

eax=24

esp

Page 23: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

57

7

5

ReturnAddress

oldebp

eax=24

esp

Page 24: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

_AddMul2:push ebpmov ebp,espmov eax, [bp+8]add eax, [bp+8+4]shl eax, 1mov esp,ebppop ebpret

...

57

7

5

ReturnAddress

eax=24

esp

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

ReturnAddress

Page 25: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

...

57

7

5

eax=24

esp

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

Page 26: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

int x, y, z;

int AddMul2(int i, int j){ return (i+j)*2;}

main(){ x=5; y=7; z=AddMul2(x, y); . . . . . . . .}

...

57

eax=24

esp

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

_main:mov ds:[x],5mov ds:[y],7push ds:[y]push ds:[x]call _AddMul2add esp,8mov ds:[z],eax. . . . . .

24

Page 27: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

_STACK SEGMENT PUBLIC ‘STACK’DB 4096 dup(?)

Bottom:_STACK ENDS

_DATA SEGMENT PULBIC ‘DATA’x dd 1 dup(?)y dd 1 dup(?)z dd 1 dup(?)_DATA ENDS

_TEXT SEGMENT PUBLIC ‘CODE’_AddMul2:

. . . // code for AddMul2_main:

. . . // code for main_TEXT ENDS

Pure code is sharable

Each process has its own stack segment

Each process has its own data segment

Page 28: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Example: Simple Code Sharing

Translate to the same physical process for different processes

Access different data areas for different processes

Page 29: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Linking and Sharing

Sharing are closely related to linking– Linking resolves external references– Sharing links to the same module

Static linking/sharing:– Resolve references before execution starts

Dynamic linking/sharing:– Resolve references while executing

Page 30: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Operating Systems PrinciplesMemory Management

Lecture 9: Sharing of Code and Data in Main Memory

Static Linking and Sharing

Page 31: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing without Virtual Memory

PhysicalMemory

PhysicalMemory

SystemComponents

UserPrograms

With one or no Relocation Register (RR)– Sharing user programs:

Possible only by partial overlapping Too restrictive and difficult; generally not used

– Sharing system components: Agree on a starting positions Linker resolves references to those locations Can also use a block of “transfer addresses,”

but this involves additional memory references. Issues difficult to identify the invoking

processes by system components.

All memory of a process is contiguous physically.

UserProgram 1

UserProgram 2

Page 32: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing without Virtual Memory

PhysicalMemory

PhysicalMemory

SystemComponents

UserPrograms

With one or no Relocation Register (RR)– Sharing user programs:

Possible only by partial overlapping Too restrictive and difficult; generally not used

– Sharing system components: Agree on a starting positions Linker resolves references to those locations Can also use a block of “transfer addresses,”

but this involves additional memory references. Issues difficult to identify the invoking

processes by system components.

All memory of a process is contiguous physically.

Page 33: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing without Virtual Memory

With multiple RR’s

CBR = Code Base Reg. Point to shared copy of code

SBR = Stack Base Reg. Point to private copy of stack

DBR = Data Base Reg. Point to private copy of data

Sharing of code

Page 34: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing without Virtual Memory

With multiple RR’s

CBR = Code Base Reg. Point to private copy of code

SBR = Stack Base Reg. Point to private copy of stack

DBR = Data Base Reg. Point to shared copy of data

Sharing of data

code1

code2

stack1

stack2

data

CBR1

SBR1

DBR1

CBR2

SBR2

DBR2

process1 process2

Page 35: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Data

DataData

DatabaseDatabase

Common data(without address)

Page 36: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Data

DataDataData1

Data2

Data3

Data2

Data3

Data1

DatabaseDatabasePhysicalMemory

Page 37: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Data

Data2

Data3

Data1

PhysicalMemory

...

...

PT10

n1

...

...

PT20

n2

n2, w

DatabaseDatabase

n1, w

Page 38: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Data

Data2

Data3

Data1

PhysicalMemory

...

...

PT10

n1

...

...

PT20

n2

n2, w

DatabaseDatabase

n1, w

The page numbers of sharing processes can be different.

The page numbers of sharing processes can be different.

Page 39: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

bra label1

label1:

bra label1

label1:

...

...

...

Sharing in Paging Systems Sharing of Code

0x0000

0x2000

0x1000

4k

4k

4k

w

bra (2,w)

label1:

bra (2,w)

label1:

...

...

...

0x0000

0x2000

0x1000

AssembleAssemble

Page 40: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Code

bra (2,w)

label1:

bra (2,w)

label1:

...

...

...

0x0000

0x2000

0x1000

bra (n+2,w)

label1:

...

...

...

Virtual Memory

nth page

Shared code

Page 41: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Code

bra (n+2,w)

label1:

...

...

...

Virtual Memory

nth page

bra (n+2,w)

...

...

Label1:

...

Physical Memory

p

q

r

Page 42: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Code

bra (n+2,w)

label1:

...

...

...

Virtual Memory

nth page

bra (n+2,w)

...

...

Label1:

...

Physical Memory

rr

qq

pp

nn+1n+2

PT

p

q

r

Page 43: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Code

bra (n+2,w)

...

...

Label1:

...

Physical Memory

n2, w

n1, w

rr

qq

pp

n1n1+1n1+2

PT1

00

rr

qq

pp

n2n2+1n2+2

PT2

0 0

rr

qq

pp

nn+1n+2

PT

p

q

r

Page 44: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Code

bra (n+2,w)

...

...

Label1:

...

Physical Memory

n2, w

n1, w

rr

qq

pp

n1n1+1n1+2

PT1

00

rr

qq

pp

n2n2+1n2+2

PT2

0 0

rr

qq

pp

nn+1n+2

PT

p

q

r

If absolute virtual address is used for coding, such a code sharing scheme is workable if

n1= n2 = n

If absolute virtual address is used for coding, such a code sharing scheme is workable if

n1= n2 = n

Page 45: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Code

bra (n+2,w)

...

...

Label1:

...

Physical Memory

n2, w

n1, w

rr

qq

pp

n1n1+1n1+2

PT1

00

rr

qq

pp

n2n2+1n2+2

PT2

0 0

rr

qq

pp

nn+1n+2

PT

p

q

r

Can we assign different

starting page numbers to a

different processes?

Can we assign different

starting page numbers to a

different processes?

Page 46: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Sharing of Code

bra (n+2,w)

label1:

...

...

...

Virtual Memory

nth page

1. Shared code is self-contained

Can we assign different

starting page numbers to a

different processes?

Can we assign different

starting page numbers to a

different processes?

2. Avoid using absolute address (page number) in share code, i.e.,

using address relative to CBR instead.

2. Avoid using absolute address (page number) in share code, i.e.,

using address relative to CBR instead.

Yes, if …

Page 47: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Short Summary

PT entries of different processes point to the same page frame

Data pages: No Restrictions Code pages:

– Must have the same page numbers in all PTs.– For generalization, avoid using page numbers in s

hared code (self-contained), i.e., using address relative to CBR instead.

Page 48: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Paging Systems Short Summary

PT entries of different processes point to the same page frame

Data pages: No Restrictions Code pages:

– Must have the same page numbers in all PTs. How to know the page numbers of shared components?Solutions:1. The total set of shared modules is known a priori.2. Resolved by an effective loader

done just-in-time and only once.

Page 49: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Dynamic Linking via Transfer Vector

...bri tv[i]

...bri tv[i]

...

Transfer Vectors

Currently Executing

Code

Linking just-in-time and only once.

call the same shared function by indirect branch instruction.

...

...

stubstub

stub

stub

01

i

n1

Page 50: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Dynamic Linking via Transfer Vector

...bri tv[i]

...bri tv[i]

...

Transfer Vectors

Currently Executing

Code

Linking just-in-time and only once.

...

...

stubstub

stub

stub

01

i

n1

When the shared function is called first time, the external reference is resolved by the corresponding stub.

Page 51: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Dynamic Linking via Transfer Vector

...bri tv[i]

...bri tv[i]

...

Transfer Vectors

Currently Executing

Code

Linking just-in-time and only once.

...

...

stubstub

stub

stub

01

i

n1

When the shared function is called first time, the external reference is resolved by the corresponding stub.

Shared code

When the external reference is resolved, the stub replaces the corresponding transfer vector to point to the shared code.

Page 52: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Dynamic Linking via Transfer Vector

...bri tv[i]

...bri tv[i]

...

Transfer Vectors

Currently Executing

Code

Linking just-in-time and only once.

...

...

stubstub

stub

stub

01

i

n1

Shared code

Once the external reference is resolved, it can be used directly later on.

Used by Win32 and POSIX (DLLs).

Page 53: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Segmented Systems

Much the same as with paged systems– Using segments instead of pages

Actually, simpler and more elegant because segments represent logical program entities.

Page 54: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Segmented Systems Sharing of Data

DatabaseDatabasePhysicalMemory

SharedData

...

...

ST10

s1

...

...

ST20

s2

s2, w

s1, w

ST entries of different processes point to the same segment in PM.

Page 55: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Segmented Systems Sharing of Data

DatabaseDatabasePhysicalMemory

SharedData

...

...

ST10

s1

...

...

ST20

s2

s2, w

s1, w

ST entries of different processes point to the same segment in PM.

How about if a segment is

also paged?How about if a segment is

also paged?

Page 56: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

PhysicalMemory

Sharing in Segmented Systems Sharing of Data

...

...

ST10

s1

...

...

ST20

s2

s2, p, w

s1, p, w

ST entries of different processes point to the same page table in PM.

Data2

Data3

Data1

p

PT

Paging

How about if a segment is

also paged?How about if a segment is

also paged?

Page 57: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

?

Sharing in Segmented Systems Sharing of Code

PhysicalMemory

SharedCode

...

...

ST10

s1

...

...

ST20

s2

s2, w

s1, w

ST entries of different processes point to the same segment in PM.

In what condition the scheme is workable?

The code must be self-

contained.

Page 58: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

?

Sharing in Segmented Systems Sharing of Code

PhysicalMemory

SharedCode

...

...

ST10

s1

...

...

ST20

s2

s2, w

s1, w

ST entries of different processes point to the same segment in PM.

How about if a segment is

also paged?How about if a segment is

also paged?

In what condition the scheme is workable?

The code must be self-

contained.

Page 59: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

? In what condition the scheme is workable?Physical

Memory

Sharing in Segmented Systems Sharing of Code

...

...

ST10

s1

...

...

ST20

s2

s2, p, w

s1, p, w

ST entries of different processes point to the same page table in PM.

Code2

Code3

Code1

p

PT

Paging

How about if a segment is

also paged?How about if a segment is

also paged?

The code must be self-

contained.

Page 60: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

? In what condition the scheme is workable?Physical

Memory

Sharing in Segmented Systems Sharing of Code

...

...

ST10

s1

...

...

ST20

s2

s2, p, w

s1, p, w

ST entries of different processes point to the same page table in PM.

Code2

Code3

Code1

p

PT

How about if the shared code is not

self-contained?How about if the shared code is not

self-contained?

The code must be self-

contained.

Assign the same segment numbers for all share codes in STs.

Assign the same segment numbers for all share codes in STs.

Page 61: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Sharing in Segmented Systems Summary

Much the same as with Paged Systems Actually, simpler and more elegant because

Segments represent logical program entities ST entries of different processes point to the

same segment in physical memory (PM) Data pages: No restrictions Code pages:

– Assign same segment numbers in all STs, or– Use base registers:

Function call loads CBR Self-references have the form w(CBR) Other references have the form (s,w)

Page 62: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Operating Systems PrinciplesMemory Management

Lecture 9: Sharing of Code and Data in Main Memory

Dynamic Linking and Sharing

Page 63: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Unrestricted Dynamic Linking/Sharing

Segmentation facilitates to implement a fully general scheme of dynamic linking and sharing.– Any two processes (user or system) can share any

portion of their space.

Pioneered in the MULTICS operating system.

Page 64: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Unrestricted Dynamic Linking/Sharing

i

j

Segment table

Code segment C

load * l d

(S, W)

Linkage section for C

CBRtrap on

Symboltable

dLBR

Page 65: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Unrestricted Dynamic Linking/Sharing

i

j

Segment table

Code segment C

load * l d

(S, W)

Linkage section for C

CBRtrap on

Symboltable

d

Addressing relative to linkage

section

Addressing relative to linkage

section

Displacement

Displacement

Indirect addressin

g

Indirect addressin

g

The address for external reference

The address for external reference

Not ready now

Not ready now

LBR

Page 66: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Unrestricted Dynamic Linking/Sharing

i

j

Segment table

Code segment C

load * l d

(S, W)

Linkage section for C

CBRtrap on

Symboltable

d

Generated by the compiler

Generated by the compiler

LBR

Page 67: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Unrestricted Dynamic Linking/Sharing

i

j

Segment table

Code segment C

load * l d

(S, W)

Linkage section for C

CBRtrap on

Symboltable

d

Resolve the external reference dynamically by the exception handler when the corresponding instruction is executed.

LBR

Page 68: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Unrestricted Dynamic Linking/Sharing

i

j

Segment table

Code segment C

load * l d

(S, W)

Linkage section for C

CBRtrap on

Symboltable

d

Resolve the external reference dynamically by the exception handler when the corresponding instruction is executed.

LBR

Segment S

w

trap off (s, w)

s

Page 69: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Unrestricted Dynamic Linking/Sharing

i

j

Segment table

i

j

Segment table

Code segment CCode segment C

load * l dload * l d

(S, W)

Linkage section f or CLinkage section f or C

CBRCBRtrap ontrap on

Symboltable

ddLBRLBR

Segment SSegment S

www

trap off (s, w)

s

i

j

Segment table

i

j

Segment table

Code segment CCode segment C

load * l dload * l d

(S, W)

Linkage section f or CLinkage section f or C

CBRCBRtrap ontrap on

Symboltable

ddLBRLBR

Before external reference is executed

After external reference is executed

Page 70: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Operating Systems PrinciplesMemory Management

Lecture 9: Sharing of Code and Data in Main Memory

Principles of Distributed Shared Memory (DSM)

Page 71: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Memory Sharing onDifferent Computer Architecture

MemoryMemory MemoryMemory

MemoryMemory

MemoryMemory

MemoryMemory

Single processor/Single memory module

Multiple processor/Single memory module

DistributedSystem

Page 72: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Distributed Share Memory (DSM)

MemoryMemory

MemoryMemory

MemoryMemory

DistributedSystem

VM– Creates the illusion of a

memory that is larger than the available physical memory

DSM– Creates the illusion of a

single shared memory

The illusion

of a single shared

memory

The illusion

of a single shared

memory

Page 73: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Distributed Share Memory (DSM)

MemoryMemory

MemoryMemory

MemoryMemory

DistributedSystem

Goal of DSM– To alleviate the burden of

programmer by hiding the fact that physical memory is distributed

DSM– Creates the illusion of a

single shared memory

The illusion

of a single shared

memory

The illusion

of a single shared

memory

Page 74: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Distributed Share Memory (DSM)

MemoryMemory

MemoryMemory

MemoryMemory

DistributedSystem

Goal of DSM– To alleviate the burden of

programmer by hiding the fact that physical memory is distributed

DSM– Creates the illusion of a

single shared memory

The illusion

of a single shared

memory

The illusion

of a single shared

memory

Great overhead on message-passing to resolve remote memory access.

To make DSM viable, efficiency on data transfer is an important consideration.

Great overhead on message-passing to resolve remote memory access.

To make DSM viable, efficiency on data transfer is an important consideration.

Page 75: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

How to implement transfers efficiently?

Two approaches: Optimize the implementation

– Exploiting locality of reference or using data replication

– Most important with Unstructured DSM

Restrict the full generality of the DSM – Exploiting what the user knows

– Basic to Structured DSM

Page 76: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Unstructured DSM

P1P1

P2P2

PnPn

MM1MM1

MM2MM2

MMnMMn

0

p10

p1

0

p1

DSMDSM

0

np1

Simulate single, fully shared, unstructured memory.

Advantage: Fully “transparent” to user

Disadvantage: Efficiency

Page 77: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Structured DSM

P1P1

P2P2

PnPn

MM1MM1

MM2MM2

MMnMMn

Local address space

shared

shared

shared

DSMDSM

Share only portion of memory, e.g., a collection of functions and shared variables, determined by user.

For efficiency, add restrictions on use of shared variables:

– Access only within (explicitly declared) Critical Sections

Variant: “object-based DSM”– Use “objects” instead of

shared variables:

Page 78: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Operating Systems PrinciplesMemory Management

Lecture 9: Sharing of Code and Data in Main Memory

Implementations of DSM

Page 79: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementations of DSM

Implementing Unstructured DSM– Granularity of data transfers– Replication of data– Memory consistency– Tracking data

Implementing Structured DSM– Critical-section based– Object based

Page 80: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Granularity of Data Transfers

Transfer too little:– Time wasted in latency (startup

cost)

Transfer too much:– Time wasted in transfer– False sharing

A nature choice is to use the page size (or its multiple) as the granularity for transfer.

Page 81: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

What action should be taken when a page fault? Two possible choices:

– Move the page from the remote to the requesting processor

Problems: Heavy network traffic, trashing, and delay– Make a copy of the page from the remote to the

requesting processor Advantages: decrease network traffic, less trashing, and

reduce delay Issue: How to maintain memory consistency?

Read work fine no action neededWrites require others to update or invalidate.

Page 82: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Rules to maintain memory consistency

Allowing only one copy of writable page, but multiple copies of read-only pages.

Page 83: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

page B(read-only)

page B(read-only)

DSM

page A(writable)

page B(read-only)

P1P1

P2P2

Page 84: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

page B(read-only)

page B(read-only)

DSM

page A(writable)

page B(read-only)

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

read

Page 85: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

page B(read-only)

page B(read-only)

DSM

page A(writable)

page B(read-only)

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

read

Operation is done locally. No extra action need to be

taken.

Operation is done locally. No extra action need to be

taken.

Page 86: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

page B(read-only)

page B(read-only)

DSM

page A(writable)

page B(read-only)

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

write

Page 87: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

page B(read-only)

page B(read-only)

DSM

page A(writable)

page B(read-only)

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

write

Operation is done locally. No extra action need to be

taken.

Operation is done locally. No extra action need to be

taken.

Page 88: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

page B(read-only)

page B(read-only)

DSM

page A(writable)

page B(read-only)

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

write

Page 89: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

page B(read-only)

page B(read-only)

DSM

page A(writable)

page B(read-only)

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

write

Invalidate copy in MM2;Upgrade copy in MM1 to

writable.

Invalidate copy in MM2;Upgrade copy in MM1 to

writable.

page B(writable)

page B(writable)

Page 90: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

page B(read-only)

DSM

page A(writable)

page B(read-only)

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

read

page B(writable)

page B(writable)

Page 91: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

page A(writable)

DSM

page A(writable)

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

read

page B(writable)

page B(writable)

Downgrade page in MM1 to read-only;

Make copy in MM2.

Downgrade page in MM1 to read-only;

Make copy in MM2.

page A(read-only)page A

(read-only)

page A(read-only)

Page 92: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

DSM

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

writ

e

page B(writable)

page B(writable)

page A(read-only)page A

(read-only)

page A(read-only)

Page 93: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

DSM

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

writ

e

page B(writable)

page B(writable)

page A(read-only)page A

(read-only)

page A(read-only)

page B(writable)Transfer page from MM1 to MM2.Transfer page from MM1 to MM2.

Page 94: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Replication of Data

Allowing only one copy of writable page, but multiple copies of read-only pages.

Example: MM1

MM2

DSM

P1P1

P2P2

P1 reads A

P1 writes A

P1 writes B

P2 reads A

P2 writes B

page B(writable)

page A(read-only)page A

(read-only)

page A(read-only)

page B(writable)

Page 95: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Memory Consistency

a1=1, b1=2a2=1, b2=2

a1=1, b1=2a2=0, b2=0a1=1, b1=2a2=0, b2=1a1=1, b1=2a2=1, b2=2

Page 96: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Memory Consistency

a1=1, b1=2a2=1, b2=2

a1=1, b1=2a2=0, b2=0a1=1, b1=2a2=0, b2=1a1=1, b1=2a2=1, b2=2

Strict Consistency

Sequential Consistency

Page 97: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Memory Consistency

Strict Consistency:– Reading a variable x returns the value

written to x by the most recently executed write operation.

Sequential Consistency:– Sequence of values of x read by different

processes corresponds to some sequential interleaved execution of those processes.

Page 98: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Tracking Data

Tracking Data: Where is it stored now? Approaches:

– Have “owner” track it by maintaining “copy set” .Only owner is allowed to write.Ownership can change.To find the owner using broadcast.

– Central Manager → BottleneckMultiple “replicated” managers split the responsibilities.

– “Probable owner” gets tracked down e.g., via page table.Retrace data’s migration.Update links traversed to show current owner.

Page 99: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Unstructured DSM Discussion

All variables in the shared space are assumed consistent all the time.– Moving and/or invalidating pages may be needed on

write

Much network traffic can be generated, resulting in poor performance.

Solution:– Structured DSM requires a new model of memory

consistency

Page 100: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Structured DSM Consistencies

Weak Consistency (Dubois et al. 1988)– Consistency by requesting synchronization explicitly.

Release Consistency (Gharachorloo 1990)– Consistency upon leaving a CS

Entry Consistency (Bershad 1993)– Consistency upon entering a CS

Page 101: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Structured DSM Weak Consistency

Introduce “synchronization variable,” S Processes access it when they are ready to

adjust/reconcile their shared variables.

Page 102: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Structured DSM Release Consistency

Synchronize upon leaving CS

– A waste if p2 never looks at x.

Page 103: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Implementing Structured DSM Entry Consistency

Before entering CS, import only those variables used

There is also a “lazy release consistency” (Keleher et al. 1992) which imports all shared variables before entering CS.

Page 104: Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文

Object-Based DSM

An object encapsulates data and methods.

Can use remote method invocation(like remote procedure calls, covered earlier)instead of copying or moving an object intolocal memory.

One can move an object to improve performance.