학교공부/운영체제

[운영체제] Synchronization Examples

Dev_Camp 2023. 4. 24. 03:02

1. Bounded buffer problem

  • Semaphore mutex는 버퍼 풀 접근에서 mutual exclusion이 적용되도록 하기 위해 사용
  • Semaphore full은 채워진 버퍼의 개수를 세줌(0개로 초기화)
  • Semaphore empty는 빈 버퍼의 개수를 세줌(n개로 초기화)

 

 

2. Readers-writers problem

  • Readers는 여러 명이 같이 읽을 수 있지만 writer는 한명 만 write할 수 있음
  • The first readers-writers problem: reader에게 우선권을 줌(writer가 starvation할 수 있음)

Read_count: 실행중인 reader의 개수

Semaphore mutex: read_count mutual exclusion될 수 있게 해줌

Semaphore rw_mutex: reader, writer 전체 mutual exclusion

          

3.  Dining-philosophers problem

가운데에 resource가 있고 각자 양쪽에 젓가락이 있는데 젓가락 두 쪽을 가진 얘만 resource에 접근할 수 있음

 

 

 

 

Synchronization in linux

  • 2.6버전부터는 완전히 preemptive하게 바뀜
  • Semaphore / atomic integers / spinlocks / reader-writer version of both를 제공
  • 단일 CPU 시스템에서 spin lock은 preemption 사용 및 미사용으로 대체됨

 

 

Named & Unnamed Semaphore

Unnamed semaphorefork()로 인해 생성된 프로세스, 단일 프로세스에서 메모리나 스택을 공유하는 스레드끼리 등 서로 semaphore 주소를 알고 있을 경우 사용될 수 있다. 반면 Named semaphore는 완전히 관련 없는 프로세스 간에도 사용될 수 있다.