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


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

Overlapped I/O

One of the key features of multitasking is that each process has its own adddress
space. Sometimes, however, it would be nice to able to do several things at once
within a process. Consider a program that copies from a hard hard disk to a diskette
(for example, a backup program).

The obvious program reads from disk, perhaps does a little processing, then writes
to the diskette, then reads from disk, etc. If it takes 1 minute to read all of the data
from the hard disk, 5 seconds to do whatever processing is required, and 2 minutes
to write it to diskette, then the whole process will take 3 minutes, 5 seconds.

However, we are leaving our resources(CPU, hard disk, diskkette drive) idle for
significant periods. Is there a way we can get higher utiliziation and thus speed
the process?

What we need is overlapped I/O. Overlapped I/O is exactly what it sounds like --
doing seperate I/O operations at the same time. There are two general ways to
achieve this. The first is via unpended I/O. Recall from the previous lession (I)
our saying that, when a process issues an I/O request, it becomes pended until that
request has been satisfied, allowing other process to run.

With unpended I/O, the call returns immediately, the process doesn't pend. Clearly,
the call then returns before I/O has been completed, so the process needs some means
to determine when the I/O is complete. This generally is accomplished through a
pending call that waits for any of the I/O operations issued to complete.

It returns some identifier for the request that has completed, and the process proceeds.
Managing such an arrangement requires the programmer to do a lot of work and do
a lot of bookkeeping.

A mere general approach to obtaining overlapped I/O (and the subject of this lessons)
is multithreading. Threads, sometimes referred to as 'lightweight processes', are like
processes in that they are schedulable entities. However, all of the threads that make
up a process share both the same address space and most other resources, such as
file I/O channels.

Thus, with multithreading, we have the power of doing many things at the same time
without the expense of multiple address spaces and interprocess communication(IPC).

Threads in the same process can communicate through memory, because they share
an adress space. Each thread in a process has its own stack area. This is not to say
that an errant thread can't write to another task's stack, but there is an area of
memory set aside for each thread.

There are two approaches to scheduling in a multithreaded system. One approach is
'two-level scheduling'. The scheduler first must choose a process to run, then it
chooses a thread within that process. In OS/2, a simpler approach is used: the scheduler
chooses threads directly.

Multithreading gives the programmer a tremendous amount of power. Along with power
comes responsibility. When you have several threads running at the same time, sharing
the same memory, a whole new dimension of bugs arises. This is the topic of the next
lessions.

To be continued.


Fri 19 Dec 2003 16:34 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.