Page 264 - Embedded Microprocessor Systems Real World Design
P. 264
Ethernet and RS-232 codes might each allocate 512K (25623 receive, 256K
transmit) for a total memory requirement of 1MB. However, as I already men-
tioned, only one interface is used at a time. In an RTOSbased system, if each task
requests only those buffers it actually needs, only one task will ever request buffers,
making the memory requirement 512K. Furthermore, suppose the system is half-
duplex, meaning that transmit and receive never occur simultaneously. In that case,
the receive task allocates a buffer, the data is processed, and the transmit task is
activated and it allocates a buffer. Because both buffers are never active at the same
time, only 256K of memory is needed.
The RTOS typically allocates memory as blocks, or chunks of contiguous
memory of a minimum size. If the block size is lK, for example, a task that needs
a l4byte buffer has to request a block and will get 1K allocated to it. Determining
block size is important in the system design. A task that needs multiple blocks
usually needs the memory to be contiguous, so the RTOS must find sufficient
contiguous blocks of memory to meet the request. If blocks are too small, memory
can become fragmented because blocks are not necessarily released by the task in
contiguous order. On the other hand, if blocks are made too large, there will be
too few blocks to meet all the memory requests of all the tasks. Figure 9.4 illustrates
both problems in graphical form.
In Figure 9.4, a small memory is shown. In Figure 9.4A, when memory blocks
are too small, memory becomes fragmented. Task 1 has allocated three blocks and
then gave two back. Task 2 did the same. Task 3 has four blocks allocated; Task 4
has two blocks. Now if a fifth task needs three contiguous blocks, there is a problem.
Blocks 1, 2, 5, 7, 8, 13, and 16 are free, but no three of these are contiguous. Task
5 cannot get enough memory to run.
Figure 9.4B shows the opposite problem, when blocks are too large. Tasks 1
through 4 have each allocated one block, even though each task may need only a
small portion of the allocated memory. When a fifth task needs a block of memory,
none is left.
Resource Management
Say our protocol converter has two possible types of data for the output device.
Perhaps, in addition to the normal receive-to-output path, diagnostic messages
also are sent to the output. In this case, an RTOS might manage the output
interface resource. If the normal output task needs to send received data, it
requests access to the output interface from the RTOS. The RTOS grants the
request, and the output task starts sending data. In the meantime, the diagnostic
task requests the interface as well. Since the interface already is allocated to the
Real-Time Operating Systems 245