Professional Documents
Culture Documents
Operating System Test Example and Solution 2
Operating System Test Example and Solution 2
(a) (10 pts) 프로세스 간 context switching 시 위의 상황을 적절하게 지원해주기 위해 운영체제는 어떤
동작을 해야하는지 설명하시오.
가 저장된 backing store에서 page를 찾고, 2) physical memory 상의 새로운 free frame을 찾아 이를
memory에 page를 load한 후, 5) page table을 update하고, 6)process를 재개한다. 이외에도 실행중이던
(b) (10pts) (a)에서 답한 운영체제 동작에서 어떤 부하가 있는지 설명하고, 이를 어떻게 개선할 수 있을
지 제안하시오.
degradation이 발생할 수 있다. 또한 page fault가 발생하는 경우, CPU는 실행 중인 프로세스를 중단하
고 page fault handler를 실행하며, page table을 update해야 하므로 page fault가 자주 일어나는 경우
또한, page replacement가 매우 빈번하게 발생하면 CPU가 거의 대부분의 시간을 page in, page out에
- 이를 개선하기 위해서는 최적화된 page replacement algorithm을 사용해야한다. FIFO, LFU, LRU 등
용량을 늘려서 더 많은 페이지를 메모리에 상주시킴으로써 페이지 교체 빈도를 줄이는 방법도 있다.
Question 2:
[30 pts] 운영체제 커널의 코드를 학습 데이터(데이터셋)로 삼아 새로운 커널을 자동으로 구현할 수 있는 생성형 딥러
과 같다.
• LOAD 프로그램도 여러 쓰레드를 생성한다. 각 쓰레드는 생성 시 OSgptLoad 함수를 실행하는 데, data 버퍼에 비
TRAIN은 실행되면 총 10개의 쓰레드를 순차적으로 생성한다. 쓰레드 생성 시 사전에 정의된 OSgptTrain 함수를 실
행한다. TRAIN은 10개의 쓰레드가 동작을 완료할 때까지 기다린다. 이 과정을 무한 반복한다.
• 코드 구현에 필요한 헤더 파일은 모두 포함되어 있다고 가정함. 이외 코드 작성에 필요한 부분은 자유롭게 가정하고,
(b) (15pts) 이제 두 함수 OSgptTrain, OSgptLoad를 구현하고자 한다. 두 함수의 동작이 데이터셋의 관점에서
• data는 다음과 같이 미리 정의됨 (MAX: 배열 크기, Dataset: 사전에 정의한 데이터셋 struct).
int MAX;
• OSgptTrain이 읽어올 버퍼의 index로 out, OSgptLoad가 추가할 버퍼의 index로 in을 사용하시오.
• 코드 구현에 필요한 헤더 파일은 모두 포함되어 있다고 가정함. 문제에서 제시한 변수 및 동작 외에는 자유롭게 가
- 가정
페이지 크기: 4 KB
3. page replacement : 새로운 free frame이 존재하지 않는 경우, page replacement algorithm을 사용
5. 네트워크 IO : victim page를 원격 서버에 전송하기 위해 네트워크 인터페이스를 준비하고, page data
를 패킷으로 분할한다.
6. Programmed IO : CPU는 programmed IO를 사용하여 네트워크 인터페이스에 page data를 전송하고
7. 원격 서버 : 원격 서버는 수신한 패킷을 합쳐서 swap space에 page를 저장하고, 이러한 결과 응답을
9. page table update : page out이 완료되면 page fault handler는 원격 서버의 swap space 위치를
10. page in : 참조하려던 페이지를 메모리에 load하고, 로컬 swap space 또는 원격 swap space에서
page를 load한다. 네트워크 I/O를 사용하여 원격 서버에서 페이지를 가져오는 경우, 이전의
11. 프로세스 재개 : page fault handler가 완료되었음을 알리고, 프로세스가 중단된 지점에서 재개되며
(a)의 과정에서
5. 네트워크 IO 준비:
victim 페이지를 원격 서버로 전송하기 위해 네트워크 인터페이스 준비하고 페이지 데이터를 패킷으로
분할 (데이터 복사)한다.
6. Programmed IO:
CPU가 Programmed IO를 사용하여 네트워크 인터페이스에 페이지 데이터 전송한다. 데이터는 패킷으로
총 시간은 4T+4N이다.
(c) (10 pts) (a)의 과정에 폴링을 적용한다고 할 때, 어떠한 장단점이 있는지 설명하시오. 또한, 이 시스템
에 CPU는 충분하고, 메모리는 불충분하다고 할 때 인터럽트 기반의 처리와 폴링 기반의 처리 중 어떠한
방식이 적합한지 결정하고 그 이유를 설명하시오.
(a) 과정에서 폴링을 적용하게 되면 인터럽트 기반 처리보다 구현이 간단하여 시스템 복잡도를 줄일 수
있고, 주기적으로 device’s status register를 확인하기 때문에 예측 가능한 응답 시간을 가진다는 장점이
있습니다. 반면, 폴링은 CPU가 계속적으로 장치 상태를 확인하는데 소비되므로, 다른 작업을 수행하지
못하게 하여 비효율적이라는 단점이 있습니다.
위와 같이 CPU가 충분하고, 메모리가 불충분한 경우에는 메모리 자원의 효율적인 사용이 중요합니다. 또
한, 이 시스템에서는 page replacement, swap space management가 빈번하게 발생할 가능성이 높으르
모 인터럽트 기반 처리가 더 적합합니다. 왜냐하면, 인터럽트 기반 처리는 필요한 시점에만 CPU를 사용
하므로, CPU 자원을 효율적으로 사용할 수 있으며 CPU가 충분한 상황에서는 더욱 유리합니다. 또한, 메
모리가 불충분한 경우, page fault와 swap 작업이 빈번하게 발생하므로, 빠른 응답이 필요합니다. 인터럽
트 기반 처리는 폴링 기반 처리보다 일반적으로 빠른 응답을 제공합니다.
하지만, 폴링 주기가 인터럽트 기반 처리의 컨텍스트 스위칭 및 스케줄링에 걸리는 시간보다 짧다면, 폴
링 기반 처리 방식이 더 적합할 수 있습니다.
Question 4:
[15 pts] 프로세스에게 64-bit 크기의 가상 주소를 제공하는 운영체제를 설계한다. 주소 공간을 총 2^12
개의 페이지로 나누려할 때, 다음 질문에 답하시오.
(a) (5 pts) VPN과 offset 비트 수를 결정하시오.
VPN : 12
offset : 52
(b) (5 pts) Single-level 페이지 테이블을 구성하고자 한다. 이 페이지 테이블은 모든 페이지에 대한 엔트
리를 가지고 있으며, 엔트리 당 크기는 4KB이다. 이 때, 페이지 테이블의 크기를 계산하시오.
(c) (5 pts) 프로세스가 100개 실행될 때, (b)에서 설계한 page table이 소모하는 전체 메모리 크기를 계산
하시오.
로 context switch가 발생하는 경우 커널은 1) 현재 process의 base register, bound register values
(b) (8 pts) With segmentation and MMU : Describe what things the kernel should do when context
switching from one process to another to maintain segmentation.
Segmentation을 사용하는 경우, 전체 address space를 연속 할당하는 대신, address space의 각각의
logical segment를 연속 할당하는 방법입니다. Contiguous allocation과 유사하게 1) 현재 프로세스의
각각의 segment의 base register와 bound register를 PCB에 저장하고 저장하고, 이후에 restore합니다.
2) 다음 process의 경우 각 segment에 대해 contiguous allocation strategies을 이용하여 base
register와 bound reigster를 설정합니다. 이를 통해 해당 segment의 start address와 크기를 정의하고,
MMU를 update합니다.
(c) (8 pts) With paging and TLB: Describe what things the kernel should do when context switching
from one process to another to maintain paging.
1. 연속 할당:
- 복잡성: 연속 할당은 프로세스의 주소 공간을 연속적인 메모리 블록에 할당하는 방식입니다. 따라서 프
로세스의 크기가 메모리에 연속적으로 맞아야 하며, 이를 위해 메모리 내의 연속적인 빈 공간을 찾는 것
이 필요합니다.
2. 세그멘테이션:
- 복잡성: 세그멘테이션은 프로세스의 주소 공간을 논리적인 세그먼트 단위로 분할하여 관리하는 방식입
니다. 각 세그먼트는 다른 크기를 가질 수 있으며, 따라서 각 세그먼트를 메모리에 할당할 때 크기와 위
치를 맞춰야 합니다.
3. 페이징:
- 복잡성: 페이징은 고정된 크기의 페이지로 나누어진 주소 공간을 관리합니다. 각 페이지는 일정한 크기
를 가지며, 페이지 테이블을 통해 가상 주소를 물리 주소로 변환합니다.
(e) (2 pts) With contiguous allocation and MMU: after context switching, the address translation
speed of process B can be slower than A.
O, 연속할당의 경우 context switching이 발생하면, process B에 대해 어느 free space에 address
space를 위치시킬지 결정하고, MMU를 update해야한다. 이 과정에서 free space를 탐색하고, start
addess를 결정하는데 추가적인 시간이 필요할 수 있어 주소 변환 속도가 A보다 느릴 수 있다.
(f) (2 pts) With segmentation and MMU: after context switching, the address translation speed of
process B can be slower than A.
O, 세그멘테이션도 마찬가지로, 어느 free space에 각각의 segmentation을 위치시킬지 결정해야하므로
A보다 느릴 수 있다.
(g) (2 pts) With paging and TLB: after context switching, the address translation speed of process B
can be slower than A.
X, 각각의 process는 각각의 page table을 가지며 주소 변환 과정은 각각의 page table에서 page가 할
당된 frame을 찾는 과정이다. 또한, TLB는 자주 사용되는 PTE를 캐시하여 주소 변환 속도를 빠르게 하
므로, 프로세스 B의 주소 변환 속도가 프로세스 A보다 느릴 수 없다.
Question 2:
[30 pts] You are now designing synchronization mechanisms for your kernel.
(a) (10 pts) You implemented a spin lock primitive using atomic instructions and observed that the
spin lock works properly in your system. As a result, no further implementations of locks, such as
spin lock with yield or yield with queue, are necessary (Yeah!). Guess and provide reasons why the
spin lock can be effective on your system.
Sadly, it seems that the above implementations have a ”problem.” Describe the types of potential
problems that could happen and why (e.g., how) they happen
Potential Problems:
1. Deadlock:
- Consider two threads, Thread 1 and Thread 2.
1) Thread 1 starts executing transfer_funds(AccountX, AccountY, 500).
=> It locks AccountX.
=> It attempts to lock AccountY but gets preempted by the scheduler.
2) Thread 2 starts executing transfer_funds(AccountY, AccountX, 300).
=> It locks AccountY.
=> It attempts to lock AccountX but is blocked because AccountX is already locked by Thread 1.
3) Now, Thread 1 is waiting to lock AccountY (which is held by Thread 2), and Thread 2 is waiting
to lock AccountX (which is held by Thread 1). This creates a circular wait condition, resulting in a
deadlock. Both threads are stuck waiting for each other to release the lock, and neither can
proceed.
2. Priority Inversion:
- Suppose there are three threads with different priorities: high, medium, and low.
1) The low-priority thread locks AccountX and gets preempted by the medium-priority thread,
which does some unrelated work.
2) Meanwhile, the high-priority thread attempts to lock AccountX but is blocked by the low-
priority thread.
3) Since the medium-priority thread is running, it prevents the low-priority thread from running
and releasing the lock. The high-priority thread is effectively blocked by a lower-priority thread,
causing priority inversion.
(c) (10 pts) Please provide a pseudo-code implementation that addresses the potential problems
mentioned in question (b).
# 계정 객체 정의
class Account:
self.balance = balance
account1.lock.acquire()
account2.lock.acquire()
else:
account2.lock.acquire()
account1.lock.acquire()
# 계정 잠금을 해제하는 함수
account1.lock.release()
account2.lock.release()
# 우선순위 상속 메커니즘 적용 함수
current_thread.priority = waiting_thread.priority
# 자금 이체 함수
current_thread = get_current_thread()
inherit_priority(current_thread, waiting_thread)
# 계정 잠금
lock_accounts(account_from, account_to)
# 잔액 확인 및 자금 이체
account_from.balance -= amount
account_to.balance += amount
# 계정 잠금 해제
unlock_accounts(account_from, account_to)
restore_priority(current_thread)
# 스레드 생성 및 실행 예제
thread1.start()
thread2.start()
thread1.join()
thread2.join()
Question 3:
[30 pts] Finally, you are designing demand paging system for our OS. This demand paging system
uses a hard disk as
backing storage.
The address space has 16 pages. The size of virtual address is 6 bit. A process runs and accesses
the following virtual
addresses in sequence:
• T0: 000100 (4 in decimal) : 페이지 번호 1
• T1: 001010 (10 in decimal) : 2
• T2: 000100 (4 in decimal) : 1
• T3: 001011 (11 in decimal) : 2
• T4: 001010 (10 in decimal) : 2
• T5: 001011 (11 in decimal) :2
• T6: 000100 (4 in decimal) : 1
• T7: 001010 (10 in decimal) : 2
• T8: 001011 (11 in decimal) : 2
• T9: 000100 (4 in decimal) :1
(a) (4 pts) Calculate the virtual page numbers and offset values each for 000100 (4 in decimal),
001010 (10 in decimal), and 001011 (11 in decimal).
2) 001010
페이지 번호 : 상위 4비트, 0010(2 in decimal)
오프셋 : 하위 2비트,10(2 in decimal)
3) 001011
페이지 번호 : 상위 4비트, 0010(2 in decimal)
오프셋 : 하위 2비트, 11(3 in decimal)
(b) (6 pts) You want to apply working set algorithm in my system with the window size (∆) as four.
Provide WS(T2), WS(T4), WS(T5), WS(T7), WS(T8) and WS(T9).
1) WS(T2) : {1, 2}
2) WS(T4) : {1, 2}
... 모두 동일
(c) (10 pts) Assume that you know the memory accesses of this process (from T0 to T9) in advance.
Based on this knowledge, determine the appropriate number of frames to maintain in memory for
this process.
모든 시간에서 접근하는 페이지 번호들을 확인해보면 고유한 페이지 번호는 : 페이지 번호: 1, 2
따라서, 이 프로세스가 참조하는 고유한 페이지는 총 두 개입니다. 이를 기반으로 적절한 프레임 수를 결
정하려면 최소한 이 두 페이지가 메모리에 항상 유지되도록 해야 합니다.
=> 프로세스가 page fault 없이 효율적으로 실행되기 위해 유지해야 할 프레임 수는 고유한 페이지 수
와 같습니다. 따라서 적절한 프레임 수: 2
이 프로세스는 최소한 2개의 프레임을 메모리에 유지해야 합니다.
(d) (10 pts) Our system runs many more processes concurrently. So it experiences frequent page
outs and swapping. You are considering whether to use 1) polling or 2) DMA for the page out and
swapping between memory and the hard disk. Choose one of the two options and provide your
reasons on the choice.
- DMA
1. CPU 활용도 향상:
- DMA는 데이터를 메모리와 디스크 사이에 전송하는 작업을 전담하는 컨트롤러를 사용합니다. CPU가
데이터 복사를 직접 수행하지 않고, 대신 DMA 컨트롤러에 작업을 맡기면 CPU는 다른 작업을 수행할 수
있습니다. 이는 시스템의 전반적인 성능을 향상시키는 데 도움이 됩니다.
5. 낮은 CPU 오버헤드:
- 폴링은 CPU가 지속적으로 장치의 상태를 확인해야 하므로, CPU 오버헤드가 증가할 수 있습니다.
DMA는 이러한 오버헤드를 최소화하고, 효율적인 자원 활용을 가능하게 합니다.
+) polling이 사용되기에 적합한 상황
폴링(Polling)을 사용하는 것이 더 적합한 상황은 다음과 같습니다:
### 결론
폴링이 적합한 상황은 장치의 데이터 전송 속도가 빠르고, 작업이 간단하며, 실시간 성능이 중요한 경우
입니다. 또한, 시스템의 CPU 로드가 낮거나 DMA 설정 오버헤드가 큰 경우에도 폴링이 효율적일 수 있
습니다. 이러한 상황에서는 폴링을 사용하여 시스템의 복잡성을 줄이고 간단한 방식으로 장치와 통신할
수 있습니다.