Page 49 - Designing Autonomous Mobile Robots : Inside the Mindo f an Intellegent Machine
P. 49

Chapter 3

            Time slicing

            Time slicing is a simple method of sharing CPU resources between tasks. Unlike the
            above interrupt event example, the tasks are not required to relinquish control, it
            simply happens to them at any time in the execution of their program. Threads may
            be allowed to mask the switch during critical operations, but otherwise the sharing
            operation is totally asynchronous to their operations.
            Basically a time slicing kernel receives an interrupt from a hardware timer, which
            interrupts the thread that is currently running. The registers for that thread are
            saved on its stack, and the kernel switches the stack to the next task. Execution
            continues until the next timer interrupt, when the process is repeated.
            UNIX and other quasi real-time operating systems have time-slicing task switching.
            In most of these systems the time slice can be set larger for some tasks and shorter
            for others, giving important tasks a bigger slice of the CPU pie.

            Occasionally there will be multiple threads that do not lend themselves to event task
            switching, while other threads require time slicing. In these cases, time slicing can
            be used on some tasks while event switching can be used on others. The biggest
            shortcoming of time slicing is that it does not necessarily provide optimal allocation
            of CPU resources. Additionally, timing glitches may be induced in the tasks being
            switched because time slicing is not generally synchronous with any particular place
            in these threads. So if another process is consuming their output, the programmer
            must assure that that process does not get half finished results.

            Normally, the kernel manages the list of running threads, the priority for each
            thread and the status of the thread, the events it is waiting for, and so forth. For
            small, dedicated systems, all of this overhead may not be justified, and the program-
            mer can just write a simple time slicing context switch driven by a timer interrupt.
            The trade-off is one of efficiency versus flexibility.

            Reentrance

            In a real-time system, the programmer no longer defines the exact sequence in
            which code will execute. Instead, the programmer sets the ground rules, and events
            determine the way things go down. This fact requires the programmer to go from
            linear thinking to thinking in dynamic terms.

            The first difference becomes apparent when programming functions (subroutines)
            that are general purpose in nature. These routines may be called by many different




                                                    32
   44   45   46   47   48   49   50   51   52   53   54