Professional Documents
Culture Documents
Prepared By
Dipan A Parikh
Roll No. 200
Contents
o Introduction
o Definition – Multitasking
o How Multitasking works
o Types of Multitasking
o Task structure
o Introduction to task and Task
Switch
o Task Switching
Introduction
In multitasking, the CPU switches back and forth quickly
between programs, giving the appearance that all
programs are running simultaneously. In task switching,
the CPU does not switch back and forth, but executes
only one program at a time. Task switching does allow
you to switch smoothly from one program to another.
Task switching is sometimes called context switching
Multitasking
The 386, as mentioned earlier, has support for
multitasking, i.e. running several processes concurrently.
In reality, however, they do not run concurrently. It only
appears to the user as though they were all running at the
same time.
The 386 uses the Task State Segments (TSSs) to support
multitasking. The TSS descriptor points to a buffer
which must be at least 104 bytes long. In addition to
multitasking, TSSs can also be used for hardware
interrupt handling (using task gates in the IDT).
The TSS selector is neither readable nor
writeable. Generally, a TSS alias is
created, which is nothing but a "data type“
segment pointing to the TSS buffer. TSS
selectors always appear in the GDT, never
in LDT or IDT. However, as mentioned
above, task gates may appear in the IDT.
The processor uses the TSS selector
internally.
Since a multitasking switch requires the processor
state to be saved, the buffer mostly contains the
contents of the hardware registers. When a task
switch occurs, the processor saves various details in
the TSS buffer automatically. This process is very
quick and hence not many CPU cycles are wasted in
switching to a new task. Before a task is initially
started, the operating system has to fill in certain
entries in the TSS buffer.
A task switch may occur during a "far jump" or a "far
call". The offset of the call or jump is simply ignored. The
values in the TSS are loaded into the registers and the
new task beings to execute. The selector referred by the
jump or call must be a TSS selector or a task gate. The
task gate contains the TSS selector. But unlike the TSS
selector, the task gate may occur in the GDT, IDT or
LDT.
The 386 also supports nested tasks. It handles nested tasks using
the NT bit in the EFLAGS register. Whenever a task "calls"
another task, the 386 stores the old task's TSS selector in the
"back-link" field of the new TSS. Also, the NT bit in the EFLAGS
register is set. When the new task wishes to return to the old one, it
issues an IRET instruction.
The TSS aren't reentrant. Whenever a task is running, the 386 sets
the BUSY bit in the TSS selector to indicate this. This is done to
prevent recurive calling of tasks.
It is incorrect to say that a multitasking OS runs multiple
programs (i.e., tasks) simultaneously. In reality, it loads a
task into memory, permits it to run for a while and then
suspends it. It suspends the program by creating a snapshot,
or image, of all or many of the processor's registers in
memory. In the IA32 architecture, the image is stored in a
special data structure in memory referred to as a Task State
Segment (TSS) and is accomplished by performing an
automatic series of memory write transactions. In other
words, the exact state of the processor at the point of
suspension is saved in memory.
Having effectively saved a snapshot that indicates the
point of suspension and the processor's complete state at
the time, the processor then initiates another task by
loading it into memory and jumping to its entry point.
Based on some OS-specific criteria, the OS at some point
makes the decision to suspend this task as well. As before,
the state of the processor is saved in memory (in this task's
TSS) as a snapshot of the task's state at its point of
suspension.
At some point, the OS makes the decision to resume a
previously-suspended task. This is accomplished by
reloading the processor's registers from the
previously-saved register image (i.e., its TSS) by
performing a series of memory read transactions. The
processor then uses the address pointer stored in the
CS:EIP register pair to fetch the next instruction,
thereby resuming program execution at the point
where it had been suspended earlier.
The criteria that an OS uses in making the decision to suspend a
program is specific to that OS. It may simply use timeslicing—
each program is permitted to execute for a fixed amount of time
(e.g., 10ms). At the end of that period of time, the currently
executing task is suspended and the next task in the queue is
started or resumed. The OS may assign priority levels to
programs, thereby permitting a higher priority program to
"preempt" a lower priority program that may currently be
running.
This is referred to as preemptive multitasking. The
OS would also choose to suspend the currently
executing program if the program needs something
that is not immediately available (e.g., when it
attempts an access to a page of information that is
currently not in memory, but resides on a mass
storage device).
TASK STRUCTURE
A task is made up of two parts: a task execution space and a task-
state segment (TSS). The task execution space consists of a code
segment, a stack segment, and one or more data segments If an
operating system or executive uses the processor’s privilege-level
protection mechanism, the task execution space also provides a
separate stack for each privilege level. The TSS specifies the
segments that make up the task execution space and provides a
storage place for task state information. In multitasking systems, the
TSS also provides a mechanism for linking tasks.
The following items define the state of the currently executing task:
The task’s curent execution space, defined by the segment selectors in the
segment
registers (CS, DS, SS, ES, FS, and GS).
The state of the general-purpose registers.
The state of the EFLAGS register.
The state of the EIP register.
The state of control register CR3.
The state of the task register.
The state of the LDTR register.
The I/O map base address and I/O map (contained in the TSS).
Stack pointers to the privilege 0, 1, and 2 stacks (contained in the TSS).
Link to previously executed task (contained in the TSS).
Prior to dispatching a task, all of these items are contained in the task’s TSS,
except the state of the task register. Also, the complete contents of the LDTR
register are not contained in the TSS, only the segment selector for the LDT.
Software or the processor can dispatch a task for execution in one of the
following ways:
A explicit call to a task with the CALL instruction.
A explicit jump to a task with the JMP instruction.
An implicit call (by the processor) to an interrupt-handler task.
An implicit call to an exception-handler task.
A return (initiated with an IRET instruction) when the NT flag in the
EFLAGS register is set.
When a task is dispatched for execution, a task switch automatically occurs
between the currently running task and the dispatched task. During a task
switch, the execution environment
of the currently executing task (called the task’s state or context) is saved in
its TSS and execution
of the task is suspended. The context for the dispatched task is then loaded
into the processor and execution of that task begins with the instruction
pointed to by the newly loaded EIP
register. If the task has not been run since the system was last initialized, the
EIP will point to
the first instruction of the task’s code; otherwise, it will point to the next
instruction after the last
instruction that the task executed when it was last active.
Task Switching
The processor transfers execution to another task in any of
four cases:
The current program, task, or procedure executes a JMP or
CALL instruction to a TSS descriptor in the GDT.
The current program, task, or procedure executes a JMP or
CALL instruction to a task-gate descriptor in the GDT or
the current LDT.
An interrupt or exception vector points to a task-gate
descriptor in the IDT.
The current task executes an IRET when the NT flag in the
EFLAGS register is set.