Information Technology Reference
In-Depth Information
to examine the addresses of variables allocated to dierent threads' staks.
Finally, you may want to be able to determine how much memory has
been allocated to your process; most operating systems have a command
or utility that an show resource consumption by currently running pro-
cesses (e.g., top in Linux, Activity Monitor in OSX, or Task Manager in
Windows.)
4.5
Asynchronous I/O and event-driven program-
ming
Although threads are a common way to express concurrency, they are not the
only way. Asynchronous I/O and event-driven programming are one popular
alternative. This approach allows a single-threaded program to cope with high-
latency I/O devices by overlapping I/O with processing and other I/O.
The basic idea is to allow a process to make a system call to issue and I/O
request but not wait for the result. At a later time the operating system provides
the result to the process by calling a signal handler, by making a callback to
code in the process, by placing the result in a queue in the process's memory, or
by storing the result in kernel memory until the process makes another system
call to retrieve it.
Example: Asynchronous disk read. Reading from disk can take tens of
milliseconds, so in Linux, rather than issuing a read() system call that blocks
until the requested blocks have been read from disk, a process can issue an
aioread() (asynchronous I/O read) system call, which tells the operating sys-
tem to initiate the read from disk but which then immediately returns. Later, the
process can call aioerror() to determine if the disk read has finished and
aioreturn() to retrieve the read's results. E.g.,
Process
aio_read()
other processing
aio_return()
OS
handler
handler
handler
Disk
read
One common design pattern allows a single thread to interleave several dif-
ferent I/O-bound tasks by waiting for several different I/O events.
Example: Web server. Consider a web server with 10 active clients. Rather
than create one thread per client and have each thread do a blocking read()
on the network connection, an alternative is for the server to have one thread
that does a select() call that blocks until any of the 10 network connections
has data available to read; when the select() call returns, it provides the
 
Search WWH ::




Custom Search