42
Sharing of Data and Code in Main Memory • Single-Copy Sharing • Sharing in Systems without Virtual Memory • Sharing in Paging Systems • Sharing in Segmented Systems • Principles of Distributed Shared Memory • Implementations of Distributed Shared Memory

Sharing of Data and Code in Main Memory

Embed Size (px)

DESCRIPTION

Sharing of Data and Code in Main Memory. Single-Copy Sharing Sharing in Systems without Virtual Memory Sharing in Paging Systems Sharing in Segmented Systems Principles of Distributed Shared Memory Implementations of Distributed Shared Memory. 9.1 Single-Copy Sharing. - PowerPoint PPT Presentation

Citation preview

Page 1: Sharing of Data and Code in Main Memory

Sharing of Data and Code in Main Memory

• Single-Copy Sharing

• Sharing in Systems without Virtual Memory

• Sharing in Paging Systems

• Sharing in Segmented Systems

• Principles of Distributed Shared Memory

• Implementations of Distributed Shared Memory

Page 2: Sharing of Data and Code in Main Memory

9.1 Single-Copy Sharing

• Data Sharing: -- communicating, cooperating, and competing among processes

• Code Sharing-- reuse of modules( simplifying the engineering of software systems)-- better utilizing main memory

9.1.1 Reasons for Sharing

Page 3: Sharing of Data and Code in Main Memory

Main memoryP1’s Virtual address space P2’s Virtual address space

Kernel

User

1GB

3GB 3GB

1GBKernel

User

. . .

. . .

0x00000000

Figure1: Sharing OS Kernel among processes

0x00000000

Page 4: Sharing of Data and Code in Main Memory

9.1.2 Requirements for Sharing

• Designating Resources as Shared-- at the time of linking or loading( statically)

-- at runtime( dynamically)

int shmget( key_t key, int size, int shmflg);

void *shmat(int shmid, const void *shmaddr, int shmflg);

• Reentrant Code-- being kept as read-only segments

-- addresses generated by the shared code should be translated correctly

Page 5: Sharing of Data and Code in Main Memory

Shared code

. . .branch. . .

load. . .

Data of process p1

Data of process p2

Figure2: addresses in shared code

Page 6: Sharing of Data and Code in Main Memory

9.1.3 Linking and Sharing

• Linking-- relocation of individual object modules

-- binding of external references

• Sharing

-- static( links prior to execution)

-- dynamic( links are resolved at runtime)

( the linking of the same copy of a module into two or more address spaces)

Page 7: Sharing of Data and Code in Main Memory

9.2 Sharing in Systems without Virtual Memory

• Background-- absence of any dynamic relocation mechanisms

-- having only a single relocation register

• Sharing of user code or data--partially overlapping processes’ memory spaces

• Sharing of system components-- being assigned specific agreed-upon addresses

-- linker replaces each reference to a shared system module by its known memory address

-- the shared system component cannot easily differentiate between the different processes that invoke it

Page 8: Sharing of Data and Code in Main Memory

Example: Sharing of Code Using Relocation Registers

. . .

CBR1

SBR1

DBR1

. . .

CBR2

SBR2

DBR2

. . .

Register contents when p1 running

Register contents when p2 running

code Branch x(CBR)

. . .

stack1. . .

stack2

. . .data2

. . .data1

. . .

Main memory

Page 9: Sharing of Data and Code in Main Memory

9.3 Sharing in Paging Systems

. . .. . .

. . .

. . .. . .

. . .n w

(n, w)

. . .

. . .

Page table

0

n

Virtual memory Physical memory

Virtual address

Page 10: Sharing of Data and Code in Main Memory

9.3.1 Sharing of data

Main memory

. . .. . .

. . .. . .

Load n1,w

Load n2,w

Shared page

. . .

. . .

. . .

. . .

PT1

PT2

0

0

n2

n1

Page 11: Sharing of Data and Code in Main Memory

9.3.2 Sharing of Code

• Branch instructions to other location within the shared page-- the branch address is compiled to absolute one

1. shared pages must be assigned the same page numbers;

2. potential page number conflicts;

3. wasted table space.

-- the branch address is compiled relative to the code base register

Page 12: Sharing of Data and Code in Main Memory

Main memory

. . .. . .

. . .. . .

Load n1,w

Load n2,w

Shared page

. . .

. . .

. . .

. . .

PT1

PT2

0

0

n2

n1

Branch (n,w)

XXX(n, w)

n1==n2

Page 13: Sharing of Data and Code in Main Memory

• External references-- static linking&binding

1. assigning page numbers and replace every external reference

wasted table space2. filling page table entries Unable to know whether the shared page is on dis

k or in memory-- dynamic linking&binding

postpone the assignment of page numbers and locating the routines until runtime, when a particular function is actually being invoked

Page 14: Sharing of Data and Code in Main Memory

. . .bri

. . .bri

. . .

. . .

stub

stub

stub

. . .

. . .bri

. . .bri

. . .

. . .

stub

stub

. . .

Shared code

Transfer vectors

Dynamic linking through transfer vector

Page 15: Sharing of Data and Code in Main Memory

……

call 0xaabbccdd

……

……

0xaabbccddJmp *GOT[i]

……

PLT[i]

……

0xaffffff0

Stub

GOT[i]

Virtual address space (ELF)

……

Func();

……

……

0xaffffff0Transfer vector

Page 16: Sharing of Data and Code in Main Memory

……

call 0xaabbccdd

……

……

0xaabbccddJmp *GOT[i]

……

PLT[i]

……

0xbbbbbbb0

Stub

GOT[i]

Virtual address space (ELF)

……

Func();

……

……

Shared page

……

……

Func(){

……

}

……0xbbbbbbb0

0xaffffff0Transfer vector

Page 17: Sharing of Data and Code in Main Memory

#include "stdio.h"#include "dlfcn.h"#define SOFILE "./my.so"#define SHARED#include "date.h"main(){

DATETYPE d;void *dp;char *error;puts("dll demo!");dp=dlopen(SOFILE,RTLD_LAZY);if(dp==NULL){

fputs(dlerror(),stderr);exit(1);

}getdate=dlsym(dp,"getdate");error=dlerror();if(error){

fputs(error,stderr);exit(1);

}getdate(&d);printf("current date:%04d-%02d-%02d

\n",d.year,d.mon,d.day);dlclose(dp);exit(0);

}

#include "time.h"#include "date.h"int getdate(DATETYPE *d){

long ti;struct tm *tm;time(&ti);tm=localtime(&ti);d->year=tm->tm_year+1900;d->mon=tm->tm_mon+1;d->day=tm->tm_mday;

}

Page 18: Sharing of Data and Code in Main Memory

(gdb) disassemble mainDump of assembler code for function main:0x08048544 <main+0>: push %ebp0x08048545 <main+1>: mov %esp,%ebp0x08048547 <main+3>: sub $0x28,%esp……0x080485ae <main+106>: pushl 0xffffffe4(%ebp)0x080485b1 <main+109>: call 0x8048404 <dlsym>0x080485b6 <main+114>: add $0x10,%esp0x080485b9 <main+117>: mov %eax,0x8049840

Disassembly of section .plt: 080483f4 <.plt>: 80483f4: ff 35 08 98 04 08 pushl 0x8049808 80483fa: ff 25 0c 98 04 08 jmp *0x804980c 8048400: 00 00 add %al,(%eax) 8048402: 00 00 add %al,(%eax) 8048404: ff 25 10 98 04 08 jmp *0x8049810 804840a: 68 00 00 00 00 push $0x0 804840f: e9 e0 ff ff ff jmp 80483f4 <_init+0x18> 8048414: ff 25 14 98 04 08 jmp *0x8049814 804841a: 68 08 00 00 00 push $0x8 804841f: e9 d0 ff ff ff jmp 80483f4 <_init+0x18>

Page 19: Sharing of Data and Code in Main Memory

(gdb) list main2 #include "stdio.h"3 #include "dlfcn.h"4 #define SOFILE "./my.so"5 #define SHARED6 #include "date.h"7 main(){8 DATETYPE d;9 void *dp;10 char *error;11 puts("dll demo!");(gdb)12 dp=dlopen(SOFILE,RTLD_LAZY);13 if(dp==NULL){14 fputs(dlerror(),stderr);15 exit(1);16 }17 getdate=dlsym(dp,"getdate");18 error=dlerror();19 if(error){20 fputs(error,stderr);21 exit(1);

Breakpoint 8, main () at date.c:1717 getdate=dlsym(dp,"getdate");(gdb) x 0x80498100x8049810 <_GLOBAL_OFFSET_TABLE_+12>: 134513674(gdb) continueContinuing. Breakpoint 9, main () at date.c:1818 error=dlerror();(gdb) x 0x80498100x8049810 <_GLOBAL_OFFSET_TABLE_+12>: 1073926128(gdb)

Page 20: Sharing of Data and Code in Main Memory

9.4 Sharing in Segmented Systems

• Same segment number in all virtual spaces-- example: I386 processor’s sharing of system segments

GDT( global descriptor table)

LDT( local descriptor table)

• Using base registers

PTLTI

0315 2 1

privilege level

TI=0:GDTR

TI=1:LDTR

selector

CS structure

Page 21: Sharing of Data and Code in Main Memory

• Unrestricted dynamic linking

• Each segment has its own linkage section

• Recording the external references for only that segment

• A single transfer vector is provided for the entire process

• Containing entries for every external references

Linkage Section Transfer Vector

Page 22: Sharing of Data and Code in Main Memory

. . .Segment table

. . .

. . .

j

i

. . .load * l d

. . .

. . .(S,W)

. . .

Symbol table

Code segment C

. . .

Linkage section for C

Trap on

. . .

dLBR

CBR

Page 23: Sharing of Data and Code in Main Memory

. . .Segment table

. . .

. . .

s

i

. . .load * l d

. . .

Code segment C

. . .

Linkage section for C

Trap on (s,w)

. . .

dLBR

CBR

. . .

. . .

Segment S

. . .

w

i

Page 24: Sharing of Data and Code in Main Memory

9.5 Principles of Distributed Shared Memory

CPU Main Memory

Memory bus

…CPU1 Main Memory

Memory bus

CPU2 CPUn

CPU1 Main Memory1

Memory bus

Bridge

I/O bus

Network controller

CPUn Main Memoryn

Memory bus

Bridge

I/O bus

Network controller

. . .

NE

TW

OR

K

Page 25: Sharing of Data and Code in Main Memory

CPU

Main memory

Single Processor &Single

Memory

CPU1

Main memory

CPU2

CPUn

. . .Multi-processor & Single

Memory

CPU1

Main memory

CPU2

CPUn

. . .

Multi-processor & Multi-Memory

. . .MM1

MM2

MMn

Page 26: Sharing of Data and Code in Main Memory

• Distributed Shared Memory Objective of DSM

Alleviating the burden on the programmer by hiding the fact that physical memory is distributed and not accessible in its entirety to all processors.

scheme Creating the illusion of a shared memory that should be mapped into distributed physical memory.

Page 27: Sharing of Data and Code in Main Memory

• The User’s View of DSM Main questions concerned by users

-- which portions of memory are shared and which portions are private among the different processors?

-- Is the sharing fully transparent(must the user do anything special to accomplish the sharing)?

Unstructured Distributed Shared Memory

simulating the behavior of a single, physically shared memory( from the user’s point of view, there is only a single linear address space)

Structured Distributed Shared Memory

segregating shared and nonshared portions of each logical spaces of processes. When any of the processors writes into the shared portion, the change becomes visible automatically and transparently by all other processors.

Page 28: Sharing of Data and Code in Main Memory

CPU1

CPU2

CPUn

. . .

Main memory

. . .MM1

DSM

MM1

MM1

Unstructured DSM

Page 29: Sharing of Data and Code in Main Memory

. . .

MM1

MM2

MMn

DSM

. . .

Logical Address Space

CPU1

(process1)

CPU2

(process2)

CPUn

(processn)

. . .

Page 30: Sharing of Data and Code in Main Memory

9.6 Implementations of Distributed Shared Memory

• Implementing Unstructured DSM Replication of Data Strict Versus Sequential Consistency Granularity of Data Transfers Finding pages

Page 31: Sharing of Data and Code in Main Memory

• Replication of Data

Page A

. . .

MM1

. . .

Page B

. . .

MM2

. . .

Page A

. . .

DSM

. . .

Page B

. . .

P1

P2 Write B

Maintaining multiple copies of a page are decreased network

traffic, less thrashing, and reduced delays resulting from

page faults

Page B

Replicating

Page 32: Sharing of Data and Code in Main Memory

Initial: x=0

Real time: t1 t2 t3 t4

P1: {x=1; a1=x; x=2; b1=x;}

P2: { a2=x; b2=x;}

operation Page location Page status Actions taken before local Read/Write

read Local Read-only

Write Local Read-only Invalidate remote copies;

upgrade local copy to writable

Read Remote Read-only Make local read-only copy

Write Remote Read-only Invalidate remote copies;

Make local writable copy

Read Local Writable

Write Local Writable

Read Remote Writable Downgrade page to read-only;

Make local read-only copy

write Remote Writable Transfer remote writable copy to local memory

Protocol for handling DSM pages

Page 33: Sharing of Data and Code in Main Memory

Page A

. . .

MM1

. . .

Page B

. . .

MM2

. . .

Page A

. . .

DSM

. . .

Page B

. . .

P1

P2

Page B

P1 reads A Done locally

P1 writes A Done locally(page is writable)

P1 writes B Invalidate copy in MM2;

Upgrade copy in MM1 to writable

P2 reads A Downgrade page in MM1 to read only;

Make copy in MM2

P2 writes B Transfer page from MM1 to MM2

Page 34: Sharing of Data and Code in Main Memory

• Strict Versus Sequential ConsistencyInitial: x=0

P1:{

x=1;

a1=x;

x=2;

b1=x;

}

P2:{

a2=x;

b2=x;

}

P1:{ x=1; a1=x; x=2; b1=x;}

P2:{ a2=x; b2=x;}

P1:{ x=1; a1=x; x=2; b1=x;}

P2:{ a2=x; b2=x;}

P1:{ x=1; a1=x; x=2; b1=x;}

P2:{ a2=x; b2=x;}

(a)

(b)

(c)

Page 35: Sharing of Data and Code in Main Memory

• Strict consistency

A DSM is said to obey strict consistency if reading a variable x always returns the value written to x by the most recently executed write operation.

• Sequential consistency

A DSM is said to obey sequential consistency if the sequence of values read by the different processes corresponds to some sequential interleaved execution of the same processes.

Page 36: Sharing of Data and Code in Main Memory

• Granularity of Data Transfers Decreased granularity

Conflicting with the principle of locality Increased granularity

False sharing phenomenon

Page 37: Sharing of Data and Code in Main Memory

…X……Y…

…X……Y…

…X…

…Y…

MM1

MM2

MM3

P1

P3

…X……Y…

…X……Y…

MM1

MM3

Page 38: Sharing of Data and Code in Main Memory

• Finding Pages Owner of pages Broadcasting Central manager Probable Owner

P1

. . .A

. . .

Page A

P2

. . .A

. . .

P3

. . .A

. . .

P2 P3

Page 39: Sharing of Data and Code in Main Memory

• Implementing Structured Distributed Shared Memory Distributed shared memory with weak

consistency Distributed shared memory with release

or entry consistency Object-based distributed shared

memory

Page 40: Sharing of Data and Code in Main Memory

x=1

a1=x

S

x=2

b1=x

x

S

a2=x

b2=x

x②

Weak memory consistency Release memory consistency

Lock

x=2

unlock

x

lock

a=x

unlock

x②

Cause propagation

Delayed until p1 unlocks

Page 41: Sharing of Data and Code in Main Memory

Release memory consistency

Lock

x=2

unlock

x

lock

a=x

unlock

x②

Cause propagation

Delayed until p1 unlocks

Page 42: Sharing of Data and Code in Main Memory

Object-based distributed shared memory

If the system provides the ability to perform remote methods invocation, there is no need to transfer a remote object into local memory to access the data objects encapsulate

Exercises:

P298—302: 2,5,6,8