본문 바로가기

운영체제26

6-2-2. Event 실습 배운 이론을 실습하는 과정 #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; int globalMemory = 0; HANDLE hEvent = INVALID_HANDLE_VALUE; UINT writeValueProc(void* p) { static int increase = 100; globalMemory += increase; SetEvent(hEvent); return 0; } UINT readValueProc(void* p) { //WaitForSingleObject(hEvent, INFINITE); printf("read value : %d\n", globalMemory.. 2022. 9. 20.
6-2-1. Semaphore 실습 배운 이론을 실습하는 과정 #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; HANDLE hSemaphore = INVALID_HANDLE_VALUE; int totalCount = 0; UINT threadProc(void* p) { totalCount++; int count = totalCount; WaitForSingleObject(hSemaphore, INFINITE); printf("Start Routine %d!\n", count); Sleep(3000); printf("End Routine %d!\n", count); ReleaseSemaphore(hSemaphore,.. 2022. 9. 20.
6-2. 동기화 개념과 Critical Section Critical Section n개의 쓰레드가 공통적인 변수(정적 혹은 전역)를 조작할 때 이 조작하는 공간은 1개의 쓰레드만 지나가야 한다 라고 정해놓은 게 임계영역(Critical Section)이다. 여러 흐름이 일렬로 지나가는 것이므로 직렬화 (Serialize) 된다고도 얘기한다. #define _CRT_SECURE_NO_WARNINGS #include #include #include #include using namespace std; CRITICAL_SECTION cs; UINT __stdcall foo(void* p) { static int n = 0; EnterCriticalSection(&cs); //n = 100; //임계영역 (Critical Section) LeaveCritical.. 2022. 9. 20.
6-1. Thread Basic 쓰레드란? 프로세스 안에서 코드를 실행하는 실행 흐름 프로세스를 만들면 기본적으로 주 스레드 1개가 만들어짐 스레드도 관리 대상이므로 TKO (쓰레드 커널 오브젝트)가 만들어짐 ※ 프로세스에 대한 부연설명 - 프로세스 가상 주소공간에는 다양한 모듈(실행파일, DLL)이 올라와 있음 - 프로세스를 만들면 OS가 PKO (프로세스 커널 오브젝트)로 관리 - 프로세스는 가상 주소공간 + PKO로 구성됨 - 프로세스는 정적인 존재 쓰레드를 만들면? 스택과 TKO가 만들어짐 쓰레드는 동적인 존재 (왜냐하면 직접 코드를 실행하는 실행 흐름이므로) 쓰레드와 병렬 실행 쓰레드는 주 쓰레드 외에 추가로 더 만들 수 있다. CPU가 2개가 있을 땐 2개의 쓰레드를 동시에 실행 가능 CPU가 2개 있을 때 4개의 쓰레드를 .. 2022. 9. 20.