Interview_Process/Thread/Deadlock/Paging/Semaphone/Mutex/Segmentation/Virtual Memory
Process π Thread
- Process:
- program running on memory
- has own address, resource
- Thread: work unit in process
- does not have independent address, resource
- only has independent stack
π‘ https://soheeparklee.github.io/posts/OS-2process-thread/
β Why use multi threading instead of multi processing?
System call to create process and allocate resource decrease
- communication between thread is costless than communication between process(IPC)
- However, need to take synchronization into account for multi threading
β Context Switching?
νλμ νλ‘μΈμ€κ° CPUλ₯Ό μ¬μ© μ€μΈ μνμμ λ€λ₯Έ νλ‘μΈμ€κ° CPUλ₯Ό μ¬μ©νλλ‘ νκΈ° μν΄, μ΄μ μ νλ‘μΈμ€ μνλ₯Ό 보κ΄νκ³ μλ‘μ΄ νλ‘μΈμ€μ μνλ₯Ό μ μ¬νλ μμ
ν νλ‘μΈμ€μ λ¬Έλ§₯μ κ·Έ νλ‘μΈμ€μ PCBμ κΈ°λ‘λ¨
π‘ https://soheeparklee.github.io/posts/OS-6pcb/
Semaphore π Mutex
Semaphore:
- 곡μ μμμ μ κ·Όν μ μλ μ΅λ νμ©μΉλ§νΌλ§ λμμ μ¬μ©μ μ κ·Ό κ°λ₯
- μ€λ λλ€μ 리μμ€ μ κ·Ό μμ²
- μΈλ§ν¬μ΄λ μΉ΄μ΄νΈκ° νλμ© μ€μ΄λ€κ² λλ©° 리μμ€κ° λͺ¨λ μ¬μ©μ€μΈ κ²½μ°(μΉ΄μ΄νΈ=0) λ€μ μμ μ λκΈ°
Mutex:
- μ μ΄λλ μΉμ μ νλμ μ€λ λλ§ νμ©
- ν΄λΉ μΉμ μ μ κ·Όνλ €λ λ€λ₯Έ μ€λ λλ€μ κ°μ μ μΌλ‘ λ§μ
- 첫λ²μ¬ μ€λ λκ° ν΄λΉ μΉμ μ λΉ μ Έλμ¬ λκΉμ§ κΈ°λ€λ €μΌ ν¨
π‘ https://soheeparklee.github.io/posts/OS-11semapore/
β What is deadlock?
What is deadlock?
When process cannot get the resource, and cannot work
occurs when several threads are trying to gain access to shared resource
Four conditions of deadlock
- Mutual exclusion: shared resouce requires exclusive control, inshareable
- Resource holding: thread is waiting for another resource to be released while holding onto an allocated resource
- circular wait: each process waiting for a resource held by the next process in the chain
- no preemption: thread cannot get a resource until the resource is finished by another thread
How to avoid deadlock?
- prevention: eliminate condition
(for no-preemtion: let go of the holding resource)- avoidance: run algorithm on requests to check for a safe state
- detection
> > recovery
π‘ https://soheeparklee.github.io/posts/OS-9deadlock/
β Memory Hierchy
- Register
- Cache
- RAM
- Hard Disk
π‘ https://soheeparklee.github.io/posts/4-WEB_os_architecture/
β Memory Allocation Algorithm
First fit:
- search memory from start
- allocate the first space that has enough storage
Next fit:
- begin search from the last allocated place
Best fit:
- search all the memory to find the best place to allocate
π‘ https://soheeparklee.github.io/posts/OS-12paging/
β Paging, Segmentation
βοΈ Paging
- optimize memory usage
- divide memory into fixed-size pages
βοΈ Segmentation
- divide memory into various size, more flexible
π‘ https://soheeparklee.github.io/posts/OS-12paging/
β Page Fault, Virtual Memory
βοΈ Page Fault
- when a running program accesses a memory page that is mapped into virtual address space
- but not loaded on physical memory
- OS has to replace one of the existing page with newly needed page
- thus, the need for page replacement algorithm
βοΈ Virtual Memory
- memory management technique
- create an illusion of memory using RAM + disk storage
- μ€μ λ©λͺ¨λ¦¬ μμ 곡κ°μ΄ λΆμ‘±νλ©΄, νμ¬ μ¬μ©νκ³ μμ§ μμ λ°μ΄ν°λ₯Ό λΉΌλ΄μ΄ κ°μ λ©λͺ¨λ¦¬μ μ μ₯ν΄λκ³ , μ€μ λ©λͺ¨λ¦¬μμ μ²λ¦¬λ§ νκ² νλ κ²μ΄ κ°μ λ©λͺ¨λ¦¬μ μν μ΄λ€.
- μ¦, μ€μ λ©λͺ¨λ¦¬μ λκ³ μλ 곡κ°μ΄ μκ² κ³μ μΌμ μν€λ κ². μ΄λ₯Ό λμμ£Όλ κ²μ΄ βκ°μ λ©λͺ¨λ¦¬β
β Page Replacement Algoirthm
- FIFO: keep memory in queue, oldest page is in front
- LRU: Least recently used μ΅κ·Ό κ°μ₯ μ€λ«λμ μ¬μ©νμ§ μμ νμ΄μ§
- LFU: Least frequently used μ¬μ© λΉλ μ μ νμ΄μ§
- MRU: Most recently used
βοΈ Key words in process, thread
- multi threading, process
- synchronization
βοΈ Key words in CPU, thread
- CPU β‘οΈ thread allocate
- CPU scheduling
- preemptive: Round Robin, SRTF
- non-preemptive: FCFS, SJF, HRN
- starvation
- aging
βοΈ Key words in Memory, page
- Memory β‘οΈ page
- paging
- segmentation
- internal
- external segmentation
- compaction
best fit/next fit/first fit
- page replacement
- page fault
- FIFO/LRU/MRU/NUR/LFU/Optimal Place Replacement