2016년 3월 29일 화요일
2016년 3월 27일 일요일
Closest Pair Algorithm
The closest pair of points problem is a problem of computational geometry: given n points in metric space, find a pair of points with the smallest distance between them.
- Solution
Divide and Conquer
Web Server based on MultiThread
- HTTP Request/Response Process
- Web Server is Server that transfer HTML(HyperText Markup Language) based on HTTP(HyperText Transfer Protocol). Web Server make sentence using HTTP standard.
- Running Screen
Source Code
2016년 3월 25일 금요일
Chatting Program using IOCP
- IOCP Server Model
- IOCP (Input Output Completion Port)
IOCP can process several clients request by IO Thread. IOCP can assign Threads to process after IO completed. That is, IO Information completed is enrolled to Kernel Object named Completion Port Object.
- Accept Thread (=Main Thread)
1. generate Completion Port Object
hComPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0)
2. connect Socket with Completion Port Object.
CreateIoCompletionPort((HANDLE)hClntSock, hComPort, (DWORD)handleInfo, 0);
2. connect Socket with Completion Port Object.
CreateIoCompletionPort((HANDLE)hClntSock, hComPort, (DWORD)handleInfo, 0);
- IO Threads
1. check IO completed
GetQueuedCompletionStatus(hComPort, &bytesTrans, (LPDWORD)&handleInfo, (LPOVERLAPPED*)&ioInfo, INFINITE);
(LPDWORD)&handleInfo <= CreateIoCompletionPort((HANDLE)hClntSock, hComPort, (DWORD)handleInfo, 0);
(LPOVERLAPPED*)&ioInfo <= WSARecv(handleInfo->hClntSock, &(ioInfo->wsaBuf), 1, &recvBytes, &flags, &(ioInfo->overlapped), NULL);
2. IO Process
GetQueuedCompletionStatus(hComPort, &bytesTrans, (LPDWORD)&handleInfo, (LPOVERLAPPED*)&ioInfo, INFINITE);
(LPDWORD)&handleInfo <= CreateIoCompletionPort((HANDLE)hClntSock, hComPort, (DWORD)handleInfo, 0);
(LPOVERLAPPED*)&ioInfo <= WSARecv(handleInfo->hClntSock, &(ioInfo->wsaBuf), 1, &recvBytes, &flags, &(ioInfo->overlapped), NULL);
2. IO Process
- IOCP Advantagement
1. IO don't need to compose repetitive statement in comparison with 'select' Server Model
2. OS manage sockets as enrolling socket to CP(Completion Port) Object
3. IOCP can adjust Thread number to process IO. Thus IOCP can block performance deterioration according to context switching.
4. IOCP can efficiently use CPU because server don't wait IO completion.
4. IOCP can efficiently use CPU because server don't wait IO completion.
- Running Screen
2016년 3월 17일 목요일
Return Value of 'scanf' Function
scanf 반환 값은 입력 받을 서식문자 중 성공한 서식문자의 개수를 반환합니다. 예를 들면, int a,b,c; scanf("%d %d %d", &a, &b, &c) 가 있을 때, 정수 3개 입력이 아닌 5 3 c를 입력하면 2가 반환 됩니다. 예외가 발생하면 EOF로 반환 된다고 알고 있는 사람들이 많습니다. 하지만 무조건 EOF를 반환하지 않습니다. ( #define EOF (-1) ) 문자열 충돌이나 제어 문자열의 내용이 맞지 않는 경우 등등이 예외가 발생하는데, 예외가 발생해도 성공한 입력인자의 개수들은 반환해줍니다. EOF를 반환하는 경우는 인자를 통해서 값을 하나도 전달 받지 못한 상태에서 입력 내용과 제어 문자열의 충돌도 일어나지 않고 파일의 끝을 만난 경우 함수는 EOF를 반환합니다. ex) 이 소스는 무슨 의미 일까요? for(; ~scanf("%d", &a) ; ) cf) !는 logical operator not ~는 bitwise operator not 입니다. ~(0) 하면 8비트 기준 11111111 이 되기 때문에 ~(-1) 해야 00000000이 됩니다. anwser) 인자를 통해서 값을 하나도 전달 받지 못한 상태에서 파일의 끝(입력 서식문자의 끝)을 만난 경우 EOF를 리턴하여 for문의 조건이 ~(EOF) 즉 false(0)이 되기에 for문을 빠져 나옵니다. 그러지 않으면 for문을 계속 돌게 됩니다. reference : 'C: A Reference Manual' Book |
2016년 3월 12일 토요일
Echo Server using Asynchronous Notification IO
1. Enroll Event Kernel Object to OS using WSACreateEvent()
WSACreateEvent() is manual-reset mode event object creation function
WSAEventSelect() - OS observe chage of IO according to 3th parameter state and notify user. Thus, this funtion mean that WSAEventSelect() connect event object and socket
※ WSAEventSelect(SOCKET s, WSAEVENT hEventObject, long lNetworkEvents)
(1) 's' delivered socket generate event according to 'lNetworkEvents' delivered event
(2) 'hEventObject' delivered handle change state into 'signaled'
ex)
newEvent = WSACreateEvent();
if (WSAEventSelect(hServSock, newEvent, FD_ACCEPT) == SOCKET_ERROR)
ErrorHandling("WSAEventSelect() error");
2. Socket have to connect Event Object as matching index value.
ex)
hSockArr[numOfClntSock] = hServSock;
hEventArr[numOfClntSock] = newEvent;
numOfClntSock++;
3. Check whether or not event generation is true using WSAWaitForMultipleEvents()
ex)
posInfo = WSAWaitForMultipleEvents(numOfClntSock, hEventArr, FALSE, WSA_INFINITE, FALSE);
startIdx = posInfo - WSA_WAIT_EVENT_0;
※ 'posInfo - WSA_WAIT_EVENT_0' is event generation start index value
4. Check Event Object signaled one by one using WSAWaitForMultipleEvents()
ex)
for (i = startIdx; i<numOfClntSock; i++)
{
int sigEventIdx = WSAWaitForMultipleEvents(1, &hEventArr[i], TRUE, 0, FALSE);
5. Classify and event type using WSAEnumNetworkEvents()
ex)
WSAEnumNetworkEvents(hSockArr[sigEventIdx], hEventArr[sigEventIdx], &netEvents);
6. Process event generated
FD_ACCEPT : Do exist connect request?
FD_READ : Do exist data recevied?
FD_CLOSE : Do exist disconnect request?
ex)
if (netEvents.lNetworkEvents & FD_ACCEPT)
// process content
if (netEvents.lNetworkEvents & FD_READ)
// process content
if (netEvents.lNetworkEvents & FD_CLOSE)
// process content
Source Code
WSACreateEvent() is manual-reset mode event object creation function
WSAEventSelect() - OS observe chage of IO according to 3th parameter state and notify user. Thus, this funtion mean that WSAEventSelect() connect event object and socket
※ WSAEventSelect(SOCKET s, WSAEVENT hEventObject, long lNetworkEvents)
(1) 's' delivered socket generate event according to 'lNetworkEvents' delivered event
(2) 'hEventObject' delivered handle change state into 'signaled'
ex)
newEvent = WSACreateEvent();
if (WSAEventSelect(hServSock, newEvent, FD_ACCEPT) == SOCKET_ERROR)
ErrorHandling("WSAEventSelect() error");
2. Socket have to connect Event Object as matching index value.
ex)
hSockArr[numOfClntSock] = hServSock;
hEventArr[numOfClntSock] = newEvent;
numOfClntSock++;
3. Check whether or not event generation is true using WSAWaitForMultipleEvents()
ex)
posInfo = WSAWaitForMultipleEvents(numOfClntSock, hEventArr, FALSE, WSA_INFINITE, FALSE);
startIdx = posInfo - WSA_WAIT_EVENT_0;
※ 'posInfo - WSA_WAIT_EVENT_0' is event generation start index value
4. Check Event Object signaled one by one using WSAWaitForMultipleEvents()
ex)
for (i = startIdx; i<numOfClntSock; i++)
{
int sigEventIdx = WSAWaitForMultipleEvents(1, &hEventArr[i], TRUE, 0, FALSE);
5. Classify and event type using WSAEnumNetworkEvents()
ex)
WSAEnumNetworkEvents(hSockArr[sigEventIdx], hEventArr[sigEventIdx], &netEvents);
6. Process event generated
FD_ACCEPT : Do exist connect request?
FD_READ : Do exist data recevied?
FD_CLOSE : Do exist disconnect request?
ex)
if (netEvents.lNetworkEvents & FD_ACCEPT)
// process content
if (netEvents.lNetworkEvents & FD_READ)
// process content
if (netEvents.lNetworkEvents & FD_CLOSE)
// process content
Source Code
2016년 3월 8일 화요일
Chatting Server based on MultiThread
- One process can implements multitasking as using multithread.
Use 'clnt_cnt, clnt_socks[]' valuables in critical section because of synchronization.
Critical section is implemented by using 'mutex'
- pthread_mutex_lock(&mutex) : critical section start
- pthread_mutex_unlock(&mutex) : critical section end
- Thread Context Switching is faster than Process Context Switching
thread share heap, data(static), code except for stack, register.
Source Code
◈ references
picture - http://fingerdev.tistory.com/24
picture - http://www.ibm.com/
피드 구독하기:
글 (Atom)