8. priority scheduling and synchronization · 2019. 6. 4. · priority scheduling-synchronization...
Post on 26-Oct-2020
5 Views
Preview:
TRANSCRIPT
8. Priority Scheduling and Synchronization
1Memory Management
Priority Scheduling-Synchronization 개요
여러 스레드가 lock, semaphore, condition variable 을 얻기 위해 기다릴
경우 우선순위가 가장 높은 thread가 CPU를 점유 하도록 구현
현재 pintos는 semaphore를 대기 하고 있는 스레드들의 list인 waiters가
FIFO로 구현되어있다
수정해야 할 주요 파일
thread.*, synch.*
2Memory Management
Priority Scheduling-Synchronization
우선순위를 무시하고 waiters list에 삽입되는 순서대로 lock을 획득
3
waiters
Priority
time
Lock 획득
Lock 요청
Lock 반환
Lock 획득
Lock 획득
Lock 반환
tail head
Memory Management
Semaphore를 요청 할때 대기 리스트를 우선순위로 정렬
sema_down()에서 waiters list를 우선수위로 정렬 하도록 수정
waiters
tail head
Priority Scheduling-Synchronization 솔루션
4
Priority
time
Lock 획득
Lock 요청
Lock 반환
Lock 반환
Lock획득 Lock 획득
Lock 반환
Lock 획득
Memory Management
Pintos 의 semaphore
void sema_init(struct semaphore *sema, unsigned value)
semaphore를 주어진 value로 초기화
void sema_down(struct semaphore *sema)
semaphore를 요청하고 획득했을 때 value를 1 낮춤
void sema_up(struct semaphore *sema)
semaphore를 반환하고 value를 1 높임
5
struct semaphore {
unsigned value; /* Current value. */
struct list waiters; /* List of waiting
threads. */
};
pintos/src/threads/synch.h
Memory Management
Pintos 의 lock
void lock_init (struct lock *lock)
lock 자료구조를 초기화
void lock_acquire (struct lock *lock)
lock을 요청
void lock_release (struct lock *lock)
lock을 반환
6
struct lock
{
struct thread *holder; /* Thread holding lock */
struct semaphore semaphore; /* Binary semaphore
controlling access. */
};
pintos/src/threads/synch.h
Memory Management
Pintos 의 condition variable
a
void cond_init(struct condition *cond)
condition variable 자료구조를 초기화
void cond_wait(struct condition *cond, struct lock *lock)
condition variable을 통해 signal이 오는지 기림
void cond_signal(struct condition *cond,
struct lock *lock UNUSED)
condition variable에서 기다리는 가장높은 우선순위의 스레드에 signal을 보냄
void cond_broadcast(struct condition *cond, struct lock *lock)
condition variable에서 기다리는 모든 스레드에 signal을 보냄
7
struct condition {
struct list waiters; /* List of waiting threads. */
};
pintos/src/threads/synch.h
Memory Management
Priority Scheduling-Synchronization 구현
sema_down() 함수 수정
Semaphore를 얻고 waiters 리스트 삽입 시, 우선순위대로 삽입되도록 수정
88
void sema_down (struct semaphore *sema)
{
…
/* waiters 리스트 삽입 시, 우선순위대로 삽입되도록 수정 */
…
}
pintos/src/threads/synch.c
Memory Management
Priority Scheduling-Synchronization 구현 (Cont.)
sema_up() 함수 수정
9
void sema_up (struct semaphore *sema)
{
…
if (!list_empty (&sema->waiters))
{
/* 스레드가 waiters list에 있는 동안 우선순위가 변경 되었을경우를 고려 하여 waiters list 를 우선순위로 정렬 한다. */
thread_unblock (list_entry (list_pop_front
(&sema->waiters), struct thread, elem));
}
sema->value++
/* priority preemption 코드 추가*/
…
}
pintos/src/threads/synch.c
Memory Management
Priority Scheduling-Synchronization 구현 (Cont.)
구현할 함수 선언
10
bool cmp_sem_priority (const struct list_elem *a,
const struct list_elem *b,
void *aux);
pintos/src/threads/synch.h
Memory Management
Priority Scheduling-Synchronization 구현 (Cont.)
cmp_sem_priority() 함수 추가
첫 번째 인자의 우선순위가 두 번째 인자의 우선순위보다 높으면 1을 반환 낮으면 0을
반환
11
bool cmp_sem_priority (const struct list_elem *a, const struct
list_elem *b, void *aux UNUSED){
struct semaphore_elem *sa = list_entry(a,
struct semaphore_elem, elem);
struct semaphore_elem *sb = list_entry(b,
struct semaphore_elem, elem);
/* 해당 condition variable 을 기다리는 세마포어 리스트를
가장 높은 우선순위를 가지는 스레드의 우선순위 순으로 정렬하도록 구현 */
}
pintos/src/threads/synch.c
Memory Management
Priority Scheduling-Synchronization 구현 (Cont.)
cond_wait() 함수 수정
condition variable의 waiters list에 우선순위 순서로 삽입되도록 수정
12
void cond_wait (struct condition *cond, struct lock *lock)
{
…
/* condition variable의 waiters list에 우선순위 순서로삽입되도록 수정 */
…
}
pintos/src/threads/synch.c
Memory Management
Priority Scheduling-Synchronization 구현 (Cont.)
cond_signal() 함수 수정
condition variable의 waiters list를 우선순위로 재 정열
대기 중에 우선순위가 변경되었을 가능성이 있음
13
void cond_signal (struct condition *cond, struct lock *lock UNUSED)
{
…
if (!list_empty (&cond->waiters))
{
/* condition variable의 waiters list를 우선순위로 재 정열 */
sema_up (&list_entry (list_pop_front (&cond->waiters),
struct semaphore_elem, elem)->semaphore);
}
}
pintos/src/threads/synch.c
Memory Management
결과
$ make check
14Memory Management
수정 및 추가 함수
void sema_down (struct semaphore *sema)
void sema_up (struct semaphore *sema)
void cond_wait (struct condition *cond, struct lock *lock)
void cond_signal (struct condition *cond,
struct lock *lock UNUSED)
bool cmp_sem_priority(const struct list_elem *a,
const struct list_elem *b,
void *aux UNUSED)
/* 첫번째 인자로 주어진 세마포어를 위해 대기 중인 가장 높은 우선순위의
스레드와 두번째 인자로 주어진 세마포어를 위해 대기 중인 가장 높은
우선순위의 스레드와 비교 */
15Memory Management
top related