카테고리 338 Synchronization with OS Support : Mutex Mutex 운영체제가 동기화를 위해 제공하는 primitive 이다. POSIX APIs Mutex를 위해 다음과 같은 POSIX API가 제공된다. int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr) int pthread_mutex_destory(pthread_mutex_t *mutex) int pthread_mutex_lock(pthread_mutex_t *mutex) semaphore의 wait과 유사 lock을 잡지 못하면 block 된다. int pthread_mutex_unlock(pthread_mutex_t *mutex) semaphore의 signal과 유사 int pthread_mutex.. 2021. 6. 20. Synchronization with OS Support : Semaphore Semaphore Semaphore는 synchronization primitive이다. 일반적인 동기화 문제를 해결하는데 primitive 한 수단이다. Bakery 알고리즘이나 peterson의 알고리즘은 synchronization 문제를 해결할 수는 있으나 알고리즘이 복잡하기 때문에 흔히 사용되지는 않는다. 일반적인 synchronization 문제의 경우 semaphore을 이용해서 해결한다. APIs Semaphore는 정수형 변수로, 운영체제가 특별하게 보호하는 변수이다. 때문에 임의로 접근하지 못하고 운영체제가 제공하는 api를 이용해서 접근해야 한다. 이런 api는 atomic하게 동작하며, 작동 중에는 다른 함수가 개입하지 못한다. wait(S) semaphore가 양수이면 1 감소한다.. 2021. 6. 20. Synchronization with Hardware Support Synchronization with Hardware Support 다음과 같은 방법이 있다. interrupt disabling : disable/enable INT test-and-set : Intel에서 제공 swap : ARM에서 제공 Interrupt Disabling Critical section에서 disable INT (interrupt)를 해서 race condition이 일어나지 않게 한다. CPU가 1개라면 race condition은 스케줄링에 의해 발생한다. Critical section의 값이 바뀌기 전에 스케줄링이 일어나서 발생하는 것이다. 그리고 스케줄링은 대부분 timmer interrupt에 의해 발생한다. 따라서 critical section에서 disable INT (.. 2021. 6. 19. Synchronization Algorithm Synchronization Algorithm 동기화를 위한 다음과 같은 알고리즘이 있다. flag 이용 turn 이용 peterson's algorithm : flag + turn 이용 bakery algorithm by lamport : 번호표 이용 Mutual Exclusion 만족 1 Mutual Exclusion을 만족시키기 위해서 flag 변수를 사용한다. boolean flag[2]; 프로세스마다 flag를 배정한다. 초기값은 false로 설정하고 critical section에 들어가기 전에 true로 설정한다. critical section에 들어가기 전에 자신의 flag를 true로 바꾸고, (flag[i] = true;) 다른 프로세스가 critical section에 들어가있다면 나.. 2021. 6. 19. Synchronization Synchronization cooperating process나 thread의 목적은 resource를 공유하고, 더 빠르게 작동하고, modular 방식에서 시스템을 동작하는 것이다. Shared resource에 동시에 접근하는 것은 non-deterministic 하거나 incorrect result를 초래할 수 있다. 프로세스나 스레드의 실행 순서에 따라서 프로그램 state나 output이 달라질 수 있다. 이를 방지하기 위해서 synchronization을 해야 한다. Critical Section Problem Race Condition 여러 프로세스가 동시에 shared resource에 접근해서 값을 바꾸려는 상황을 말한다. 최종적인 결과는 프로세스의 실행 순서에 따라서 달라진다. Ra.. 2021. 6. 19. 이전 1 ··· 37 38 39 40 41 42 43 ··· 68 다음