Here is a sample of a program that would demonstrate shared memory working.
The main program simply starts two processes then exits:
FORK PRO1
FORK PRO2
EXIT
Process One allocates a page of shared memory as virtual page 10 (which will contain all zeros
to start with); it then goes into a little loop waiting until the first int in
that page (virtual address 5120) is not zero any more (this can only happen if another
process modifies it), at which point it prints 999 and stops.
PRO1:
LOADC 10
STORR 2
LOADC 237535
STORR 3
SHMAP
LOOP1:
LOAD 5120
CMPC 0
JZER LOOP1
LOADC 999
OUTD
EXIT
Process Two allocates the same shared page, but as virtual page 5, it then goes into a
countdown loop in which the accumulator starts at 15 and counts down to 1, printing
each number as it is reached. When the
accumulator is equal to 10, it sets the first int of the shared page (virtual address
2560) to 1, as a signal to the other process. To avoid using a temporary variable in memory,
the loop control variable (15..1) is kept safe in register B.
PRO2:
LOADC 5
STORR 2
LOADC 237535
STORR 3
SHMAP
LOADC 15 ; set ACC and B to 15
STORR 2
LOOP2:
JZER END ; when zero its all over
OUTD
CMPC 10
JZER SIG ; when ACC=10 send the signal
CONT:
LOADR 2 ; restore ACC from B, CMPC ruined it
SUBC 1
STORR 2 ; decrement before looping
JUMP LOOP2
SIG:
LOADC 1
STORE 2560 ; send the signal
LOADR 2 ; restore ACC from B
JUMP CONT ; continue as usual
END:
EXIT