Inheritance

The documentation often refers to processes as parents, children, and siblings. A process that creates other processes is called the parent, and the new processes are its children. All the children of a single parent are siblings. Once a parent creates a child, the child no longer depends on the parent. If the parent terminates, the child may continue. The Win32 subsystem does not enforce any strong hierarchical dependence between parents and children.

The familial metaphor extends to one other action: inheritance. A new process inherits some attributes from its parent. Inherited attributes include the following:

A child of a console process—that is, of a parent that uses the console API instead of the Windows GUI for drawing on the screen—also inherits its parent’s console window. A child may inherit handles to processes, threads, mutexes, events, semaphores, pipes, and file-mapping objects. It may also inherit handles created with CreateFile, including files, console input buffers, console screen buffers, serial communication devices, and mailslots.

NOTE

Consoles simulate character-based display. The Windows 98 command shells, for example, run in a console window.

When a child inherits a handle, both the parent and the child end up with handles to the same object. Whatever one does to the object affects the other. If a child’s thread waits for and acquires a shared mutex, any parent threads that wait for the same object will be blocked. If a parent writes to a file, a child using the same handle will find its file position marker has moved forward, too.

When a child inherits a handle, it really only inherits access to the object. It does not inherit handle variables. When creating a child, the parent must both arrange for inheritance and pass the handles explicitly. The handles may be passed in several ways: on the command line, through a file, or through a pipe.

An easier but less intuitive option involves the standard I/O channels. Recall that character-based C programs frequently direct their I/O through three standard channels called stdin, stdout, and stderr. Win32 processes automatically possess the same three channels, although they do not have the same predefined names. The standard I/O channels are generally useless to a GUI application because they are not actually connected to any device. (A GUI application may, however, open a console window and use its standard I/O devices there.) Normally, a child inherits the same standard I/O devices its parent uses, but when creating the child, the parent may specify a different set of handles for the child to receive. The child retrieves its inherited handles with SetStdHandle. The parent may also change one or more of its own devices before creating the child. The child inherits the changes.

Children do not inherit all kinds of objects. Memory handles and pseudohandles are excluded from inheritance, for example. Each process has its own address space, so it cannot inherit memory objects from another process. Pseudohandles, such as the value returned by GetCurrentThread, are by definition valid only in the place where they originate. Nor do children inherit DLL module handles, GDI handles, or USER handles, including HBRUSH and HWND objects. Similarly, children do not inherit their parent’s priority class. By default a child’s priority will be NORMAL_PRIORITY_CLASS. There is one exception: the IDLE_PRIORITY_CLASS is inheritable. By default, low-priority parents create low-priority children; the poor stay poor. Parents of any other priority create children of normal priority.

© 1998 SYBEX Inc. All rights reserved.