Multitasking

Before you continue with the lesson, run the Chap01a.exe animation located on the CD-ROM that accompanies this book. This animation explains multitasking.

Multitasking is the ability of an operating system to run more than one program, or task, at the same time. Multitasking contrasts with single tasking, where one process must be completed before another can begin. MS-DOS is a single-tasking environment, while Windows 95 and Windows NT are both multitasking environments.

In a multitasking system, multiple processes do not actually run at the same time. Instead, the processor can switch between multiple processes. Because of this ability, you can execute a query in a database and switch to a word processing program until the results of the query are returned. Multitasking also allows the computer to use the time it would otherwise spend waiting for I/O devices or user input.

To understand multitasking, you must understand processes and threads.

Processes and Threads

An application written for Windows consists of one or more processes. A process, in the simplest terms, is an executing program. A process has an address space and resources assigned to it, and one or more threads run in the context of the process.

A thread is the basic unit to which the operating system allocates processor time and is the smallest piece of code that can be scheduled for execution. It is the actual component of a process that is executing at one time. A thread runs in the address space of the process and uses resources allocated to the process.

click to view at full size.

Figure 1.3 Thread priority

A process always has at least one thread to execute code. MS-DOS and 16-bit Windows-based programs have only one thread per process. A 32-bit Windows-based program may have many threads per process.


Note

Resource ownership occurs through the process, not the threads. Threads use the resources, but the process maintains ownership. For example, if a program requests use of a port, the process controls that port. Any of that process' threads may use the port, but a thread may not request the port.


* To examine threads

  1. Close all running programs.
  2. On the Windows Start menu, point to Programs , Accessories , System Tools , and then click System Monitor .

    The System Monitor starts.

  3. On the Edit menu, click Remove Item .
  4. Select all previously added items and click OK .

    This will clear the chart.

  5. On the View menu, click Numeric Charts .
  6. On the Edit menu, click Add Item .
  7. From Category , select Kernel and from Item , select Threads then click OK .
  8. How many threads are active?
  9. From the Windows Start menu, point to Programs then Accessories and click WordPad .
  10. Switch to the System Monitor .
  11. How many threads are active?
  12. From the WordPad toolbar, click Open .
  13. Switch to System Monitor and watch the Kernel:Threads box for a few seconds.
  14. How many threads are active?
  15. Cancel WordPad's Open dialog and close WordPad.
  16. Switch to System Monitor .

    The number of threads should return to the original amount.

  17. From the Windows Start menu, point to Programs and click MS-DOS Prompt .
  18. Switch to System Monitor .
  19. How many threads are active?
  20. Why has the MS-DOS Window created two threads?

    Answers

* To examine the effects of a multithreaded application

  1. In the WA\Practice\Ch01 folder, start the Multithreaded Demo Program ( Winbezmt.exe ).
  2. Switch to System Monitor .
  3. How many threads are active?
  4. Switch to the Multithreaded Demo Program.
  5. On the Bezier menu, select Add Thread (or CTRL+T).
  6. Switch to System Monitor .
  7. How many threads are active?
  8. Add two more threads. How many threads are active?
  9. Close all programs and the System Monitor .

    Answers

Preemptive and Cooperative Multitasking

Before you continue with the lesson, run the Chap01b.exe animation located on the CD-ROM that accompanies this book. This animation compares preemptive and cooperative multitasking, and explains how MS-DOS, 16-bit, and 32-bit programs multitask.

There are two types of multitasking: cooperative (or nonpreemptive) and preemptive. In a cooperative multitasking environment such as Windows 3.1, use of the processor is never taken from a task. Instead, a task must voluntarily yield control of the processor before any other task can run.

Preemptive multitasking differs from cooperative multitasking in that the operating system can take control of the processor without the task's cooperation. The process of taking control away from a task is called preemption. Windows 95 and Windows NT use preemptive multitasking for MS-DOS and 32-bit Windows programs.

click to view at full size.

Figure 1.4 Preemptive and cooperative multitasking

Programs running under a cooperative operating system must be specially written to cooperate in multitasking by yielding control of the processor at frequent intervals. A program that does not yield often enough causes cooperative systems to stay "locked" in that program until it does yield.

Windows NT allows preemptive multitasking of 16-bit applications for Windows and MS-DOS. Windows NT provides full memory protection for 16-bit applications because each application runs in its own virtual machine. However, Windows 95 uses cooperative multitasking with all 16-bit applications. This is necessary to maintain compatibility with 16-bit Windows-based programs that expect to control their own execution.

Scheduling

Scheduling is a process used by the operating system to determining which thread has use of the processor at any given time. Windows provides multitasking by assigning each thread a scheduling priority that allows it to use system resources. Scheduling is based on a predetermined unit of time called a time slice . The actual length of a time slice depends on the configuration of the system. The priority levels range from 0 ( lowest priority) to 31 (highest priority). The thread with the highest priority gets to use the processor.

click to view at full size.

Figure 1.5 The scheduling process

The priority of each thread is determined by the following criteria:

  • The priority class of its process.
  • The priority level of the thread within the priority class of its process.

Priority Classes

The priority class and priority levels are combined to form the base priority of a thread. Windows priority levels are divided into two classes:

  • Real-time

    The real-time class (priority 16 to 31) is used for time-critical operating system tasks and generally is not used for applications.

  • Variable priority

    The variable priority class spans priority levels 0 to 15, and is used to determine processor priority for applications. Only the zero-page thread can have a priority of 0. The zero-page thread is a system thread.

Priority Levels

The following base priority levels can be assigned to a process:

  • Low

    Starts the application with a priority value of 4.

  • Normal

    Starts the application with a priority value of 7.

  • High

    Starts the application with a priority value of 13.

  • Real-time

    Starts the application with a priority value of 24.


Note

Do not start an application in the real-time priority class. This can cause the operating system to become unstable.




Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net