Page 108 - DCAP103_Principle of operating system
P. 108

Unit 3: Process Management-II



                                                                                                  Notes
            to run for a very short time. It then interrupts that thread and allows a second thread to
            run for a very short time, and so on. A portion of the thread transitions on such an operating
            system is shown in Figure 3.10.

                              Figure 3.10: Thread States with OS Scheduling




























            Java does not mandate that its threads be time-sliced, but most operating systems do so. There
            is often some confusion in terminology here: preemption is often confused with time-slicing. In
            fact, preemption means only that a higher-priority thread runs instead of a lower-priority one,
            but when threads have the same priority, they do not preempt each other. They are typically
            subject to time-slicing, but that is not a requirement of Java.

            There is one other important point about these two figures. In our first figure, the time points
            (T1, T2, and so on) are relatively far apart. The time transitions in that case are determined
            when a particular thread changes state: when the main thread changes to the blocked state, a
            task thread changes to become the currently running thread. When that thread changes to the
            exiting state, a second task thread changes to become the currently running thread and so on.
            In the second case, the time transitions occur at a much shorter interval, on the order of a few
            hundred milliseconds or so. In this case, the transitions of the threads between currently running
            and waiting for CPU are imposed by the operating system and not as a result of anything the
            thread itself has done. Of course, if a thread voluntarily changes to the exiting or waiting state,
            a transition occurs at that point as well.

            3.8 Priority Exceptions

            When an operating system schedules Java threads, it may choose to run a lower-priority thread
            instead of a higher-priority thread in two instances, described next.
            Priority inversion:  In  a  typical  priority-based  threading  system,  something  unusual  occurs
            when a thread attempts to acquire a lock that is held by a lower-priority thread: because the
            higher-priority thread becomes blocked, it temporarily runs with an effective priority of the
            lower-priority thread. Suppose that we have a thread with a priority of 8 that wants to acquire
            a lock that is held by a thread with a priority of 2. Because the priority 8 thread is waiting for
            the priority 2 threads to release the lock, it ends up running with an effective priority of 2. This
            is known as priority inversion.



                                             LOVELY PROFESSIONAL UNIVERSITY                                   101
   103   104   105   106   107   108   109   110   111   112   113