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.
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.
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.
NoteResource 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
The System Monitor starts.
This will clear the chart.
The number of threads should return to the original amount.
Answers
* To examine the effects of a multithreaded application
Answers
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.
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 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.
Figure 1.5 The scheduling process
The priority of each thread is determined by the following criteria:
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:
The real-time class (priority 16 to 31) is used for time-critical operating system tasks and generally is not used for applications.
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:
Starts the application with a priority value of 4.
Starts the application with a priority value of 7.
Starts the application with a priority value of 13.
Starts the application with a priority value of 24.
NoteDo not start an application in the real-time priority class. This can cause the operating system to become unstable.