본문 바로가기
Computer Science/Operating System

Synchronization with OS Support : Mutex

by Gofo 2021. 6. 20.

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_trylock(pthread_mutex_t *mutex)
    • lock과 동일하지만, block 없이 즉시 return 한다.
    • 대신 return 값이 실패함을 의미한다.
    • block 하는 것 대신 유용하나 다른 작업을 할 수 있다.

 

Semaphore vs. Mutex

Binary semaphore와 유사하다.

다만 mutex는 ownership이 있다.

 

Semaphore는 내가 wait을 하더라도 다른 semaphore가 signal 하는 것이 가능하다.

 

그러나 mutex는 내가 lock을 하면 반드시 내가 unlock을 해야 한다.

다른 프로세스는 unlock 할 수 없다.

 

일반적으로 semaphore는 프로세스 간 동기화를 위해 사용한다.

Mutex는 스레드 간 동기화를 위해 사용한다.

'Computer Science > Operating System' 카테고리의 다른 글

Deadlock  (0) 2021.06.20
Synchronization Problems  (0) 2021.06.20
Synchronization with OS Support : Semaphore  (0) 2021.06.20
Synchronization with Hardware Support  (0) 2021.06.19
Synchronization Algorithm  (0) 2021.06.19

댓글