Академический Документы
Профессиональный Документы
Культура Документы
Disk I
Printer 2
Motor conffol 3
Monitor 4
Now if a processwishes to use both the printer and the monitor, it must requ
first the printer and then the monitor' It can be proved that such a sche
eliminatesthe possibility of deadlock.
The hold and wait condition occurs when processesrequestresourcese
then lock that resourceuntil subsequentresourcelequestsare hlled. c)ne solut
to this problem is to allocateto a processall potentially requiredresourcesat
same time. This can, however, lead to starvation to other processes'Anoi
solution is never to allow a processthat locks more than one resourceat a til
For example,when writing one semaphore-protected disk file to another,lock (
file and copy a record, unlock that file, lock the other file, write the record' i
so on. This, of course,can lead to poor resourceutilization as well as windowt
opportunity for other processes to interrupt and interfere with resou
utilization.
resource and signal the semaphore.If we allow the higher priority task to pree
the lower one, then the deadlock can be eliminated. However, this can lea(
starvation in the low-priority process as well as to nasty interference proble
Sec.7.6I Deadlock
(For example,what if the low-priority task had locked the printer for output, and
now the high-priority task startsprinting?)
Two other ways of combating deadlock are to avoid it through avoidance
algorithms like the Banker's algorithm, or to detect it and recover from it.
Detection of deadlock is not always easy, although in embedded systems
watchdogtimers can be usedand in organic systemsmonitors are appiopriate.
7.6.1Avoidance
T EXAiIPLE 7.11
A taskneedsto rcad from file 1 andwdte to file 2. It might openfite l, reada record,closehle l.
Thenit opensflle 2, writes therecordandcldseshle 2 The processis repeatedfor eachrecorduntil
the file is transferred I
I EXAMPLE
7..I2
CoNider a systemwith threeptocesses, A, B, alrd C, and a pool of 10 resourcesof a certaint]?e
(e.g., memoryblocks). It is known that processA will neverneedmore than 6 blocks at any one
B and C the totals are 5 aDd7, respectively.A table such as the one below is
time. For proce-sses
constnrctedto keep track of the rcsouce needsand availability.
r86 Chap. 7 I Intertask Communica on and S\
6 t
5 0 5
1 0
:
Total Available ta
When resourcesare requested,the opemting system updates the table, ensuring dll I
deadlock state is not reached.An example of a "saie state" is
6 2
5 3
c 7 I
Toral Available
6 4
5. l
,1
2
Total Available
ln this case, the total requtrementsof no task can be met with the total available
deadlock could ensue.
to the child who just losr his quarter).If the systemis a mannedolle or in control
of say, an assemblyline, then the ostrich algorithm is unacceptable
Another methodfor handling the deadlockis to resetthe systemcomPletelY.
Again, this may be unacceptablefor certain critical systerhs.
Finally, if a deadlockis detected,someform of rollback to a pre-deadlockstate
can be perfomred,althoughthis may lead to a recurrentdeadlock,and operations
suchas writing to certainfiles or devicescannotbe rolled backeasily.
7,7 EXERCISES
1 What effect would size N of a ring buffer have on its performance?How would you
determinethe opdmal size?
2. For a machine you are familiar with, discuss whether the counting semaPhore
implementationgiven in this chapter has any cntical region problems That is' can the
semaphoreitself be tnte[upted in a harmful way'?
3. Why is it not wise to disable intetrupts before the while statement In the binary
semaphore,P?
4. Rewrilelhe rinC bulTertead-and-wrile procedures in
(a) C or C++
(b) Ada or Ada 95
(c) Modula-2
5. Modify the wnte ptocedure for the ring buffer to handle the overflow condition'
6, Write a set of pseudocoderoutines to access(read from and write to) a 20-item ring
buffer. The routines should use semaphoresto allow more than one user to accessthe
buffer,
7. Consider a binary semaphore,counting semaphore'queues,and mailboxes Any three
can be implementedwith the fourth We have shown hovr'binary semaphorescan be used
to implement counting semaphoresand vice versa, how mailboxes can be used to
implementbinary semaphores,and how mailboxescan be usedto implementqueues For
each pair, show how one can be used to implement the otherl
(a) Binary semaphoresfor impl€menting mailboxes.
(b) Binary semaphoresfor implementing queues.
(c) Queuesfor implemeniing binary semaphores.
(d) Queuesfor implementing mailboxes.
(e) Queuesfor implementin8 counting semaphores.
(O Counting semaphoresfor implementing mailboxes.
(g) Counting semaphoresfor implementing queues
(h) Mailboxes for implemenring counting semaphores.
8. Discussthe problemsthat can ariseif the test and setin the P(S) operationare not atomic'
What could happen if the simple assignmentstatementin the v(S) operation were nol
atomic?
9. Rewrite the binary semaphoreimplementationof the counling semaphorein
(a) C or C++
(b) Ada or Ada 95
(c) Modula-2
10. Using the ANSI-C raise and signal facilities' implement the pend(datas) and
post(data,S) operations for arbitrary mailbox S
188 Chap. ? I Intertask Communicationand
Process 2:
r|aNInq <an:6h^ra
If this busy wait must be used, is there a bettet way to test the semaphorein pror-:rs
so that the bus is not tied up?
13. Rewrite the exception handler in Example 7.8 in
(a) Ada or Ada 95
(b) Modula-2
(c) C++
14. Write a function to compute.xfactorial, tI, where i is some nonnegativeinteger
that rI = j . (.r - J ) . l and 0/ = .1.)Write an associatedexceptionhandlerthat
errols related to trying to take .r/ for -t < 0 and to handle overflow conditions-
factorial funcrion should invoke the exception handler if either error type occurs. Dc
ln
(a) c
(b) Ada or Ada 95
(c) C+r
15. Investigatethe use of signal handlersin the imPlementationof a Unix process
communication mechanism called a pipcline. Pipelines allow lhe outpuls of
processesto be used as inputs to other processes.Your investigation can be doE
examining the sourcecode to any Unix operating system,if available,or by
one of the many texts on the Unix operating system
Real-Time
Memory
Management
8.1 PROCESS
STACKMANAGEMENT
8.1.1Task-Control
Block Model
If the task-controlblock model is used, then a list of task-controlblocks is kept.
This list can be either fixed or dynamic.
In the fixed case,/r task-controlblocks are allocatedat system genemtion
time, all in the dormant state.As tasks are created,the task-confiol block enten
the ready state.Prioritizationor time slicing will then move the task to the execute
state. If a task is to be deleted, its task-control block is simply placed in the
dormant state.In the caseof a fixed number of task-conholblocks, no real-time
memory managementrs necessary.
In the dynamic case,task-controlblocks are addedto a linked list or sorne
other dynamic data structure as tasks are created.Again, the tasks are in the
suspendedstateupon creation and enter the ready statevia an operatingsystem
call or event. The tasks enter the executestateowing to priority or time-slicing.
When a task is deleted,its task-controlblock is removedfrom the linked list, and
its heap memory allocation is retumed to the unoccupiedoJ available status.
In this scheme,real-time memory managementconsistsof managing the
heap neededto supply the task-control blocks; however, gther data structures such
as a list or queuecan be used.(A heap is a specialkind of data structurebasedon
a binary tree. For a disiussion of these structures,consult any text on data
structures,for example,[83].)
to save the current context of the machine into a stack area.This call should be
made immediately after intemrpts have been disabled to prevent disaster.The
restore routine should be called just before interrupts are enabled andtf,efore
retuming from the interrupt handler.
T EXAMPLE
8.'I
Consideathe implementationof the saveroutine. Assume that global variable "stack' is to point to
the top of the stack and that eight generalregisters(R0-R7) are to be savedon a stack The memory
location "PC" cofiespondsto the lnterrupt retum vector location, and so it containsthe PC value at
the time of intefiuption. We need to save this on the stack to allow stackingof interupts The code
for a 2-addressarchltectureresemblesthe followinsl
Stack +
R7
R1
Stack RO
8 . 1 . 3Run-TimeFing Buffer
A run-time stack cannot be usedin a round-robin systembecauseof the firsfin/
firscout natureof the scheduling.In this casea ring buffer or circular queuecan
be used to save context.The context is savedto the tail of the list and restored
from the head.The saveandresloreroulines can be easilymodified to accompJish
this operation.
MaximumStackSize
The maximum amountof spaceneededfor the run{ime stackneedsto be known a
priori.ln general,stacksizecanbe determinedifrecursion is not usedandheapdata
structuresare avoided.If maximum stack memory requirementsare not known.
then a catastrophicmemory allocationcan occur,andthe systemwill fail to satisfy
event determinism.Ideally, provision for at least one more task than anticipated
should be allocated to the stack to allow for spurious intem:pm and time
overloading.We will discussthis matterfurther in Chapter11;also see[95],
8 . 1 . 5MultipleStackArrangements
Often a single run-time stackis inadequateto manageseveralprocessesin, say,a
foreground/backgroundsystem. Of course, in a multiprocessingsystem, each
processwill manage its own stack, but this is not the kind of multiple stack
schemewe are talking about.
A multiple stackschemeusesa singlerun-time stackand severalapplication
stacks. Using multiple stacks in embedded real-time systems has several
advantages.
DPI disableinterrupts
LOAD RO, context, 1
SUB RO, 1
LOAD PC, RO, 1 restoreretum location
SUB R O ,1
LOAD R7 , RO. I restorcregister7
suB R0, 1
L O A D R 5 , R O ,1 restoreiegister 6
suB R'0,1
L O A D R 5 . R O ,1 restoreregister5
SUB R O ,1
L O A D R 4 , R O ,1 restorercgister4
SUB R O ,1
LOAD R3 . RO, 1 resaoreregister3
suB R0, 1
LOAD R2 , RO, 1 restoreregister2
- suB R0, 1
8.1 I ProcessStack Manaeement 195
LOAD R 1 ,R 0 , L restoreregisterI
SUB R0,1
LOAD R O ,R O ,I restoreregister0
SUB contexL, 9 decrement stackpointer
EPI enable interupts
nr--611,'16 i n1- .
beg in
save (mains tack ) ;
.: ca i nFa-rrrnF ^f
1: inLl;
2t iIlL2;
3: inL3;
end
resCore (mainsLack )
end
hr^-6/l.,7a i-ts1
/*irtarrrr-r h:nrllar 1 */
begin
save (stackl ) ; /* save context on stack */
taskl; /" execute task 1 */
restore(stackl) /* restore context fron stack
end
hr^-64,r-6 i nF, .
/r interrupt handler 2 */
begin
drrra/ci.^l/r\.
/* save context on stack r/
task2; /* execute task 2 */
racl ^,6/cr-.^L,) /t restore context from stack */
end
hr^-aA,r,6 ini-1'
/* ihi6r,,,^r h:h/ll67 I +/
begin
.:valal:^L1\. /* save contexfo-n stack */
task3; /* execute task 3 */
restore (stack3) /* restore context from stack r/'
end
196 Chap. 8 I Real-Time Memory Management
I EXAMPLE
8.2
Supposethree processesare running in an interrupt-only systemwhere a single interrupt basedon
three prioritized intenupts is gener4ted.Let taskl, task2, and task3 be as follows:
procedure taskl;
begfn
appLicl;
aPPl ic2
end
procedure Lask2;
oeg1n
applic2;
appf rc3
end
procedure task3;
begln
appfic3;
applic4
end
Supposetaskl is running when it is interupted by task2 during applic2. Later, task2 is interrupted
by task3 dudng applic3. The main and run time stackswill then look like Figure 8.3. I
8.1.6Task-Control
Block Model
When implementingthe task-controlblock (TCB) model of rcal-time multitaskrn&
the chief memory management issue is the maintenance of the linked lists for tbe
ready ald suspendedtasks.As shown in Figure 8.4, when the currently executing
task completes,is preempted,or is suspendedwhile waiting for a resource,the ne
highest priority task in th€ ready list is rernoved and is made the executing one. E
Sec.8.2 I DynamicAllocation 197
Ready Ljst
1. Exsculinqtask|El€as€sdilic€l
rcsourcane-€dsdby suspendsd
high-prioitylask
2. Ex€culingtssk insenedin
3. Suspgnd€dtaskbegins
the executing task needsto be added to the suspendedlist, that is done. (If the
executingtaskhascompleted,then its TCB is no longer needed.)
Hence,by properly managingthe linked lists, updatingthe statusword in the
TCBs, and adheringto the appropriateschedulingpolicy by checking the priority
word in theTCBs, round-robin,preemptivepriority, or both kinds ofschedulingcan
be induced.Other memory managementcan include the maintenanceof reserved
blocks of memorythat are allocatedto individual task applicationsasrequested.
ALLOCATION
8.2 DYNAMIC
8.2.1Swapping
The simplestschemethat allows the operatingsystemto allocatememory to two
processes"simultaneously" \s swapping' In this case, the operating system is
always memory resident)and one processcan co-residein the memory spacenot
required by the operatingsystem,called the user space When a secondprocess
needsto run, the frrstprocessis suspendedandthen swapped,alongwith its contexl
to a secondarystoragedevice, usually a disk. The secondprocess,along with its
context,is then loadedinto the user spaceand initiated by the dispatcher.
This type of schemecan be used along with round-robin or preemptive
priority systems,but we would like the executiontime of eachprocessto be long
relative to the swap time. The accesstime to the secondarystore is the principal
contributor to the context switch overheadand real-time responsedelays
8.2.2 Overlays
A technique that allows a single program to be larger than the allowable user
spaceis called overlaying.In this casethe program is broken up into dependent
code and data sections called overlays, which can fit into available memoq'.
Specialprogram code must be included that permits new overlaysto be swaPped
into memory as needed(over the existing overlays), and care must be exercised
in the design of such sYstems.
This technique has negative real-time implications becausethe overla;c
must be swappedfrom secondarystoragedevices.Nevertheless,overlaying ca
be usedin conjunction with any of the techniquesmentionedlater in this chapter
to extend the available address space. Many commercial tools are available thr
facilitate overlaid linking and loading in conjunction with commonly used
programming languagesand machines.
Note that in both swapping and overlaying a portion of memory is never
swapped to disk or overlaid. This memory contains the swap or overlay
manager(and in the caseof overlaying any code that is common to all overlayr
is called the root).
8.2.3 MFT
A moreelegantschemethansimpleswappingallowsmore thanoneprocessto bc
rnemory-resident at any one time by dividing the userspaceinto a numberd
txed-size partitions.This scherneis calledMFT (multiprogrammingwith a fixed
numberof tasks)and is useful in systemswherethe numberof tasksto bc
I DynamicAllocation
I EXAMPLE8.3
In Figure 8.5, even though 40 megabytesof memory are available,they are in noncottlguousblocks,
so the requestcannot be honored.
8.2.4MVT
ln MVT (or multiprogramming with a variable number of tasks), memory is
allocated in amounts that are not fixed, but rather are determined by the
requirementsof the processto be loaded into memory. This techniqueis more
appropriatewhen the numberof reahime tasksis unknown or varies.In addition'
memory utilization is better for this techniquethan for MFT becauselittle or no
intemal fragmentationcan occur,as the memory is allocatedin the amountneeded
for each process.Extemal fragmentationcan still occur becauseof the dynamic
nature of memory allocation and deallocation,and becausememory must still be
allocatedto a processcontiguously.
In MVT, however,extemal fragmentationcan be mitigated by a processof
compressing fragmented memory so that it is no longer fragmented. This
techniqueis called compaction(seeFigure 8.6). Compactionis a CPU-intensive
processand is not encouragedin hard real-time systems.If compactionmust be
performed,it shouldbe donein the background,and it is imperativethat interrupts
be disabledwhile memory is being shuffled.
The bottom line is that MVT is useful when the number of real-time tasks
is unknown or can vary. Unfortunately,its context-switchingoverheadis much
hieher than in simpler schemessuch asMFT, and thus it is not always appropriate
Belore
I Dynamic Allocation
DemandPaging
In demand pdge systems, program segments are permitted to be loaded in
noncontiguousmemory as they are requestedin fixed-sizechunks calledpagesor
page frames. This schemehelps to eliminate extemal fragmentation.Program
codethat is not held in main memoryis "swapped"to secondarystorage,usually
a disk. When a memory referenceis made to a location within a page not loaded
in main memory, a page fault exception is raised.The interrupt handler for this
exceptionchecksfor a free page slot in memory. If none is found, a page frame
must be selectedand swappedto disk (if it has beenaltered)-a processcalled
page stealing.Paging,which is providedby mostcommercialoperatingsystems,
is advantageousbecauseit allows nonconsecutivereferencesto pagesvia a pd.qe
table. ln addiiion, paging can be used in conjunction wirh bank switching
hardwareto extend the virtual addressspace.In either case,pointers are usedto
accessthe desiredpage (see Figure 8.7). Thesepointers may representmemory-
mapped locations to map into the desired hard-wired memory bank; may be
implbmentedthrough associativememory; or may be simple offsets into memory,
in which casethe actual addressin main memory needsto be calculatedwith each
memory reference.
Paging can lead to problems including very high paging activity called
thrashing, intemal fragmentation,and the more seriousdeadlock(seeChapter7).
But it is unlikely.that you would use so complex a schemeas paging in an
embedded real-time system where the overhead would be too great and the
associatedhardware support is not usually available.
8.4
T EXAMPLE
A pagedmemory systemis divided into sixteen256-megabytepagesof which any 4 can b€ loaded
at ihe sametime. Each page is tagged(1, 2, etc ) The operatingsystemkeepstrack of the usageof
each page.For examPle,the Pagereierencestring
2 3 4 5
indicatesthat pages2, 3, 4, and 5 have been used in that order' If a requestis made for page7' theo
page2 will be swappedout in order to make room for page7, becauseit was the leastrecently used
The loaded pageswould then be 3,4,5' and 7 with referencestdng
2 3 4 5 ' 7
pleasenote that referencesto pagesalreadyloaded in memory causeno page fault. For instance,if
a referenceis now made to page 3, no pages need to be swapped becausePage 3 is loaded iD
memory. If this referenceis followed by one to page 6, page 4 would have to be swappedou
becauseit had the least recent reference.The loaded pages would then be 3, 5' 7' and 6 wih
reference string
2 3 4 5 ' 7 3 6
Notethatinapagingmemoryscheme,thewolstpossiblescenarioinvolvespagestealirrgforead
request of memory. This occurs, fol examPle, in a four-page system when five pages are requested
cyclically as in the page referencestring
2 4 6 8 9 2 4 6 8 9"'
You should note that the performance of LRU is the same in this case as FIFO (in
terms of number of Pagefaults).
ln FIFO pagereplacementschemes(whetheror not usedin conjunctionwitl
working sets),we might tliink thatby increasingt\e numberofpagesin mernory(a
windowsin the working settwe canreducethenumberof pagefaults Oftenthisb
the case,but occasionallyan anomalousconditionoccurswherebyincreasingtb
numberof pagesactually increaseslhe numberof pagefaults This is Beladyb
Anomaly,whichasit tumsout,doesnot occurin LRU replacement schemes'
To conclude, the overhead for the LRU scheme rests in recording the
sequenceto all pages,which can be quite substantial.Therefore' the benefrtsof usi
LRU need to be weighed against the effort in implernenting it vis-d'vls FIFO'
i:c. X 2 I DlnamicAllocation
8.2.6WorkingSets
Working sets are based,onthe model of localtty-of-reference.The idea is if you
examine a list of recently executedprogram instructionson a logic analyzer,you
will note that most of the instructionsare localized to within a small number of
instructions in most cases. (For example, in the absence of lnterrupts and
blanching, the program is executedsequentially.Or the body of a ioop may be
executeda large number of times.)However, when interrupts,procedurecalls, or
branchingoccurs,the locality-of-referenceis altered.The idea in workhg setsis
that a set of local code windows is maintained in the cache and that upon
accessinga memory location not containedin one of the working sets,one of the
windows in the working set is replaced(using a replacementrule such as FIFO or
Chap. 8 I Real-Time Memory Management
LRU). The performanceof the schemeis basedentirely on the sizeof the working
set window, the number of windows in the working set, and the locality-of-
referenceof the code beins executed.
8.2.7Real-Time
GarbageCollection
In a memory-management context,garbageis memorythat hasbeenallocatedbut
is no longer being usedby a task (that is, the task has abandonedit). Garbagecan
accumulatewhen tasks terminate abnormally without releasingmemory resour-
cesn441. It can also occur in object-orientedsystemsand as a normal byproduct
of nonprocedurallanguages[4], [162].
In C, for example,if memory is allocatedusing the malloc procedureand the
pointer for that memory block is lost, then that block cannotbe usedor properly
freed. The samesituationcan occur in Pascalwhen recordscreatedwith the new
statementare not properly disposedof.
Garbage collection algorithms generally have unpredictableperformance
(althoughaverageperformancemay be known). Garbagecan be reclaimedusing
the following procedure.Tag all memory from the heap which is pointed to by a
variable (including those variables in procedure activation frames-a non-
deterministicdata structure).Then reclaim all nontaggedmemory for the heap.
The loss of determinismresultsfrom the unknown amountof garbage,the tagging
time of the nondeterministicdata structures,and the fact that many incremental
garb;ge collectorsrequire that every memory allocationor deallocationfrom the
heap be willing to service a page-faulttrap handler
Another techniqueis to build a heap or table of memory blocks along wirt
an associatedprocessID for the owner of the memory block. This data structurE
is then periodically checkedto determinewhether memory has been allocatedto
a processthat no longer exists. If this is the case,the memory can be released
Becauseof the overheadinvolved, this method should not be implementedin
high-frequencycycles, and ideally garbagecollection should be performed as r
backgroundactivity or not performedat all [4]. Nevertheless,researchin real-tic
garbagecollection is still open.
8.2.8ContiguousFileSystems
Disk I/O is a problem in many real-time systemsthat can be exacerbatedby
fragmentation. File fragmentation is analogousto memory fragmentation and
the same associatedproblems, only worse. In addition to the logical
incuned in finding thcinext allocation unit in the file, the physical overhead of
disk mechanism is a factor.For example,physicaloverhead involvedin
the disk's read/writeheadto the desiredsectorcan be sisnificant
To reduce or eliminate this problem aliogether, many commercial real-d
systems,such asreal-time UNIX, force all allocatedsectorsto follow one
on the disk.-This techniqueis called contiguousfile allocation.
Sec.8.4 t Exercises 205
8,3 STATICSCHEMES
Static memory issues revolve around the partltioning of memory into the
appropriateamount of RAM, ROM, memory-mappedI/O space,and so on. This
in Chapter9.
problemof resourceallocationis discussed
8.4 EXERCISES
1. Rewrite the sale and restoreroutines assumlngthat eight generalregisters(R0 R7) and
the paogramcounter are to be saved on a stack. Do this fbr
(a) O-addressmachrne
(b) l-addressmachine
(c) 3-addressmachine
2, Rewrite the saveand restoreroutinesin 2-addresscode,assumingblock move (BMOVE)
and restore (BRESTORE) instructions are available Make the necessaryassumptions
aboutlhe tormal o[ the.e instruction:..
3. Rewaifethe saveand restoreroutines so that they saveand restoteto the headand tail of
a ring buffer, respectively.
4. Rewrite the save and testore routines in Pascal so that they employ push and pop
procequres.
Write a pseudocodealgorithm that allocatespagesof memory on request.Assume that
100 pages of size I megabyte, 2 megabytes, and 4 megabytes are available The
algorithm should take size of the pagerequestedas an argument,and retum a pointer to
the desiredpage.The smallestavailable page should be used,but if the smallestsrze ls
unavailable.the next smallestshould be used.
6. Write a pseudocodealgorithm compacting64 megabytesof memory that is divided into
l-megabyte pages Use a pointer scheme.
7. For a four-page merilory systemwith memory referencestring, for example,
2 4 6 8 9 2 4 6 8 9-..
show rhat the number or page faults for FIFO replacementis lhe same as for the LRU
replacementscheme.
8. A pagedmemory systemis divided into sixteen256-megabytepagesof which any four
can be loaded at the same time. Each page is tagged (1,2, etc.). Write a pseudocode
algorithm to implement the least recently used rule.
9, Write a heapmanagerto handlearbitrary-sizeddatablocksin a link list (analogousto the C
malloco routine).Rememberthat the run-time stackcan collide with the heap.Do this in
fa) C
(b) Ada
fc) Pascal
(d) Modula-2
(e) C++
10. Modify the heap manager in the previous exercise so that a table consisting of the
memory block number and processID is stored.Wrire a garbagecollection routine lo
accomFanythe heap manager which consults a second table consisting of a list of all
existing processIDs and frees all memory blocks belonging to extinc! processes.