star

搜索

RSS

RSS Link

计数器

140156

Windows进程管理实验

2011年1月25日 04:31 | Comments(87) | Category:Windows | Tags:

1.线程的创建与撤销

#include <windows.h>
#include <stdio.h>

void func();

static HANDLE h1 = NULL;
static HANDLE hHandle1 = NULL;

int main(INT argc, TCHAR* argv[])
{
	int nRetCode = 0;
	DWORD dwThreadID1;
	DWORD dRes, err;
	
	//创建一个信号量 
	hHandle1 = CreateSemaphore(NULL, 0, 1, "SemaphoreName1");
	if (hHandle1 == NULL)
		puts("Semaphore Create Fail!");
	else
		puts("Semaphore Create Success!"); 
	//打开信号量 
	hHandle1 = OpenSemaphore(SYNCHRONIZE | SEMAPHORE_MODIFY_STATE,
		NULL, "SemaphoreName1");
	if (hHandle1 == NULL)
		puts("Semaphore Open Fail!");
	else
		puts("Semaphore Open Success!"); 
	//创建子线程
	h1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func,
		 NULL, 0, &dwThreadID1); 
	if (h1 == NULL)
		puts("Thread1 create Fail!");
	else
		puts("Thread1 create Success!");
	//主线程等待子线程结束 
	dRes = WaitForSingleObject(hHandle1, INFINITE);
	err  = GetLastError();
	printf("WaitForSingleObject err = %d\n", err);
	
	if(dRes == WAIT_TIMEOUT)
		printf("TIMEOUT!dRes = %d\n", dRes);
	else if(dRes == WAIT_OBJECT_0)
		printf("WAIT_OBJECT!dRes = %d\n", dRes);
	else if(dRes == WAIT_ABANDONED)
		printf("WAIT_ABANDONED!dRes = %d\n", dRes);
	else
		printf("dRes = %d\n", dRes);
	
	CloseHandle(h1);
	CloseHandle(hHandle1);
	ExitThread(0);
		 
	return nRetCode;
}

void func()	//线程函数 
{
	BOOL rc;
	DWORD err;

	puts(" NOW IN THREAD!");
	//子线程焕醒主线程,此时同步执行 
	rc = ReleaseSemaphore(hHandle1, 1, NULL);
	err = GetLastError();
	printf("ReleaseSemaphore err = %d\n", err);
	if (rc == 0)
		puts("ReleaseSemaphore Fail!");
	else
		printf("ReleaseSemaphore Success! rc = %d\n", rc);
}

2.线程的同步

#include <windows.h>
#include <stdio.h>

void ThreadName1();

static HANDLE hHandle1 = NULL;
DWORD dwThreadID1;

int main(INT argc, TCHAR* argv[])
{
	int nRetCode = 0;
	//创建一个线程 
	hHandle1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadName1, 
	NULL, 0, &dwThreadID1);
	//挂起5秒 
	Sleep(5000);
	CloseHandle(hHandle1);
	//撤消线程 
	ExitThread(0);
	return nRetCode;
}

void ThreadName1()	//线程函数 
{
	printf("Thread is Running!\n");
}

3.线程的互斥

#include <windows.h>
#include <stdio.h>

static INT count = 5;
static HANDLE h1, h2; 
LPCRITICAL_SECTION hCriticalSection;
CRITICAL_SECTION Critical;
void func1();
void func2();

int main(INT argc, TCHAR* argv[])
{
	int nRetCode = 0;
	DWORD dwThreadID1, dwThreadID2;
	//指向临界区 
	hCriticalSection = &Critical;
	InitializeCriticalSection(hCriticalSection);
	//创建线程1 
	h1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func1,
		NULL, 0, &dwThreadID1);
	if (h1 == NULL)
		puts("Thread1 create Fail!");
	else
		puts("Thread1 create Success!");
	//创建线程 
	h2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func2,
		NULL, 0, &dwThreadID1);
	if (h2 == NULL)
		puts("Thread2 create Fail!");
	else
		puts("Thread2 create Success!");
	
	Sleep(1000);
	
	//回收处理 
	CloseHandle(h1);
	CloseHandle(h2);
	
	DeleteCriticalSection(hCriticalSection);
	ExitThread(0);
		 
	return nRetCode;
}

void func1()	//线程函数1
{
	int r1;
	
	//进入临界区 
	EnterCriticalSection(hCriticalSection);	
	r1 = count;
	Sleep(500);
	++r1;
	count = r1;
	printf("count in func1 = %d\n", count);
	//退出临界区 
	LeaveCriticalSection(hCriticalSection);
}

void func2()	//线程函数2
{
	int r2;
	
	EnterCriticalSection(hCriticalSection);
	r2 = count;
	Sleep(500);
	++r2;
	count = r2;
	printf("count in func2 = %d\n", count);
	LeaveCriticalSection(hCriticalSection);
}

4.使用命名管道实现进程通信

服务端

//Server.c
#include <windows.h>

int main(void)
{
	INT nRetCode = 0;
	INT err;
	BOOL rc;
	HANDLE hPipeHandle1;
	
	CHAR lpName[] = "\\\\.\\pipe\\myPipe";
	CHAR InBuffer[50] = "";
	CHAR OutBuffer[50] = "";
	DWORD BytesRead, BytesWrite;
	
	//创建一个命名管道 
	hPipeHandle1 = CreateNamedPipe((LPCTSTR)lpName, 
		PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | WRITE_DAC ,
		PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, 20, 30, 
		NMPWAIT_USE_DEFAULT_WAIT, NULL);
	
	if((hPipeHandle1 == INVALID_HANDLE_VALUE) || (hPipeHandle1 == NULL))
	{
		err = GetLastError();
		printf("Server Pipe Create Fail! err = %d\n", err);
		exit(1); 
	}
	else
		puts("Server Pipe Create Success!");
		
	while(TRUE)
	{
		//连接命名管道 
		rc = ConnectNamedPipe(hPipeHandle1, NULL);
		if (rc == 0)
		{
			err = GetLastError();
			printf("Server Pipe Connect Fail! err = %d\n", err);
			exit(2);
		}
		else
			puts("Server Pipe Connect Success!");
		strcpy(InBuffer, ""); strcpy(OutBuffer, "");
		//向命名管道中读数据 
		rc = ReadFile(hPipeHandle1, InBuffer, sizeof(InBuffer), &BytesRead, 
		NULL);
		if (rc == 0 && BytesRead == 0)
		{
			err = GetLastError();
			printf("Server Read Pipe Fail! err = %d\n", err);
			exit(2);
		}
		else
			printf("Server Read Pipe Success!\nDATA from Client is = %s\n", 
			InBuffer);
		rc = strcmp(InBuffer, "end");
		if (rc == 0)
			break;
		puts("Please Input Data to Send");
		scanf("%s", OutBuffer);
		//向命名管道中写数据 
		rc = WriteFile(hPipeHandle1, OutBuffer, sizeof(OutBuffer), &BytesWrite, 
		NULL);
		if (rc == 0)
			puts("Server Write Pipe Fail!");
		else
			puts("Server Write Pipe Success!");
		//拆除与命名管道的连接 
		DisconnectNamedPipe(hPipeHandle1);
		rc = strcmp(OutBuffer, "end");
		if (rc == 0)
			break;
	}
	puts("Now Server be END!");
	CloseHandle(hPipeHandle1);
	return nRetCode;
}

客户端

//Clinet.c
#include <windows.h>

int main(void)
{
	INT nRetCode = 0;
	INT err = 0;
	BOOL rc = 0;
	
	CHAR lpName[] = "\\\\.\\pipe\\myPipe";
	CHAR InBuffer[50] = "";
	CHAR OutBuffer[50] = "";
	DWORD BytesRead;
	
	while(TRUE)
	{
		strcpy(InBuffer, ""); strcpy(OutBuffer, "");
		puts("Input Data Please!");
		scanf("%s", InBuffer);
		rc = strcmp(InBuffer, "end");
		if (rc == 0)
		{
			//连接命名管道 
			rc = CallNamedPipe(lpName, InBuffer, sizeof(InBuffer), OutBuffer, 
				sizeof(OutBuffer), &BytesRead, NMPWAIT_USE_DEFAULT_WAIT);
			break;
		}
		//等待命名管道 
		rc = WaitNamedPipe(lpName, NMPWAIT_WAIT_FOREVER);
		if (rc == 0)
		{
			err = GetLastError();
			printf("Wait Pipe Fail! err = %d\n", err);
			exit(1);
		}
		else
			puts("Wait Pipe Success!");
		rc = CallNamedPipe(lpName, InBuffer, sizeof(InBuffer), OutBuffer, 
			sizeof(OutBuffer), &BytesRead, NMPWAIT_USE_DEFAULT_WAIT);
		rc = strcmp(OutBuffer, "end");	
		if (rc == 0)
			break;
		if (rc == 0)
		{
			err = GetLastError();
			printf("Pipe Call Fail! err = %d\n", err);
			exit(1);
		}
		else
			printf("Pipe Call Success!\nData from Server is %s\n", OutBuffer);
	}
	puts("Now Client to be End!");
	return nRetCode;
}