Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文
Content <ul><li>Single-Copy Sharing  </li></ul><ul><ul><li>Reasons of Sharing  </li></ul></ul><ul><ul><li>Requirements for...
Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Single-Copy Sharing
Sharing <ul><li>Reusing  Software Modules </li></ul><ul><ul><li>Individual software modules are  constructed separately . ...
Why? <ul><li>Processes need to  access  common  data , e.g., </li></ul><ul><ul><li>Communication  btw producer & consumer ...
What? <ul><li>OS  kernel   routines   </li></ul><ul><ul><li>I/O  drivers </li></ul></ul><ul><ul><li>System  services , e.g...
How? <ul><li>How to  express  what is shared? </li></ul><ul><ul><li>System Components </li></ul></ul><ul><ul><ul><li>Desig...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing _STACK SEGMENT PUBLIC ‘STACK’ DB 4096 dup(?) Bottom: _STACK ENDS _DATA SEGMENT PULBIC ‘DATA’ ...
Example: Simple Code Sharing Translate to the same physical process for different processes Access different data areas fo...
Linking and Sharing <ul><li>Sharing  are closely related to  linking </li></ul><ul><ul><li>Linking resolves  external   re...
Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Static Linking and Shari...
Sharing without Virtual Memory <ul><li>With  one  or  no   Relocation Register  (RR) </li></ul><ul><ul><li>Sharing  user p...
Sharing without Virtual Memory <ul><li>With  one  or  no   Relocation Register  (RR) </li></ul><ul><ul><li>Sharing  user p...
Sharing without Virtual Memory <ul><li>With  multiple  RR’s </li></ul><ul><li>CBR  =  Code  Base Reg. </li></ul><ul><li>  ...
Sharing without Virtual Memory <ul><li>With  multiple  RR’s </li></ul><ul><li>CBR  =  Code  Base Reg. </li></ul><ul><li>  ...
Sharing in Paging Systems   Sharing of Data Data Common data (without address) Database
Sharing in Paging Systems   Sharing of Data Data Data 1 Data 2 Data 3 Data 2 Data 3 Data 1 Database Physical Memory
Sharing in Paging Systems   Sharing of Data Data 2 Data 3 Data 1 n 2 ,  w n 1 ,  w Physical Memory . . . . . . PT 1 0 n 1...
Sharing in Paging Systems   Sharing of Data Data 2 Data 3 Data 1 . . . . . . PT 1 0 n 1 . . . . . . PT 2 0 n 2 n 2 ,  w n...
Sharing in Paging Systems   Sharing of Code Assemble bra  label1 label1 :  . . . . . . . . . 0x0000 0x2000 0x1000 4 k 4 k...
Sharing in Paging Systems   Sharing of Code Virtual Memory n th   page Shared code bra  (2,w) label1 :  . . . . . . . . ....
Sharing in Paging Systems   Sharing of Code Virtual Memory n th   page Physical Memory p q r bra  (n+2,w) label1 :  . . ....
Sharing in Paging Systems   Sharing of Code Virtual Memory n th   page Physical Memory p q r bra  (n+2,w) label1 :  . . ....
Sharing in Paging Systems   Sharing of Code n 2 ,  w n 1 ,  w p q r bra  (n+2,w) . . . . . . Label1 :  . . . Physical Mem...
Sharing in Paging Systems   Sharing of Code n 2 ,  w n 1 ,  w p q r If  absolute  virtual address is used for coding, suc...
Sharing in Paging Systems   Sharing of Code n 2 ,  w n 1 ,  w r q p n1 n1+1 n1+2 PT 1 0 0 r q p n2 n2+1 n2+2 PT 2 0 0 r q...
Sharing in Paging Systems   Sharing of Code Virtual Memory n th   page 1. Shared code is  self-contained Can we assign di...
Sharing in Paging Systems   Short Summary <ul><li>PT entries of  different   processes  point to the  same   page frame <...
Sharing in Paging Systems   Short Summary <ul><li>PT entries of  different   processes  point to the  same   page frame <...
Dynamic Linking via Transfer Vector ... bri tv[i]  ... bri tv[i]  ... Transfer  Vectors  Currently  Executing  Code  Linki...
Dynamic Linking via Transfer Vector ... bri tv[i]  ... bri tv[i]  ... Transfer  Vectors  Currently  Executing  Code  Linki...
Dynamic Linking via Transfer Vector ... bri tv[i]  ... bri tv[i]  ... Transfer  Vectors  Currently  Executing  Code  Linki...
Dynamic Linking via Transfer Vector ... bri tv[i]  ... bri tv[i]  ... Transfer  Vectors  Currently  Executing  Code  Linki...
Sharing in Segmented Systems <ul><li>Much  the  same  as with  paged systems </li></ul><ul><ul><li>Using segments instead ...
Sharing in Segmented Systems   Sharing of Data Shared Data s 2 ,  w s 1 ,  w ST entries of different processes point to t...
Sharing in Segmented Systems   Sharing of Data Shared Data s 2 ,  w s 1 ,  w ST entries of different processes point to t...
Sharing in Segmented Systems   Sharing of Data s 2 ,  p ,  w s 1 ,  p ,  w ST entries of different processes point to the...
Sharing in Segmented Systems   Sharing of Code ? Shared Code s 2 ,  w s 1 ,  w ST entries of different processes point to...
Sharing in Segmented Systems   Sharing of Code ? Shared Code s 2 ,  w s 1 ,  w ST entries of different processes point to...
Sharing in Segmented Systems   Sharing of Code ? In  what condition  the scheme is  workable ? s 2 ,  p ,  w s 1 ,  p ,  ...
Sharing in Segmented Systems   Sharing of Code ? In  what condition  the scheme is  workable ? s 2 ,  p ,  w s 1 ,  p ,  ...
Sharing in Segmented Systems   Summary <ul><li>Much  the  same  as with  Paged Systems </li></ul><ul><li>Actually,  simpl...
Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Dynamic Linking and Shar...
Unrestricted Dynamic Linking/Sharing <ul><li>Segmentation  facilitates to implement a  fully general scheme  of  dynamic l...
Unrestricted Dynamic Linking/Sharing Symbol table i j Segment table Code segment C load * l d ( S ,  W ) Linkage section f...
Unrestricted Dynamic Linking/Sharing ( S ,  W ) trap on Symbol table Addressing relative to linkage section Displacement I...
Unrestricted Dynamic Linking/Sharing ( S ,  W ) trap on Symbol table Generated by the compiler i j Segment table Code segm...
Unrestricted Dynamic Linking/Sharing ( S ,  W ) Symbol table Resolve the external reference  dynamically  by the  exceptio...
Unrestricted Dynamic Linking/Sharing ( S ,  W ) Symbol table Resolve the external reference  dynamically  by the  exceptio...
Unrestricted Dynamic Linking/Sharing Before external reference is executed After external reference is executed
Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Principles of Distribute...
Memory Sharing on Different Computer Architecture Single processor/ Single memory module Multiple processor/ Single memory...
Distributed Share Memory (DSM) <ul><li>VM </li></ul><ul><ul><li>Creates the illusion of a memory that is  larger  than  th...
Distributed Share Memory (DSM) <ul><li>Goal of DSM </li></ul><ul><ul><li>To alleviate the burden of programmer by  hiding ...
Distributed Share Memory (DSM) <ul><li>Goal of DSM </li></ul><ul><ul><li>To alleviate the burden of programmer by  hiding ...
How to implement transfers efficiently? <ul><li>Two  approaches: </li></ul><ul><li>Optimize  the  implementation </li></ul...
Unstructured DSM <ul><li>Simulate  single ,  fully shared ,  unstructured  memory. </li></ul><ul><li>Advantage:  </li></ul...
Structured DSM <ul><li>Share only  portion  of memory, e.g., a collection of  functions  and  shared variables , determine...
Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Implementations of DSM
Implementations of DSM <ul><li>Implementing  Unstructured  DSM </li></ul><ul><ul><li>Granularity  of data transfers </li><...
Implementing Unstructured DSM     Granularity  of Data Transfers <ul><li>Transfer too  little : </li></ul><ul><ul><li>Tim...
Implementing Unstructured DSM     Replication  of Data <ul><li>What action should be taken when a  page fault ? </li></ul...
Implementing Unstructured DSM     Replication  of Data <ul><li>Rules to maintain  memory consistency   </li></ul>Allowing...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Replication  of Data Allowing only  one  copy of  writable  page, but  multiple  copies...
Implementing Unstructured DSM     Memory Consistency a1=1, b1=2 a2=1, b2=2 a1=1, b1=2 a2=0, b2=0 a1=1, b1=2 a2=0, b2=1 a1...
Implementing Unstructured DSM     Memory Consistency a1=1, b1=2 a2=1, b2=2 a1=1, b1=2 a2=0, b2=0 a1=1, b1=2 a2=0, b2=1 a1...
Implementing Unstructured DSM     Memory Consistency <ul><li>Strict  Consistency: </li></ul><ul><ul><li>Reading a variabl...
Implementing Unstructured DSM     Tracking Data <ul><li>Tracking Data: Where is it stored now? </li></ul><ul><li>Approach...
Implementing Unstructured DSM     Discussion <ul><li>All  variables  in the shared space are assumed  consistent  all the...
Implementing Structured DSM     Consistencies <ul><li>Weak  Consistency (Dubois et al. 1988) </li></ul><ul><ul><li>Consis...
Implementing Structured DSM     Weak Consistency  <ul><li>Introduce “ synchronization variable ,”  S </li></ul><ul><li>Pr...
Implementing Structured DSM     Release Consistency  <ul><li>Synchronize upon  leaving  CS </li></ul><ul><ul><li>A  waste...
Implementing Structured DSM     Entry Consistency  <ul><li>Before  entering  CS, import  only  those variables used </li>...
Object-Based DSM <ul><li>An object encapsulates  data  and  methods . </li></ul><ul><li>Can use  remote method invocation ...
Upcoming SlideShare
Loading in …5
×

Os9 2

452 views

Published on

  • Be the first to comment

  • Be the first to like this

Os9 2

  1. 1. Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory 主講人:虞台文
  2. 2. Content <ul><li>Single-Copy Sharing </li></ul><ul><ul><li>Reasons of Sharing </li></ul></ul><ul><ul><li>Requirements for Sharing </li></ul></ul><ul><li>Static Linking and Sharing </li></ul><ul><ul><li>Sharing in Systems w/o Segmentation or Paging </li></ul></ul><ul><ul><li>Sharing in Paging Systems </li></ul></ul><ul><ul><li>Sharing in Segmented Systems </li></ul></ul><ul><li>Dynamic Linking and Sharing </li></ul><ul><li>Principles of Distributed Shared Memory (DSM) </li></ul><ul><ul><li>The User's View of DSM </li></ul></ul><ul><li>Implementations of DSM </li></ul><ul><ul><li>Implementing Unstructured DSM </li></ul></ul><ul><ul><li>Implementing Structured DSM </li></ul></ul>
  3. 3. Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Single-Copy Sharing
  4. 4. Sharing <ul><li>Reusing Software Modules </li></ul><ul><ul><li>Individual software modules are constructed separately . </li></ul></ul><ul><ul><li>Develop applications by linking with other well-developed software modules . </li></ul></ul><ul><ul><li>Reduce software developing cost . </li></ul></ul><ul><ul><li>Each process owns private copy of shared objects </li></ul></ul><ul><li>Single-Copy Sharing </li></ul><ul><ul><li>Processes share a single copy of code or data in memory </li></ul></ul><ul><ul><li>Why ? </li></ul></ul><ul><ul><li>What ? </li></ul></ul><ul><ul><li>How ? </li></ul></ul>
  5. 5. Why? <ul><li>Processes need to access common data , e.g., </li></ul><ul><ul><li>Communication btw producer & consumer </li></ul></ul><ul><ul><li>Cooperation among divide-and-conquer processes </li></ul></ul><ul><ul><li>Competition on resources </li></ul></ul><ul><li>Better utilization of memory (code & data) </li></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>Without sharing, </li></ul></ul><ul><ul><ul><li>memory requirement would increase dramatically and, thus, reduce the number of login users . </li></ul></ul></ul><ul><ul><ul><li>increase I/O overhead to load excess copies </li></ul></ul></ul><ul><ul><ul><li>increase the page fault rate and, thus, the risk of thrashing . </li></ul></ul></ul>
  6. 6. What? <ul><li>OS kernel routines </li></ul><ul><ul><li>I/O drivers </li></ul></ul><ul><ul><li>System services , e.g., </li></ul></ul><ul><ul><ul><li>memory management and file manipulation routines. </li></ul></ul></ul><ul><ul><li>System utilities , e.g., </li></ul></ul><ul><ul><ul><li>Compiler , linker , loader and debugger . </li></ul></ul></ul><ul><li>User-lever applications </li></ul><ul><ul><li>A single copy of the same code applies to different data sets . </li></ul></ul>
  7. 7. How? <ul><li>How to express what is shared? </li></ul><ul><ul><li>System Components </li></ul></ul><ul><ul><ul><li>Designated at the time of system design or initialization </li></ul></ul></ul><ul><ul><li>User-Level Applications </li></ul></ul><ul><li>Shared code must be reentrant (read-only, “pure”) </li></ul><ul><ul><li>Stack and heap must be replicated per process </li></ul></ul>OpenFileMapping(), UnmapViewofFile (), CloseHandle() Shmdt () Shmctl () OpenFileMapping (), MapViewofFile () Shmat () CreateFileMapping () shmget () Win32 Unix
  8. 8. 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); . . . . . . . . } Compile
  9. 9. 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); . . . . . . . . } _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . .
  10. 10. 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); . . . . . . . . } _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . . . . esp
  11. 11. 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); . . . . . . . . } _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . 5 . . . esp
  12. 12. 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); . . . . . . . . } _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . 5 7 . . . esp
  13. 13. 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); . . . . . . . . } _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . 5 7 . . . esp 7
  14. 14. 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); . . . . . . . . } _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . 5 7 . . . esp 7 5
  15. 15. 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); . . . . . . . . } _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . 5 7 . . . Return Address esp 7 5 Return Address
  16. 16. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 esp 7 5 Return Address
  17. 17. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 esp 7 5 Return Address old ebp
  18. 18. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 i j esp 7 5 Return Address old ebp ebp ebp+8 ebp+12
  19. 19. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 i j eax= 5 7 5 Return Address old ebp ebp ebp+8 ebp+12
  20. 20. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 i j eax= 12 7 5 Return Address old ebp ebp ebp+8 ebp+12
  21. 21. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 i j eax= 24 7 5 Return Address old ebp ebp ebp+8 ebp+12
  22. 22. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 i j eax= 24 7 5 Return Address old ebp ebp ebp+8 ebp+12 esp
  23. 23. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 eax= 24 7 5 Return Address old ebp esp
  24. 24. 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); . . . . . . . . } _AddMul2: push ebp mov ebp,esp mov eax, [bp+8] add eax, [bp+8+4] shl eax, 1 mov esp,ebp pop ebp ret . . . 5 7 eax= 24 _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . 7 5 Return Address esp Return Address
  25. 25. 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); . . . . . . . . } . . . 5 7 eax= 24 _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . 7 5 esp
  26. 26. 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); . . . . . . . . } . . . 5 7 eax= 24 _main: mov ds:[x],5 mov ds:[y],7 push ds:[y] push ds:[x] call _AddMul2 add esp,8 mov ds:[z],eax . . . . . . 24 esp
  27. 27. 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
  28. 28. Example: Simple Code Sharing Translate to the same physical process for different processes Access different data areas for different processes
  29. 29. Linking and Sharing <ul><li>Sharing are closely related to linking </li></ul><ul><ul><li>Linking resolves external references </li></ul></ul><ul><ul><li>Sharing links to the same module </li></ul></ul><ul><li>Static linking/sharing: </li></ul><ul><ul><li>Resolve references before execution starts </li></ul></ul><ul><li>Dynamic linking/sharing: </li></ul><ul><ul><li>Resolve references while executing </li></ul></ul>
  30. 30. Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Static Linking and Sharing
  31. 31. Sharing without Virtual Memory <ul><li>With one or no Relocation Register (RR) </li></ul><ul><ul><li>Sharing user programs : </li></ul></ul><ul><ul><ul><li>Possible only by partial overlapping </li></ul></ul></ul><ul><ul><ul><li>Too restrictive and difficult ; generally not used </li></ul></ul></ul><ul><ul><li>Sharing system components : </li></ul></ul><ul><ul><ul><li>Agree on a starting positions </li></ul></ul></ul><ul><ul><ul><li>Linker resolves references to those locations </li></ul></ul></ul><ul><ul><ul><li>Can also use a block of “ transfer addresses ,” but this involves additional memory references. </li></ul></ul></ul><ul><ul><ul><li>Issues  difficult to identify the invoking processes by system components. </li></ul></ul></ul>Physical Memory System Components User Programs All memory of a process is contiguous physically. User Program 1 User Program 2
  32. 32. Sharing without Virtual Memory <ul><li>With one or no Relocation Register (RR) </li></ul><ul><ul><li>Sharing user programs : </li></ul></ul><ul><ul><ul><li>Possible only by partial overlapping </li></ul></ul></ul><ul><ul><ul><li>Too restrictive and difficult ; generally not used </li></ul></ul></ul><ul><ul><li>Sharing system components : </li></ul></ul><ul><ul><ul><li>Agree on a starting positions </li></ul></ul></ul><ul><ul><ul><li>Linker resolves references to those locations </li></ul></ul></ul><ul><ul><ul><li>Can also use a block of “ transfer addresses ,” but this involves additional memory references. </li></ul></ul></ul><ul><ul><ul><li>Issues  difficult to identify the invoking processes by system components. </li></ul></ul></ul>Physical Memory System Components User Programs All memory of a process is contiguous physically.
  33. 33. Sharing without Virtual Memory <ul><li>With multiple RR’s </li></ul><ul><li>CBR = Code Base Reg. </li></ul><ul><li> Point to shared copy of code </li></ul><ul><li>SBR = Stack Base Reg. </li></ul><ul><li> Point to private copy of stack </li></ul><ul><li>DBR = Data Base Reg. </li></ul><ul><li> Point to private copy of data </li></ul>Sharing of code
  34. 34. Sharing without Virtual Memory <ul><li>With multiple RR’s </li></ul><ul><li>CBR = Code Base Reg. </li></ul><ul><li> Point to private copy of code </li></ul><ul><li>SBR = Stack Base Reg. </li></ul><ul><li> Point to private copy of stack </li></ul><ul><li>DBR = Data Base Reg. </li></ul><ul><li> Point to shared copy of data </li></ul>Sharing of data code 1 code 2    stack 1 stack 2    data          CBR 1 SBR 1 DBR 1    CBR 2 SBR 2 DBR 2 process 1 process 2
  35. 35. Sharing in Paging Systems  Sharing of Data Data Common data (without address) Database
  36. 36. Sharing in Paging Systems  Sharing of Data Data Data 1 Data 2 Data 3 Data 2 Data 3 Data 1 Database Physical Memory
  37. 37. Sharing in Paging Systems  Sharing of Data Data 2 Data 3 Data 1 n 2 , w n 1 , w Physical Memory . . . . . . PT 1 0 n 1 . . . . . . PT 2 0 n 2 Database
  38. 38. Sharing in Paging Systems  Sharing of Data Data 2 Data 3 Data 1 . . . . . . PT 1 0 n 1 . . . . . . PT 2 0 n 2 n 2 , w n 1 , w The page numbers of sharing processes can be different. Physical Memory Database
  39. 39. Sharing in Paging Systems  Sharing of Code Assemble bra label1 label1 : . . . . . . . . . 0x0000 0x2000 0x1000 4 k 4 k 4 k w bra (2,w) label1 : . . . . . . . . . 0x0000 0x2000 0x1000
  40. 40. Sharing in Paging Systems  Sharing of Code Virtual Memory n th page Shared code bra (2,w) label1 : . . . . . . . . . 0x0000 0x2000 0x1000 bra (n+2,w) label1 : . . . . . . . . .
  41. 41. Sharing in Paging Systems  Sharing of Code Virtual Memory n th page Physical Memory p q r bra (n+2,w) label1 : . . . . . . . . . bra (n+2,w) . . . . . . Label1 : . . .
  42. 42. Sharing in Paging Systems  Sharing of Code Virtual Memory n th page Physical Memory p q r bra (n+2,w) label1 : . . . . . . . . . bra (n+2,w) . . . . . . Label1 : . . . r q p n n+1 n+2 PT
  43. 43. Sharing in Paging Systems  Sharing of Code n 2 , w n 1 , w p q r bra (n+2,w) . . . . . . Label1 : . . . Physical Memory r q p n1 n1+1 n1+2 PT 1 0 0 r q p n2 n2+1 n2+2 PT 2 0 0 r q p n n+1 n+2 PT
  44. 44. Sharing in Paging Systems  Sharing of Code n 2 , w n 1 , w p q r If absolute virtual address is used for coding, such a code sharing scheme is workable if n 1 = n 2 = n bra (n+2,w) . . . . . . Label1 : . . . Physical Memory r q p n1 n1+1 n1+2 PT 1 0 0 r q p n2 n2+1 n2+2 PT 2 0 0 r q p n n+1 n+2 PT
  45. 45. Sharing in Paging Systems  Sharing of Code n 2 , w n 1 , w r q p n1 n1+1 n1+2 PT 1 0 0 r q p n2 n2+1 n2+2 PT 2 0 0 r q p n n+1 n+2 PT p q r Can we assign different starting page numbers to a different processes? bra (n+2,w) . . . . . . Label1 : . . . Physical Memory
  46. 46. Sharing in Paging Systems  Sharing of Code Virtual Memory n th page 1. Shared code is self-contained 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 . Yes, if … bra (n+2,w) label1 : . . . . . . . . .
  47. 47. Sharing in Paging Systems  Short Summary <ul><li>PT entries of different processes point to the same page frame </li></ul><ul><li>Data pages: No Restrictions </li></ul><ul><li>Code pages: </li></ul><ul><ul><li>Must have the same page numbers in all PTs . </li></ul></ul><ul><ul><li>For generalization , avoid using page numbers in shared code ( self-contained ), i.e., using address relative to CBR instead. </li></ul></ul>
  48. 48. Sharing in Paging Systems  Short Summary <ul><li>PT entries of different processes point to the same page frame </li></ul><ul><li>Data pages: No Restrictions </li></ul><ul><li>Code pages: </li></ul><ul><ul><li>Must have the same page numbers in all PTs . </li></ul></ul><ul><ul><ul><li>How to know the page numbers of shared components? </li></ul></ul></ul><ul><ul><ul><li>Solutions : </li></ul></ul></ul><ul><ul><ul><li>The total set of shared modules is known a priori . </li></ul></ul></ul><ul><ul><ul><li>Resolved by an effective loader  </li></ul></ul></ul>done just-in-time and only once .
  49. 49. 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. ... ... stub stub stub stub 0 1 i n  1
  50. 50. Dynamic Linking via Transfer Vector ... bri tv[i] ... bri tv[i] ... Transfer Vectors Currently Executing Code Linking just-in-time and only once . ... ... stub stub stub stub 0 1 i n  1 When the shared function is called first time , the external reference is resolved by the corresponding stub .
  51. 51. Dynamic Linking via Transfer Vector ... bri tv[i] ... bri tv[i] ... Transfer Vectors Currently Executing Code Linking just-in-time and only once . ... ... stub stub stub stub 0 1 i n  1 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.
  52. 52. Dynamic Linking via Transfer Vector ... bri tv[i] ... bri tv[i] ... Transfer Vectors Currently Executing Code Linking just-in-time and only once . ... ... stub stub stub stub 0 1 i n  1 Shared code Once the external reference is resolved, it can be used directly later on . Used by Win32 and POSIX ( DLLs ).
  53. 53. Sharing in Segmented Systems <ul><li>Much the same as with paged systems </li></ul><ul><ul><li>Using segments instead of pages </li></ul></ul><ul><li>Actually , simpler and more elegant because segments represent logical program entities. </li></ul>
  54. 54. Sharing in Segmented Systems  Sharing of Data Shared Data s 2 , w s 1 , w ST entries of different processes point to the same segment in PM. Database Physical Memory . . . . . . ST 1 0 s 1 . . . . . . ST 2 0 s 2
  55. 55. Sharing in Segmented Systems  Sharing of Data Shared Data s 2 , w s 1 , w ST entries of different processes point to the same segment in PM. How about if a segment is also paged? Database Physical Memory . . . . . . ST 1 0 s 1 . . . . . . ST 2 0 s 2
  56. 56. Sharing in Segmented Systems  Sharing of Data s 2 , p , w s 1 , p , w ST entries of different processes point to the same page table in PM. Data 2 Data 3 Data 1 PT How about if a segment is also paged? Physical Memory . . . . . . ST 1 0 s 1 . . . . . . ST 2 0 s 2 p Paging
  57. 57. Sharing in Segmented Systems  Sharing of Code ? Shared Code s 2 , w s 1 , 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. Physical Memory . . . . . . ST 1 0 s 1 . . . . . . ST 2 0 s 2
  58. 58. Sharing in Segmented Systems  Sharing of Code ? Shared Code s 2 , w s 1 , w ST entries of different processes point to the same segment in PM. How about if a segment is also paged? In what condition the scheme is workable ? The code must be self-contained. Physical Memory . . . . . . ST 1 0 s 1 . . . . . . ST 2 0 s 2
  59. 59. Sharing in Segmented Systems  Sharing of Code ? In what condition the scheme is workable ? s 2 , p , w s 1 , p , w ST entries of different processes point to the same page table in PM. Code 2 Code 3 Code 1 PT How about if a segment is also paged? The code must be self-contained. Physical Memory . . . . . . ST 1 0 s 1 . . . . . . ST 2 0 s 2 p Paging
  60. 60. Sharing in Segmented Systems  Sharing of Code ? In what condition the scheme is workable ? s 2 , p , w s 1 , p , w ST entries of different processes point to the same page table in PM. Code 2 Code 3 Code 1 PT 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. Physical Memory . . . . . . ST 1 0 s 1 . . . . . . ST 2 0 s 2 p
  61. 61. Sharing in Segmented Systems  Summary <ul><li>Much the same as with Paged Systems </li></ul><ul><li>Actually, simpler and more elegant because Segments represent logical program entities </li></ul><ul><li>ST entries of different processes point to the same segment in physical memory (PM) </li></ul><ul><li>Data pages: No restrictions </li></ul><ul><li>Code pages: </li></ul><ul><ul><li>Assign same segment numbers in all STs, or </li></ul></ul><ul><ul><li>Use base registers: </li></ul></ul><ul><ul><ul><li>Function call loads CBR </li></ul></ul></ul><ul><ul><ul><li>Self-references have the form w(CBR) </li></ul></ul></ul><ul><ul><ul><li>Other references have the form (s,w) </li></ul></ul></ul>
  62. 62. Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Dynamic Linking and Sharing
  63. 63. Unrestricted Dynamic Linking/Sharing <ul><li>Segmentation facilitates to implement a fully general scheme of dynamic linking and sharing . </li></ul><ul><ul><li>Any two processes (user or system) can share any portion of their space. </li></ul></ul><ul><li>Pioneered in the MULTICS operating system. </li></ul>
  64. 64. Unrestricted Dynamic Linking/Sharing Symbol table i j Segment table Code segment C load * l d ( S , W ) Linkage section for C CBR trap on d LBR
  65. 65. Unrestricted Dynamic Linking/Sharing ( S , W ) trap on Symbol table Addressing relative to linkage section Displacement Indirect addressing The address for external reference Not ready now i j Segment table Code segment C load * l d Linkage section for C CBR d LBR
  66. 66. Unrestricted Dynamic Linking/Sharing ( S , W ) trap on Symbol table Generated by the compiler i j Segment table Code segment C load * l d Linkage section for C CBR d LBR
  67. 67. Unrestricted Dynamic Linking/Sharing ( S , W ) Symbol table Resolve the external reference dynamically by the exception handler when the corresponding instruction is executed . i j Segment table Code segment C load * l d Linkage section for C CBR trap on d LBR
  68. 68. Unrestricted Dynamic Linking/Sharing ( S , W ) Symbol table Resolve the external reference dynamically by the exception handler when the corresponding instruction is executed . trap off ( s , w ) s i j Segment table Code segment C load * l d Linkage section for C CBR trap on d LBR Segment S w
  69. 69. Unrestricted Dynamic Linking/Sharing Before external reference is executed After external reference is executed
  70. 70. Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Principles of Distributed Shared Memory (DSM)
  71. 71. Memory Sharing on Different Computer Architecture Single processor/ Single memory module Multiple processor/ Single memory module Distributed System Memory Memory Memory Memory Memory
  72. 72. Distributed Share Memory (DSM) <ul><li>VM </li></ul><ul><ul><li>Creates the illusion of a memory that is larger than the available physical memory </li></ul></ul><ul><li>DSM </li></ul><ul><ul><li>Creates the illusion of a single shared memory </li></ul></ul>Memory Memory Memory Distributed System The illusion of a single shared memory
  73. 73. Distributed Share Memory (DSM) <ul><li>Goal of DSM </li></ul><ul><ul><li>To alleviate the burden of programmer by hiding the fact that physical memory is distributed </li></ul></ul><ul><li>DSM </li></ul><ul><ul><li>Creates the illusion of a single shared memory </li></ul></ul>Memory Memory Memory Distributed System The illusion of a single shared memory
  74. 74. Distributed Share Memory (DSM) <ul><li>Goal of DSM </li></ul><ul><ul><li>To alleviate the burden of programmer by hiding the fact that physical memory is distributed </li></ul></ul><ul><li>DSM </li></ul><ul><ul><li>Creates the illusion of a single shared memory </li></ul></ul>Memory Memory Memory Distributed System The illusion of a single shared memory <ul><li>Great overhead on message-passing to resolve remote memory access. </li></ul><ul><li>To make DSM viable , efficiency on data transfer is an important consideration. </li></ul>
  75. 75. How to implement transfers efficiently? <ul><li>Two approaches: </li></ul><ul><li>Optimize the implementation </li></ul><ul><ul><li>Exploiting locality of reference or using data replication </li></ul></ul><ul><ul><li>Most important with Unstructured DSM </li></ul></ul><ul><li>Restrict the full generality of the DSM </li></ul><ul><ul><li>Exploiting what the user knows </li></ul></ul><ul><ul><li>Basic to Structured DSM </li></ul></ul>
  76. 76. Unstructured DSM <ul><li>Simulate single , fully shared , unstructured memory. </li></ul><ul><li>Advantage: </li></ul><ul><li>Fully “ transparent ” to user </li></ul><ul><li>Disadvantage: Efficiency </li></ul>P1 P2 P n MM1 MM2 MM n 0 p  1 0 p  1 0 p  1 DSM 0 np  1
  77. 77. Structured DSM <ul><li>Share only portion of memory, e.g., a collection of functions and shared variables , determined by user . </li></ul><ul><li>For efficiency , add restrictions on use of shared variables: </li></ul><ul><ul><li>Access only within (explicitly declared) Critical Sections </li></ul></ul><ul><li>Variant: “ object-based DSM ” </li></ul><ul><ul><li>Use “objects” instead of shared variables: </li></ul></ul>P1 P2 P n Local address space shared shared shared MM1 MM2 MM n DSM
  78. 78. Operating Systems Principles Memory Management Lecture 9: Sharing of Code and Data in Main Memory Implementations of DSM
  79. 79. Implementations of DSM <ul><li>Implementing Unstructured DSM </li></ul><ul><ul><li>Granularity of data transfers </li></ul></ul><ul><ul><li>Replication of data </li></ul></ul><ul><ul><li>Memory consistency </li></ul></ul><ul><ul><li>Tracking data </li></ul></ul><ul><li>Implementing Structured DSM </li></ul><ul><ul><li>Critical-section based </li></ul></ul><ul><ul><li>Object based </li></ul></ul>
  80. 80. Implementing Unstructured DSM  Granularity of Data Transfers <ul><li>Transfer too little : </li></ul><ul><ul><li>Time wasted in latency (startup cost) </li></ul></ul><ul><li>Transfer too much : </li></ul><ul><ul><li>Time wasted in transfer </li></ul></ul><ul><ul><li>False sharing </li></ul></ul>A nature choice is to use the page size (or its multiple) as the granularity for transfer.
  81. 81. Implementing Unstructured DSM  Replication of Data <ul><li>What action should be taken when a page fault ? </li></ul><ul><li>Two possible choices: </li></ul><ul><ul><li>Move the page from the remote to the requesting processor </li></ul></ul><ul><ul><ul><li>Problems: Heavy network traffic, trashing, and delay </li></ul></ul></ul><ul><ul><li>Make a copy of the page from the remote to the requesting processor </li></ul></ul><ul><ul><ul><li>Advantages: decrease network traffic, less trashing, and reduce delay </li></ul></ul></ul><ul><ul><ul><li>Issue: How to maintain memory consistency ? </li></ul></ul></ul><ul><ul><ul><li>Read work fine  no action needed </li></ul></ul></ul><ul><ul><ul><li>Writes require others to update or invalidate . </li></ul></ul></ul>
  82. 82. Implementing Unstructured DSM  Replication of Data <ul><li>Rules to maintain memory consistency </li></ul>Allowing only one copy of writable page, but multiple copies of read-only pages.
  83. 83. Implementing Unstructured DSM  Replication of Data Allowing only one copy of writable page, but multiple copies of read-only pages. Example: P1 P2 MM1 MM2 page A (writable) page B (read-only) page B (read-only) DSM page A (writable) page B (read-only)
  84. 84. 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) P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B read
  85. 85. 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) P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B Operation is done locally . No extra action need to be taken. read
  86. 86. 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) P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B write
  87. 87. 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) P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B Operation is done locally . No extra action need to be taken. write
  88. 88. 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) P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B write
  89. 89. 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) P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B Invalidate copy in MM2; Upgrade copy in MM1 to writable. page B (writable) page B (writable) write
  90. 90. 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) P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B page B (writable) page B (writable) read
  91. 91. 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) P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B page B (writable) page B (writable) Downgrade page in MM1 to read-only; Make copy in MM2. page A (read-only) page A (read-only) page A (read-only) read
  92. 92. Implementing Unstructured DSM  Replication of Data Allowing only one copy of writable page, but multiple copies of read-only pages. Example: MM1 MM2 DSM P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B page B (writable) page B (writable) page A (read-only) page A (read-only) page A (read-only) write
  93. 93. Implementing Unstructured DSM  Replication of Data Allowing only one copy of writable page, but multiple copies of read-only pages. Example: MM1 MM2 DSM P1 P2 P1 reads A P1 writes A P1 writes B P2 reads A P2 writes B 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. write
  94. 94. Implementing Unstructured DSM  Replication of Data Allowing only one copy of writable page, but multiple copies of read-only pages. Example: MM1 MM2 DSM P1 P2 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)
  95. 95. Implementing Unstructured DSM  Memory Consistency a1=1, b1=2 a2=1, b2=2 a1=1, b1=2 a2=0, b2=0 a1=1, b1=2 a2=0, b2=1 a1=1, b1=2 a2=1, b2=2
  96. 96. Implementing Unstructured DSM  Memory Consistency a1=1, b1=2 a2=1, b2=2 a1=1, b1=2 a2=0, b2=0 a1=1, b1=2 a2=0, b2=1 a1=1, b1=2 a2=1, b2=2 Strict Consistency Sequential Consistency
  97. 97. Implementing Unstructured DSM  Memory Consistency <ul><li>Strict Consistency: </li></ul><ul><ul><li>Reading a variable x returns the value written to x by the most recently executed write operation. </li></ul></ul><ul><li>Sequential Consistency: </li></ul><ul><ul><li>Sequence of values of x read by different processes corresponds to some sequential interleaved execution of those processes. </li></ul></ul>
  98. 98. Implementing Unstructured DSM  Tracking Data <ul><li>Tracking Data: Where is it stored now? </li></ul><ul><li>Approaches: </li></ul><ul><ul><li>Have “ owner ” track it by maintaining “ copy set ” . </li></ul></ul><ul><ul><li>Only owner is allowed to write . </li></ul></ul><ul><ul><li>Ownership can change . </li></ul></ul><ul><ul><li>To find the owner using broadcast . </li></ul></ul><ul><ul><li>Central Manager -> Bottleneck </li></ul></ul><ul><ul><li>Multiple “ replicated ” managers split the responsibilities. </li></ul></ul><ul><ul><li>“ Probable owner ” gets tracked down  e.g., via page table . </li></ul></ul><ul><ul><li>Retrace data’s migration. </li></ul></ul><ul><ul><li>Update links traversed to show current owner. </li></ul></ul>
  99. 99. Implementing Unstructured DSM  Discussion <ul><li>All variables in the shared space are assumed consistent all the time . </li></ul><ul><ul><li>Moving and/or invalidating pages may be needed on write </li></ul></ul><ul><li>Much network traffic can be generated, resulting in poor performance . </li></ul><ul><li>Solution: </li></ul><ul><ul><li>Structured DSM  requires a new model of memory consistency </li></ul></ul>
  100. 100. Implementing Structured DSM  Consistencies <ul><li>Weak Consistency (Dubois et al. 1988) </li></ul><ul><ul><li>Consistency by requesting synchronization explicitly . </li></ul></ul><ul><li>Release Consistency (Gharachorloo 1990) </li></ul><ul><ul><li>Consistency upon leaving a CS </li></ul></ul><ul><li>Entry Consistency (Bershad 1993) </li></ul><ul><ul><li>Consistency upon entering a CS </li></ul></ul>
  101. 101. Implementing Structured DSM  Weak Consistency <ul><li>Introduce “ synchronization variable ,” S </li></ul><ul><li>Processes access it when they are ready to adjust/reconcile their shared variables. </li></ul>
  102. 102. Implementing Structured DSM  Release Consistency <ul><li>Synchronize upon leaving CS </li></ul><ul><ul><li>A waste if p 2 never looks at x . </li></ul></ul>
  103. 103. Implementing Structured DSM  Entry Consistency <ul><li>Before entering CS, import only those variables used </li></ul>There is also a “ lazy release consistency ” (Keleher et al. 1992) which imports all shared variables before entering CS.
  104. 104. Object-Based DSM <ul><li>An object encapsulates data and methods . </li></ul><ul><li>Can use remote method invocation (like remote procedure calls, covered earlier) instead of copying or moving an object into local memory. </li></ul><ul><li>One can move an object to improve performance. </li></ul>

×