Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Windows, how does console window ownership work?

When a console application is started from another console application, how does console ownership work?

I see four possibilities:

  1. The second application inherits the console from the first application for its lifetime, with the console returning to the original owner on exit.
  2. Each application has its own console. Windows then somehow merges the content of the two into what the "console" visible to the user
  3. The second application get a handle to the console that belongs to the first application.
  4. The console is placed into shared memory and both applications have equal "ownership"

It's quite possible that I missed something and none of these four options adequately describe what Windows does with its consoles.

If the answer is close to option 4. My follow-up question is which of the two processes is responsible for managing the window? (Handling graphical updates when the screen needs to be refreshed / redrawn, etc)

A concrete example: Run CMD. Then, using CMD, run [console application]. The [console application] will write to what appears to be the same console window that CMD was using.

like image 651
shroudednight Avatar asked May 11 '10 00:05

shroudednight


People also ask

How does the Windows console work?

Windows Console is the infrastructure for console applications in Microsoft Windows. An instance of a Windows Console has a screen buffer and an input buffer. It allows console apps to run inside a window or in hardware text mode (so as to occupy the entire screen).

What is Windows console application?

A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command-line interface of some operating systems (Unix, DOS, etc.) or the text-based interface included with most graphical user interface (GUI) operating systems, such as the Windows Console ...

Is there a console in Windows?

The Windows Console is available from the Start Button on the Taskbar; it is called 'Command Prompt' or 'MSDOS Prompt' depending on the OS; the almost undecipherable icon actually contains the letters of "MSDOS". Click on the menu, and a Console window will appear.

What is a console screen?

Console is a basic computer or monitor and keyboard that is connected to another computer, server, or a mainframe over a network.


2 Answers

None of your four possibilities is actually the case, and the answer to your follow-on question, "Which of the two processes is responsible for managing the window?", is that neither process is responsible. TUI programs don't have to know anything about windows at all, and, under the covers, aren't necessarily even plumbed in to the GUI.

Consoles are objects, accessed via handles just like files, directories, pipes, processes, and threads. A single process doesn't "own" a console via its handle to it any more than a process "owns" any file that it has an open handle to. Handles to consoles are inherited by child processes from their parents in the same way that all other (inheritable) handles are. Your TUI application, spawned by CMD, simply inherits the standard handles that CMD said that it should inherit, when it called CreateProcess() — which are usually going to be CMD's standard input, output, and error (unless the command-line told CMD to use some other handles as the child's standard input, output, and error).

Consoles aren't dependent upon CMD. They exist as long as there are (a) any open handles to the console's input or output buffers or (b) any processes otherwise "attached" to the console. So in your example you could kill CMD, but only when you terminated the child process too would the console actually be destroyed.

The process that is in charge of displaying the GUI windows in which consoles are presented is, in Windows NT prior to version 6.1, CSRSS, the Client-Server Runtime SubSystem. The window handling code is in WINSRV.DLL, which contains the "console server" that — under the covers — Win32 programs performing console I/O make LPC calls to. In Windows NT 6.1, this functionality, for reasons covered by Raymond Chen, moved out of CSRSS into a less-privileged process that CSRSS spawns.

like image 147
JdeBP Avatar answered Oct 13 '22 00:10

JdeBP


My guess is somewhere between 3 and 4. The console is a self-standing object, which has standard input, output and error streams. These streams are attached to the first process that uses the console. Subsequent processes can also inherit these streams if not redirected (e.g. running a command with redirect to a file.)

Normally there is no contention, since parent processes usually wait for their child process to complete, and asynchronous processes typically start their own console (e.g. try "start cmd" in a command prompt) or redirect standard output.

However, there is nothing to stop both processes writing to the output stream at the same time - the streams are shared. This can be a problem when using some runtime libraries since writes to standard output/error may not be immediately flushed, leading to mixed garbled output. In general, having to processes actively writing to the same output stream is usually not a good idea, unless you take measures to co-ordinate their output through concurrency primitives like Mutexes, Events and the like.

like image 30
mdma Avatar answered Oct 13 '22 00:10

mdma