January 21, 2006
When does a program terminate?
This simple question isn´t so simple as it seems to be!
In the Win32-Documentation you can find the following:
How Processes are Terminated
A process executes until one of the following events occurs:
How Processes are Terminated
A process executes until one of the following events occurs:
- Any thread of the process calls the ExitProcess function.
- The primary thread of the process returns.
- The last thread of the process terminates.
- Any thread calls the TerminateProcess function with a handle to the process.
- For console processes, the default console control handler calls ExitProcess when the console receives a CTRL+C or CTRL+BREAK signal.
- The user shuts down the system or logs off.
And this list is wrong. Do you see the wrong point??? It is The primary thread of the process returns
This is simply wrong. You can prove it if you write a simple test-application:
#include <windows.h>
#include <tchar.h>
#pragma comment(linker, "/entry:myMain")
DWORD WINAPI MyThread2(LPVOID)
{
TCHAR szText[] = _T("I am in Thread 2!n");
while(1)
{
Sleep(1000);
DWORD dwWritten;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szText, _tcslen(szText), &dwWritten, NULL);
}
return 0;
}
int WINAPI myMain()
{
DWORD dwThreadId;
CloseHandle(CreateThread(NULL, 0, MyThread2, NULL, 0, &dwThreadId));
return 0;
}
The program will run forever, even if the “primary thread” has returned (or you can also call “ExitThread”).
You need to remember: The documentation talks about Win32-API. If you write a program which uses the CRT (C-Runtime), then it will terminate if you return from ”_tmain”... but not because the WinAPI terminates your process, instead the CRT calls ExitProcess and does not return from the entry-thread.
After digging a bit in the source, in found no hint that the OS is doing anything special with the “initial-thread” (the entry-thread is so called in comments in the source) expect it uses it to start the entry-point. If the entry-points returns, the _BaseProcessStart function will just call “ExitThread”. And in ExitThread there is only a query for “ThreadAmILastThread”. And only if this is the last thread, ExitThread calls ExitProcess.
So the WinAPI-Documentation is wrong about: “The process will terminate if the primary thread of the process returns.
This is only the case if you use the CRT. It would be great if MS could add a comment about this behaviour in the documentation!
The trackback url for this post is http://blog.kalmbachnet.de/bblog/trackback.php/65/
Posted 1 year, 11 months ago by Scherbina Vladimir • • www • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/358/
Posted 1 year, 11 months ago by Jochen Kalmbach • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/360/
Posted 1 year, 11 months ago by Scherbina Vladimir • • www • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/361/
Posted 1 year, 11 months ago by Jochen Kalmbach • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/362/
Posted 1 year, 11 months ago by Scherbina Vladimir • • www • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/363/
Posted 1 year, 11 months ago by Jochen Kalmbach • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/364/
Posted 1 year, 11 months ago by Scherbina Vladimir • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/365/
Posted 1 year, 11 months ago by Jochen Kalmbach • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/366/
Posted 1 year, 11 months ago by Okko Willeboordse • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/368/
Posted 1 year, 11 months ago by Jochen Kalmbach • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/65/369/
Comments have now been turned off for this post