728x90

 

프로세스의 핸들 정보

- PROCESS_INFORMATION에서 확인할 수 있음

typedef struct _PROCESS_INFORMATION {
    HANDLE hProcess;
    HANDLE hThread;
    DWORD dwProcessId;
    DWORD dwThreadId;
} PROCESS_INFORMATION, *PPROCESS_INFORMATION, *LPPROCESS_INFORMATION;
// operation.cpp

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

int _tmain(int argc, TCHAR * argv[])
{
	STARTUPINFO si = { 0, };
	PROCESS_INFORMATION	pi;

	si.cb = sizeof(si);	// 구조체 사이즈 설정

	TCHAR command[] = _T("operation2.exe");		// operation2.exe 실행 명령어

	CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);	// 프로세스 생성

	DWORD timing = 0;
	while (1)
	{
		for (DWORD i = 0; i < 10000; i++)
			for (DWORD i = 0; i < 10000; i++);
		_fputts(_T("operation1.exe \n"), stdout);

		timing += 1;
		if (timing == 2)
			SetPriorityClass(pi.hProcess, 	// pi.hProcess에 자식 프로세스 핸들 정보가 저장되어 있음
            NORMAL_PRIORITY_CLASS);			// 우선순위를 다시 일반으로 바꿈       
            
	}

	return 0;
}
// operation2.cpp

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


int _tmain(int argc, TCHAR* argv[])
{
	SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
    /*
	SetPriorityClass(_In_ HANDLE hProcess, _In_ DWORD dwPriorityClass);
    	SetPriorityClass() : 우선순위 변경 함수
        GetCurrentProcess() : 자신의 핸들 값 반환
        HIGH_PRIORITY_CLASS : 높은 우선순위 부여
    */
	while (1)
	{
		for (DWORD i = 0; i < 10000; i++)
			for (DWORD i = 0; i < 10000; i++);
		_fputts(_T("operation2.exe \n"), stdout);
	}

	return 0;
}

 

Usage Count

- 프로세스 커널 오브젝트에 접근 가능한 사용자 수

- 프로세스가 소멸되면 줄어듬

- 부모 프로세스가 더 이상 접근하려 하지 않을 때 UC는 0이 되어 커널 오브젝트는 사라짐

    -> closeHandle() 이용

// operation.cpp

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

int _tmain(int argc, TCHAR * argv[])
{
	STARTUPINFO si = { 0, };
	PROCESS_INFORMATION	pi;

	si.cb = sizeof(si);	// 구조체 사이즈 설정

	TCHAR command[] = _T("operation2.exe");		// operation2.exe 실행 명령어

	CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);	// 프로세스 생성

	CloseHandle(pi.hProcess);
	/*
	프로세스를 생성하지마자 자식 프로세스에 
	*/

	return 0;
}
// operation2.cpp

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


int _tmain(int argc, TCHAR* argv[])
{
	SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
    /*
	SetPriorityClass(_In_ HANDLE hProcess, _In_ DWORD dwPriorityClass);
    	SetPriorityClass() : 우선순위 변경 함수
        GetCurrentProcess() : 자신의 핸들 값 반환
        HIGH_PRIORITY_CLASS : 높은 우선순위 부여
    */
	while (1)
	{
		for (DWORD i = 0; i < 10000; i++)
			for (DWORD i = 0; i < 10000; i++);
		_fputts(_T("operation2.exe \n"), stdout);
	}

	return 0;
}

- 프로세스를 생성 후 closeHandle()을 실행하여 프로세스를 분리함

    -> Usage Count를 하나 감소 시킴

    -> 핸들 테이블에서 자식 프로세스의 핸들 정보를 삭제함

- 자식 프로세스를 종료시키는 함수가 아님

    -> 부모 프로세스는 종료되어 "계속하려면 아무 키나 누르십시오" 가 출력되지만 자식 프로세스는 실행 중임

728x90

+ Recent posts