RU/2: Форум. Общение пользователей и разработчиков OS/2 (eCS). : Multithreading (I)


Список сообщений | Написать новое | Ответить на сообщение | Домой Поиск:
Предыдущее сообщение | Следующее сообщение
From : ???
To : All
Subj : Multithreading (I)

The Multithreading Concepts

At the "dawn of civilization", in the 1950s, when computers were starting to move
out of the laboratory into the real world, they did one thing at a time. One
program executed, and another program could not be started until the first had
finished. When a program needed to access an I/O device like a card reader or a
printer, the whole system waited for the I/O to complete.

It was quickly recognized that keeping the CPU effectively idle for the duration of
an I/O operation was a waste of a precious resource. Why not allow the program B
to run on the CPU while program A was waiting for I/O to complete? From this desire
multiprogramming or multitasking was born.

Multitasking allows many programs to be run concurrently on a single CPU. How does
this work? The first thing that you need is an operating system to negotiate the use
of the hardware between the various programs that are running. The part of the
operating system that decides which program to run next and runs it is called the
scheduler.

Before discussing the operation of a scheduler, we need to define what a process is.
A process is simply a running program. The term program generally is used to refer to
source code or executable machine code. Program refers to a static, unchanging entity,
whereas process refers to the program as it is running.

Just what constitutes a process? The main components of a process are an 'address
space' and a 'CPU state'. The 'address space' is an area of the system's memory that is
accessible to the program. Generally, this area is in some way protected from use by
other processes. This area, at least initially, will contain an image of the program that
the process is running. As the process executes, it modifies data in memory, and thus,
in time, the process memory deviates from the program's memory image.

In addition to an address space, a CPU state needs to be associated with a process. As
the process executes, the registers of the CPU change. One particularly important
register is the program counter(PC), which sometimes is referred to as the instruction pointer(IP). The register gives the address of the next instruction to execute in the
process. The registers are part of the state of the process. In addition to the address
space and CPU state, a process also generally has associated with it open I/O channels
and other operating system resources.

Scheduling is the act of picking a process, making its adress space available, loading its
CPU state registers into the CPU and resuming the process at ist next instruction. There
are two major classes of scheduling: preemptive and nonpreemptive.

A nonpreemptive scheduler, as the name implies, does not preempt one process to run
another. A process only loses control of the CPU(stops running to allow another process
to run) when it surrenders control to the scheduler. This surrender of control might be
as a result of an explicit call to the scheduler from the process, or it might be a result
of an I/O operation for which the process must wait.

A preemptive scheduler can take control from a process at any time. This prevents an
uncooperative process from monopolizing the CPU. A preemptive scheduler uses timer
interrupts to gain control from the process. It than can either resume that process or
pass control to another process. Processes that require CPU are called 'ready-to-run'.

Some processes are waiting for an external event such as I/O to complete or another
process to free a resource. These processes are called 'blocked', 'pended' or 'waiting'.
These terms will be used interchangeably in these following lessons.

Most operating systems do preemptive scheduling, so one can concentrate on that.
A preemptive scheduler can gain control of the CPU an any of several ways:

(I) The currently running process calls the scheduler explicitly. The currently running
process wants to give other processes the chance to run immediately, so it makes a
call to the scheduler. The CPU state of the process is saved. Another process is chosen,
its CPU state is restored and the new process is resumed.

(II) The currently running process performs a request that cannot be satisfied immedi-
ately. The currently running process makes a request for I/O of some sort(for example,
input from the user). The result of this I/O will not be avaible for some time, so the
process will not need the CPU. The state is saved and a new process is scheduled.

(III) A timer interrupt gives control to the scheduler. The currently running process has
been running on the CPU long enough that a timer interrupt has occurred. The state
has been saved as part of the interrupt. The scheduler picks a process and schedules
it.

(IV) An I/O interrupt can give control to the scheduler. An I/O request computes. The
completion is signalled by an interrupt(for example, a keyboard interrupt caused by
the user's striking a key). The completion of the I/O can make a process ready-to-run.
The scheduler picks a process from among the ready-to-run processes and schedules
it.

(to be continued)






Tue 16 Dec 2003 17:39 Mozilla/4.61 [en] (OS/2; U)




Programmed by Dmitri Maximovich, Dmitry I. Platonoff, Eugen Kuleshov.
25.09.99 (c) 1999, RU/2. All rights reserved.
Rewritten by Dmitry Ban. All rights ignored.