computer organization cs224
DESCRIPTION
Computer Organization CS224. Fall 2012 Lesson 12. Synchronization. Two processors or threads sharing an area of memory P1 writes, then P2 reads Data race if P1 and P2 don’t synchronize Result depends of order of accesses !! Software synchronization routines needed - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/1.jpg)
Computer OrganizationCS224
Fall 2012
Lesson 12
![Page 2: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/2.jpg)
Synchronization Two processors or threads sharing an area of memory
P1 writes, then P2 reads Data race if P1 and P2 don’t synchronize
- Result depends of order of accesses !!
Software synchronization routines needed Lock and unlock, etc. To implement mutual exclusion to shared memory
Hardware support required Atomic read-and-modify memory operation No other access to the location allowed between the read and write Used for e.g. atomic swap of register ↔ memory Could be a single instruction (as in some processors) Or an atomic pair of instructions (as in MIPS and others)
§2.11 Parallelism
and Instructions: Synchronization
![Page 3: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/3.jpg)
Atomic Exchange Support
Atomic exchange (atomic swap) – interchanges a value in a register for a value in memory atomically, i.e., as one operation (instruction)
Implementing an atomic exchange would require both a memory read and a memory write in a single, uninterruptable instruction. An alternative is to have a pair of specially configured instructions
ll $t1, 0($s1) #load linked
sc $t0, 0($s1) #store conditional
![Page 4: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/4.jpg)
Atomic Exchange with ll and sc
If the contents of the memory location specified by the ll are changed before the sc to the same address occurs, the sc fails (returns a zero)
try: add $t0, $zero, $s4 #$t0=$s4 (exchange value)ll $t1, 0($s1) #load memory value to
$t1sc $t0, 0($s1) #try to store exchange
#value to memory, if fail #$t0 will be 0
beq $t0, $zero, try #try again on failureadd $s4, $zero, $t1 #load value in $s4
If the value in memory between the ll and the sc instructions changes, then sc returns a 0 in $t0 causing the code sequence to try again.
![Page 5: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/5.jpg)
The C Code Translation Hierarchy
C program
compiler
assembly code
assembler
object code library routines
executable
linker
loader
memory
machine code
§2.12 Translating and S
tarting a Program
![Page 6: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/6.jpg)
Assembler Pseudoinstructions
Most assembler instructions represent machine instructions one-to-one
Pseudoinstructions: figments of the assembler’s imagination
move $t0, $t1 → add $t0, $zero, $t1
blt $t0, $t1, L → slt $at, $t0, $t1bne $at, $zero, L
$at (register 1): assembler temporary
![Page 7: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/7.jpg)
Producing an Object Code Module
Assembler (or compiler) translates program into machine instructions, makes an object module
Provides information for building a complete program from the pieces
Header: described contents of object module Text segment: translated instructions Static data segment: data allocated for the life of the
program Relocation info: for contents that depend on absolute
location of loaded program Symbol table: global definitions and external refs Debug info: for associating with source code
![Page 8: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/8.jpg)
Linking Object Modules
Produces an executable image (.exe file)1.Merges segments
2.Resolve labels (determine their addresses)
3.Patch location-dependent and external refs
Could leave location dependencies for fixing later with a relocating loader
But with virtual memory, no need to do this Program can be loaded into absolute location in virtual memory
space
![Page 9: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/9.jpg)
Loading a Program
Load from image file on disk into memory1.Read header to determine segment sizes for text and data
2.Create virtual address space (large enough for text and data)
3.Copy text and initialized data into memory
4.Copy parameters for main program (if any) onto stack
5. Initialize registers (including $sp, $fp, $gp)
6. Jump to startup routine
- Copies arguments to $a0, … and calls main
- When main returns, do exit syscall
![Page 10: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/10.jpg)
Dynamic Linking
Only link/load library procedure when it is called Requires procedure code to be relocatable Avoids image bloat caused by static linking of all
(transitively) referenced libraries Automatically picks up new library versions
![Page 11: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/11.jpg)
Lazy Linkage
Indirection table
Stub: loads routine ID,jumps to linker/loader
Linker/loader code
Dynamicallymapped code
![Page 12: Computer Organization CS224](https://reader036.vdocument.in/reader036/viewer/2022082517/56812a59550346895d8db7f9/html5/thumbnails/12.jpg)
Starting Java Applications
Simple portable instruction set for
the JVM
Interprets bytecodes
Compiles bytecodes of “hot” methods
into native code for host
machine