728x90
반응형
2._tmain_STDIO의 파일함수로 파일을 카피하자(TCHAR).cpp
#include "MySystemError.h"
#define MAXSTRING 100
enum{READ,WRITE }; //#define READ 0 #define WRITE 1
int _tmain(void)
{
_tsetlocale(LC_ALL, _T("korean"));
//1.copy할 파일의 이름을 저장한다.
TCHAR filename[2][MAXSTRING] = { 0 };
if (!SelectOpenFile(filename[READ])) return -1;
if (!SelectSaveFile(filename[WRITE])) return -1;
for (int i = 0; i < 2; i++)
_tprintf(_T("[ %s ]\n"), filename[i]);
//2.파일을 연다
FILE* file[2] = { 0 };
_tfopen_s(&file[READ], filename[READ], _T("rb"));
_tfopen_s(&file[WRITE], filename[WRITE], _T("wb"));
int fileSize = MyFileSize(file[READ]);
int result = -1,currentPosition=0;
//3.읽을 파일에서 파일을 읽고 쓸 파일에 파일을 쓴다.
char buffer[MAXSTRING] = { 0 };
while (1)
{
if (currentPosition >= fileSize) break;
result = fread(buffer, sizeof(char), MAXSTRING, file[READ]);
result = fwrite(buffer, sizeof(char), result, file[WRITE]);
currentPosition += result;
}
//4.파일을 닫는다.
fclose(file[READ]);
fclose(file[WRITE]);
return 0;
}
3._tmain_시스템 함수를 써서 Copy해보자.cpp
#include "MySystemError.h"
#define MAXSTRING 100
enum { READ, WRITE }; //#define READ 0 #define WRITE 1
int _tmain(void)
{
_tsetlocale(LC_ALL, _T("korean"));
//1.copy할 파일의 이름을 저장한다.
TCHAR filename[2][MAXSTRING] = { 0 };
if (!SelectOpenFile(filename[READ])) return -1;
if (!SelectSaveFile(filename[WRITE])) return -1;
for (int i = 0; i < 2; i++)
_tprintf(_T("[ %s ]\n"), filename[i]);
//2.파일을 연다
//FILE* file[2] = { 0 };
HANDLE hFile[2];
hFile[READ] = CreateFile(filename[READ], GENERIC_READ, 0
, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile[READ] == INVALID_HANDLE_VALUE)
SystemErrorExit(_T("CreateFile-read"));
else SystemOKMsg(_T("CreateFile-read"));
hFile[WRITE] = CreateFile(filename[WRITE], GENERIC_WRITE, 0
, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile[WRITE] == INVALID_HANDLE_VALUE)
SystemErrorExit(_T("CreateFile-WRITE"));
else SystemOKMsg(_T("CreateFile-WRITE"));
//_tfopen_s(&file[READ], filename[READ], _T("rb"));
//_tfopen_s(&file[WRITE], filename[WRITE], _T("wb"));
DWORD fileSize = GetFileSize(hFile[READ], NULL);//MyFileSize(file[READ]);
DWORD result = 2, currentPosition = 0,realReadByte=0,realWriteByte=0;
//3.읽을 파일에서 파일을 읽고 쓸 파일에 파일을 쓴다.
char buffer[MAXSTRING] = { 0 };
while (1)
{
if (currentPosition >= fileSize) break;
result = ReadFile(hFile[READ], buffer, MAXSTRING, &realReadByte, NULL);
if (!result)
{
SystemErrorMsg(_T("ReadFile"));
break;
}
//if (!realReadByte) break;//end of file
currentPosition += realReadByte;
result = WriteFile(hFile[WRITE], buffer, realReadByte, &realWriteByte, NULL);
if (!result)
{
SystemErrorMsg(_T("WriteFile"));
break;
}
//result = fread(buffer, sizeof(char), MAXSTRING, file[READ]);
//result = fwrite(buffer, sizeof(char), result, file[WRITE]);
//currentPosition += result;
}
//4.파일을 닫는다.
//fclose(file[READ]);
result = CloseHandle(hFile[READ]);
if (!result) SystemErrorMsg(_T("Closehandle-file-read"));
else SystemOKMsg(_T("CloseHandle-file-read"));
result = CloseHandle(hFile[WRITE]);
if (!result) SystemErrorMsg(_T("Closehandle-file-write"));
else SystemOKMsg(_T("CloseHandle-file-write"));
//fclose(file[WRITE]);
return 0;
}
4._tmain_시스템함수를 써서 Copy를 진행하는데(비동기적으로).cpp
#include "MySystemError.h"
#define MAXSTRING 100
enum { READ, WRITE }; //#define READ 0 #define WRITE 1
int _tmain(void)
{
_tsetlocale(LC_ALL, _T("korean"));
//1.copy할 파일의 이름을 저장한다.
TCHAR filename[2][MAXSTRING] = { 0 };
if (!SelectOpenFile(filename[READ])) return -1;
if (!SelectSaveFile(filename[WRITE])) return -1;
for (int i = 0; i < 2; i++)
_tprintf(_T("[ %s ]\n"), filename[i]);
//2.파일을 연다
//FILE* file[2] = { 0 };
HANDLE hFile[2];
hFile[READ] = CreateFile(filename[READ], GENERIC_READ, 0
, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hFile[READ] == INVALID_HANDLE_VALUE)
SystemErrorExit(_T("CreateFile-read"));
else SystemOKMsg(_T("CreateFile-read"));
hFile[WRITE] = CreateFile(filename[WRITE], GENERIC_WRITE, 0
, NULL, CREATE_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
if (hFile[WRITE] == INVALID_HANDLE_VALUE)
SystemErrorExit(_T("CreateFile-WRITE"));
else SystemOKMsg(_T("CreateFile-WRITE"));
//_tfopen_s(&file[READ], filename[READ], _T("rb"));
//_tfopen_s(&file[WRITE], filename[WRITE], _T("wb"));
DWORD fileSize = GetFileSize(hFile[READ], NULL);//MyFileSize(file[READ]);
DWORD result = 2, currentPosition = 0;//, realReadByte = 0, realWriteByte = 0;
//3.읽을 파일에서 파일을 읽고 쓸 파일에 파일을 쓴다.
char buffer[MAXSTRING] = { 0 };
OVERLAPPED ovRead = { 0 }, ovWrite = { 0 };
while (1)
{
//if (currentPosition >= fileSize) break;
result = ReadFile(hFile[READ], buffer, MAXSTRING, NULL, &ovRead);
if (!result)
{
result = GetLastError();
if(result!=ERROR_IO_PENDING)
break;
}
result = WaitForSingleObject(hFile[READ], INFINITE);
if (result != WAIT_OBJECT_0)
{
SystemErrorMsg(_T("WaitForSingleObject-read"));
break;
}
if (ovRead.Internal != 0)
{
if (ovRead.Internal == 0xC0000011L)
result = 0; //end of file
else result = ovRead.Internal;
break;
}
ovRead.Offset += ovRead.InternalHigh;
currentPosition += ovRead.InternalHigh;
result = WriteFile(hFile[WRITE], buffer, ovRead.InternalHigh, NULL, &ovWrite);
if (!result)
{
result = GetLastError();
if(result!=ERROR_IO_PENDING)
break;
}
result = WaitForSingleObject(hFile[WRITE], INFINITE);
if (result != WAIT_OBJECT_0)
{
SystemErrorMsg(_T("WaitForSingleObject-write"));
break;
}
ovWrite.Offset += ovWrite.InternalHigh;
}
//4.파일을 닫는다.
//fclose(file[READ]);
result = CloseHandle(hFile[READ]);
if (!result) SystemErrorMsg(_T("Closehandle-file-read"));
else SystemOKMsg(_T("CloseHandle-file-read"));
result = CloseHandle(hFile[WRITE]);
if (!result) SystemErrorMsg(_T("Closehandle-file-write"));
else SystemOKMsg(_T("CloseHandle-file-write"));
//fclose(file[WRITE]);
return 0;
}
5._tmain_전역변수를 공유하는 쓰레드끼리 동기화를 진행할때 쓰는 크리티컬 섹션.cpp
#include "MySystemError.h"
#define MAX 1000000
#define MAXTHREAD 10
int total; //전역변수
//DWORD WINAPI ThreadFunc(LPVOID lpParameter);
unsigned int _stdcall ThreadFunc(void* lpParameter);
//HANDLE hMutex; //키 Locked 커널내부에 생기는 커널 오브젝트
//CRITICAL_SECTION cs; //일반 적인 애
//HANDLE hSemaphore;
HANDLE hEvent;
int _tmain(void)
{
BOOL result = FALSE;
_tsetlocale(LC_ALL, _T("korean"));
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
//hMutex = CreateMutex(NULL, FALSE, NULL);
//hSemaphore = CreateSemaphore(NULL, 1, 1, NULL);
//InitializeCriticalSection(&cs);
HANDLE hThread[MAXTHREAD];
for (int i = 0; i < MAXTHREAD; i++)
{
hThread[i] =(HANDLE)_beginthreadex(NULL, 0, ThreadFunc, NULL
, CREATE_ALWAYS, NULL);
//hThread[i] = CreateThread(NULL, 0, ThreadFunc, NULL
// , CREATE_ALWAYS, NULL);
}
SetEvent(hEvent);
result = WaitForMultipleObjects(MAXTHREAD, hThread, TRUE, INFINITE);
for (int i = 0; i < MAXTHREAD; i++)
{
result = CloseHandle(hThread[i]);
}
//CloseHandle(hMutex);
//DeleteCriticalSection(&cs);
//result = CloseHandle(hSemaphore);
result = CloseHandle(hEvent);
_tprintf(_T("total=%d\n"), total);
return 0;
}
unsigned int _stdcall ThreadFunc(void* lpParameter)
{
//WaitForSingleObject(hMutex, INFINITE);
//EnterCriticalSection(&cs);
//WaitForSingleObject(hSemaphore, INFINITE);
WaitForSingleObject(hEvent, INFINITE);
for (int i = 0; i < MAX; i++)
total++;
//ReleaseMutex(hMutex);
//LeaveCriticalSection(&cs);
//ReleaseSemaphore(hSemaphore, 1, NULL);
SetEvent(hEvent);
return 0;
}
6._tmain_세마포어를 사용해보자.cpp
#include "MySystemError.h"
#include<time.h>
//뮤텍스는 세마포어의 일종인다. count가 1인 애들이다.
//뮤텍스는 화장실인데 한칸 밖에 없어요
//세마포어는 화장실인데 여러개가 있는 애들인거죠
#define MAXTHREAD 50
#define MAXSEMAPHORE 10
HANDLE hSemaphore;
unsigned int _stdcall ThreadFunc(void* lpParameter);
int _tmain(void)
{
srand((unsigned int)time(NULL));
hSemaphore = CreateSemaphore(NULL, MAXSEMAPHORE, MAXSEMAPHORE
, NULL);
BOOL result = FALSE;
HANDLE hThread[MAXTHREAD];
_tsetlocale(LC_ALL, _T("korean"));
for (int i = 0; i < MAXTHREAD; i++)
{
hThread[i] = (HANDLE)_beginthreadex(NULL, 0, ThreadFunc, NULL
, CREATE_ALWAYS, NULL);
}
result = WaitForMultipleObjects(MAXTHREAD, hThread, TRUE, INFINITE);
for (int i = 0; i < MAXTHREAD; i++)
{
result = CloseHandle(hThread[i]);
}
result = CloseHandle(hSemaphore);
_tprintf(_T("[명동교자]가 영업을 종료했습니다.\n"));
return 0;
}
unsigned int _stdcall ThreadFunc(void* lpParameter)
{
_tprintf(_T("Wait [명동교자]...(%d)\n"), GetCurrentThreadId());
WaitForSingleObject(hSemaphore, INFINITE);
_tprintf(_T("Enter to [명동교자]...(%d)\n"), GetCurrentThreadId());
_tprintf(_T("Having a lunch at [명동교자]...(%d)\n"), GetCurrentThreadId());
Sleep(10 * (rand() % MAXTHREAD));
_tprintf(_T("Leave from [명동교자]...(%d)\n"), GetCurrentThreadId());
ReleaseSemaphore(hSemaphore, 1, NULL);
return 0;
}
7._tmain_이벤트1.cpp
#include "MySystemError.h"
#define MAXSTRING 100
TCHAR string[MAXSTRING];
unsigned int _stdcall ThreadFuncString(void* lpParameter);
unsigned int _stdcall ThreadFuncLength(void* lpParameter);
HANDLE hEvent;
int _tmain(void)
{
BOOL result = FALSE;
//TRUE = manual, FALSE - nonsignal
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
_tsetlocale(LC_ALL, _T("korean"));
HANDLE hThread[2];
hThread[0] = (HANDLE)_beginthreadex(NULL, 0, ThreadFuncString
, NULL, CREATE_ALWAYS, NULL);
hThread[1] = (HANDLE)_beginthreadex(NULL, 0, ThreadFuncLength
, NULL, CREATE_ALWAYS, NULL);
_tprintf(_T("문자열을 입력해 주세요:"));
_getts_s(string, MAXSTRING);
result = SetEvent(hEvent);
result = WaitForMultipleObjects(2, hThread, TRUE, INFINITE);
for (int i = 0; i < 2; i++)
{
result = CloseHandle(hThread[i]);
}
result = CloseHandle(hEvent);
return 0;
}
unsigned int _stdcall ThreadFuncString(void* lpParameter)
{
BOOL result = FALSE;
result = WaitForSingleObject(hEvent, INFINITE);
_tprintf(_T("입력하신 문자열은 [%s]입니다.\n"), string);
return 0;
}
unsigned int _stdcall ThreadFuncLength(void* lpParameter)
{
BOOL result = FALSE;
result = WaitForSingleObject(hEvent, INFINITE);
_tprintf(_T("입력하신 문자열은 [%d]갯수를 갖고 있습니다.\n")
, _tcslen(string));
return 0;
}
8._tmain_이벤트2.cpp
#include "MySystemError.h"
#define MAXSTRING 100
TCHAR string[MAXSTRING];
unsigned int _stdcall ThreadFuncString(void* lpParameter);
unsigned int _stdcall ThreadFuncLength(void* lpParameter);
unsigned int _stdcall ThreadFuncInput(void* lpParameter);
HANDLE hEvent;
int _tmain(void)
{
BOOL result = FALSE;
//TRUE = manual, FALSE - nonsignal
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
_tsetlocale(LC_ALL, _T("korean"));
HANDLE hThread[3];
hThread[0] = (HANDLE)_beginthreadex(NULL, 0, ThreadFuncString
, NULL, CREATE_ALWAYS, NULL);
hThread[1] = (HANDLE)_beginthreadex(NULL, 0, ThreadFuncLength
, NULL, CREATE_ALWAYS, NULL);
hThread[2] = (HANDLE)_beginthreadex(NULL, 0, ThreadFuncInput
, NULL, CREATE_ALWAYS, NULL);
result = WaitForMultipleObjects(3, hThread, TRUE, INFINITE);
for (int i = 0; i < 3; i++)
{
result = CloseHandle(hThread[i]);
}
result = CloseHandle(hEvent);
return 0;
}
unsigned int _stdcall ThreadFuncString(void* lpParameter)
{
BOOL result = FALSE;
result = WaitForSingleObject(hEvent, INFINITE);
_tprintf(_T("입력하신 문자열은 [%s]입니다.\n"), string);
return 0;
}
unsigned int _stdcall ThreadFuncLength(void* lpParameter)
{
BOOL result = FALSE;
result = WaitForSingleObject(hEvent, INFINITE);
_tprintf(_T("입력하신 문자열은 [%d]갯수를 갖고 있습니다.\n")
, _tcslen(string));
return 0;
}
unsigned int _stdcall ThreadFuncInput(void* lpParameter)
{
BOOL result = FALSE;
_tprintf(_T("문자열을 입력해 주세요:"));
_getts_s(string, MAXSTRING);
result = SetEvent(hEvent);
return 0;
}
728x90
반응형