Windows系统编程chap6
Windows系统编程chap6 P134
进程管理
进程将其代码和数据存储在自己独立的虚拟地址空间里,以免受其他进程的影响。每个进程又包含一个或多个独立的线程。进程的线程可以创建新的线程以及新的独立进程,并管理对象间的通信和同步。
通过创建和管理进程,应用程序可以用多个并发任务来处理文件,执行计算或同其他网络系统进行通信。甚至可以使用多处理器来加速处理。
Windows进程和线程
从程序员角度,每个Windows进程包括如下资源组件:
一个或多个线程;
一个虚拟空间;
一个或多个代码段;
一个或多个包含全局变量的数据段;
环境字符串,
进程堆
进程的每个线程共享代码,全局变量,环境字符串和资源。每个线程都独立进行调度,并包含如下要素:
为过程调用,中断,异常处理器和自动存储建立的堆栈;
线程本地存储(TLS)—指针数组,
堆栈参数
上下文结构
《inside windows 2000》了解OS必读;
进程创建
.1 指定可执行的映像和命令行
。2 可继承句柄
进程句柄计数
BOOL GetProcessHandleCount()
进程标识
HANDLE GetCurrentProcess(VOID)
复制句柄
退出及终止进程
等待进程终止
环境块和字符串
范例:并发模式查找
/*Chap6. grepMP. Multiple process version of grep command.*/
#include "EvryThng.h"
int _tmain(DWORD argc, LPTSTR argv[])
/*Create a separate process to search each file on the command line.
Each process is given a temporary file, in the current directory, to receive the result.*/
{
HANDLE hTempFile;
//SA for inheritable handle.
SECURITY_ATTRIBUTES StdOutSA = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
TCHAR CommandLine[MAX_PATH + 100];
STARTUPINFO StartUpSearch, StartUp;
PROCESS_INFORMATION ProcessInfo;
DWORD iProc, ExCde;
HANDLE *hProc;/*Pointer to an array of proc handles.*/
typedef struct {TCHAR TempFile[MAX_PATH]} PROCFILE;
PROCFILE *ProcFile;/*Pointer to array of temp file names.*/
GetStartupInfo(&StartUpSearch);
GetStartupInfo(&StartUp);
ProcFile = malloc((argc - 2) * sizeof(PROCFILE));
hProc = malloc((argc - 2) * sizeof(HANDLE));
/*Create a separate "grep" process for each file.*/
for (iProc = 0; iProc < argc - 2; iProc++)
{
_stprintf(CommandLine, _T("%s%s %s"), _T("grep"), argv[1], argv[iProc + 2]);
GetTempFileName(_T("."), _T("gtm"), 0, ProcFile[iProc].TempFile);/*For search results.*/
/*This handle is inheritable*/
hTempFile = CreateFile(ProcFile[iProc].TempFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, &StdOutSA,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
StartUpSearch.dwFlags = STARTF_USESTDHANDLES;
StartUpSearch.hStdOutput = hTempFile;
StartUpSearch.hStdError = hTempFile;
StartUpSearch.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
/*Create a process to execute the command line.*/
CreateProcess(NULL, CommandLine, NULL, NULL, TRUE, 0, NULL, NULL, &StartUpSearch, &ProcessInfo);
/*Close unwanted handles.*/
CloseHandle(hTempFile);
CloseHandle(ProcessInfo.hThread);
hProc[iProc] = ProcessInfo.hProcess;
}
/*Process are all running. Wait for them to complete.*/
for (iProc = 0; iProc < argc - 2; iProc += MAXIMUM_WAIT_OBJECTS)
{
//Allows a large # of processes
WaitForMultipleObjects(min(MAXIMUM_WAIT_OBJECTS, argc - 2 - iProc), &hProc[iProc], TRUE, INFINITE);
}
/*Result files sent to std output using "cat."*/
for (iProc = 0; iProc < argc - 2; iProc++)
{
if (GetExitCodeProcess(hProc[iProc], &ExCde) && ExCde == 0)
{
//Pattern was detected ---List results.
if (argc > 3)
{
_tprintf(_T("%s:\n"), argv[iProc + 2]);
}
fflush(stdout);//Multiple processes use stdout.
_stprintf(CommandLine, _T("%s%s"), _T("cat"), ProcFile[iProc].TempFile);
CreateProcess(NULL, CommandLine, NULL, NULL, TRUE, 0, NULL, NULL, &StartUp, &ProcessInfo);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
}
CloseHandle(hProc[iProc]);
DeleteFile(ProcFile[iProc].TempFile);
}
free(ProcFile);
free(hProc);
return 0;
}
任务对象
Chap7 线程和调度 P161
7.1 线程概览
章线程进程的缺点:
7.2 线程基础
7.3 线程管理
createthread()
7.3.1 线程标识
其他线程管理函数
//7-10章说明如何管理和同步进程内的线程
//6创建和管理进程
Chap11 进程间通信 P246
Chap12 windows套接字进行网络编程 P 269
Chap 13 windows服务 P296
13.1 编写Windows服务概览
13.2 main()服务
新的main()函数由SCM来调用,负责用SCM来注册服务,以及启动服务控制调度程序。
使用一个或多个逻辑服务的名称和入口点来调用函数StartServiceCtrlDispatcher
#include "EvryThng.h"
void WINAPI ServiceMain(DWORD argc, LPTSTR argv[]);
static LPTSTR ServiceMain = _T("SocketCommandLineService");
/*Main routine that starts the service control dispatcher.*/
VOID _tmain(int argc, LPTSTR argv[])
{
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ ServiceName, ServiceMain},
{ NULL, NULL}
};
if (!StartServiceCtrlDispatcher(DispatchTable))
{
ReportError(_T("Failed to start srvc ctrl dis."), 1, TRUE);
}
/*ServiceMain() will not run until started by the SCM.*/
/*Return here only when all services have terminated. */
return;
}
13.3 ServiceMain()函数