Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Ctrl-C message delivered to a process running on windows?

I created a C# console application to handle Ctrl-C by subscribing it to Console.CancelKeyPress event. When I executed (debug version) the application, there were 14 threads already created in the process. When I pressed Ctrl-C a new, 15th thread was created and my handler was invoked. I used process explorer from sysinternals to view the state of the process.

I am curious to know the internals on how the Ctrl-C message is delivered to a process and how the additional thread gets created? I guess, even if I do not subscribe to an event, it will still create an additional thread and exit the process. How is the default mechanism for handling Ctrl-C is setup for an application.

I am a .net developer but want to understand on how the windows operating system works under the hood. The above question is only out of curiosity to learn windows operating system.

like image 869
Anand Patel Avatar asked Dec 04 '11 16:12

Anand Patel


People also ask

What does Ctrl C do in Windows?

(1) (ConTRoL-C) In a Windows PC, holding down the Ctrl key and pressing the C key copies the currently highlighted object. The Mac equivalent is Command-C. See Ctrl-V. (2) (ConTRoL-C) In a Windows PC, holding down the Ctrl key and pressing the C key cancels the running program or batch file.

How do I send CTRL C to a process in C#?

Attach the main . NET process to the console of the process that you want to signal with Ctrl + C . Prevent the main . NET process from stopping because of Ctrl + C event by disabling handling of the signal with SetConsoleCtrlHandler() .

Which signal will be emitted on hitting Ctrl C?

By default, when a console window has the keyboard focus, CTRL + C or CTRL + BREAK is treated as a signal (SIGINT or SIGBREAK) and not as keyboard input. By default, these signals are passed to all console processes that are attached to the console.


1 Answers

When Windows needs to notify a console program of an external event, there is no window message loop to send the notification to, so Windows will create a thread in the target process to execute whatever callback function is defined. The default handler for the CTRL+C event just calls ExitProcess, but hooking the CancelKeyPress event calls the Win32 SetConsoleCtrlHandler function with a handler function.

The documentation for the handler function explains how it works:

An application-defined function used with the SetConsoleCtrlHandler function. A console process uses this function to handle control signals received by the process. When the signal is received, the system creates a new thread in the process to execute the function.

Note that the thread that Windows injects into your process has a fairly small stack, so the CLR handler routine actually queues up a Threadpool work item to execute your event handler. This means that the thread injected by Windows and a worker thread could both be created, causing you to see up to 2 additional threads during the processing of the CTRL+C event.

like image 67
Gabe Avatar answered Sep 19 '22 04:09

Gabe