Page 322 - DCAP403_Operating System
P. 322
Unit 14: Windows 2000
In addition to sending a message, a thread can also send data with the message. Since processes Notes
have separate address spaces, the data must be copied. The system copies them by calling
endMessage to send a message of type WM COPYDATA with a COPYDATASTRUCT data
structure that contains the length and address of the data to be transferred. When the message is
sent, Windows 2000 copies the data to a new block of memory and gives the virtual address of
the new block to the receiving process.
Unlike the 16-bit windows environment, every Win32 thread has its own input queue from
which the thread receives messages. (All input is received via messages.) This structure is more
reliable than the shared input queue of 16-bit windows, because, with separate queues, one stuck
application cannot block input to the other applications.
14.6.3 Memory Management
TheWin32 API provides several ways for an application to use memory: virtual memory,
memory-mapped FIles, heaps, and thread-local storage.
An application calls VirtualAlloc to reserve or commit virtual memory, and VirtualFree to
decommit or release the memory. These functions enable the application to specify the virtual
address at which the memory is allocated.
They operate on multiples of the memory pagesize, and the starting address of an allocated
region must be greater than 0_10000.
A process may lock some of its committed pages into physical memory by calling VirtualLock.
The maximum number of pages that a process can lock is 30, unless the process FIrst calls
SetProcessWorkingSetSize to increase the minimum working-set size.
Another way for an application to use memory is by memory mapping a file into its address space.
Memory mapping is also a convenient way for two processes to share memory: Both processes
map the same file into their virtual memory. Memory mapping is a multistage process.
If a process wants to map some address space just to share a memory region with another process,
no file is needed. The process can call CreateFileMapping with a file handle of off 0×ffffffff and a
particular size. The resulting file-mapping object can be shared by inheritance, by name lookup,
or by duplication.
The third way for applications to use memory is a heap. A heap in the Win32 environment is just
a region of reserved address space. When a Win32 process is initialized, it is created with a 1-MB
default heap. Since many Win32 functions use the default heap, access to the heap is synchronized
to protect the heap’s space-allocation data structures from being damaged by concurrent updates
by multiple threads. Win32 provides several heap-management functions so that a process can
allocate and manage a private heap. These functions are HeapCreate, HeapAlloc, HeapRealloc,
HeapSize, HeapFree, and HeapDestroy.
TheWin32 API also provides the HeapLock and HeapUnlock functions to enable a thread to gain
exclusive access to a heap. Unlike VirtualLock, these functions perform only synchronization;
they do not lock pages into physical memory.
The fourth way for applications to use memory is a thread-local storage mechanism. Functions
that rely on global or static data typically fail to work properly in a multithreaded environment.
For instance, the C run-time function strtok uses a static variable to keep track of its current
position while parsing a string. For two concurrent threads to execute strtok correctly, they
need separate .current position. variables. The thread-local storage mechanism allocates global
storage on a per-thread basis. It provides both dynamic and static methods of creating thread-
local storage.
LOVELY PROFESSIONAL UNIVERSITY 315