Skip to content

Latest commit

Β 

History

History
241 lines (206 loc) Β· 7.5 KB

deadlock.md

File metadata and controls

241 lines (206 loc) Β· 7.5 KB

κ΅μ°©μƒνƒœ(Deadlock)에 λŒ€ν•œ 기술 λ©΄μ ‘ λ‹΅λ³€

μ£Όμš” 질문

"κ΅μ°©μƒνƒœ(Deadlock)κ°€ 무엇이며, λ°œμƒ 쑰건과 ν•΄κ²° 방법에 λŒ€ν•΄ μ„€λͺ…ν•΄μ£Όμ„Έμš”."

1. κ΅μ°©μƒνƒœ μ •μ˜

κ΅μ°©μƒνƒœ(Deadlock)λŠ” 두 개 μ΄μƒμ˜ ν”„λ‘œμ„ΈμŠ€λ‚˜ μŠ€λ ˆλ“œκ°€ μ„œλ‘œκ°€ 가진 μžμ›μ„ 기닀리며 λ¬΄ν•œνžˆ λŒ€κΈ°ν•˜λŠ” μƒνƒœλ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.

1.1 κ΅μ°©μƒνƒœ μ˜ˆμ‹œ μ½”λ“œ

// κ΅μ°©μƒνƒœ λ°œμƒ κ°€λŠ₯ν•œ μ½”λ“œ
public class DeadlockExample {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();
    
    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (lock1) {
                System.out.println("Thread 1: Holding lock1");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (lock2) {
                    System.out.println("Thread 1: Holding lock2");
                }
            }
        });
        
        Thread thread2 = new Thread(() -> {
            synchronized (lock2) {
                System.out.println("Thread 2: Holding lock2");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (lock1) {
                    System.out.println("Thread 2: Holding lock1");
                }
            }
        });
        
        thread1.start();
        thread2.start();
    }
}

2. κ΅μ°©μƒνƒœ λ°œμƒ 쑰건

2.1 4가지 ν•„μš” 쑰건

  1. μƒν˜Έ 배제(Mutual Exclusion)

    • μžμ›μ€ ν•œ λ²ˆμ— ν•œ ν”„λ‘œμ„ΈμŠ€λ§Œ μ‚¬μš© κ°€λŠ₯
    synchronized void transfer(Account from, Account to, double amount) {
        // κ³„μ’Œ μž”μ•‘ 이체 둜직
    }
  2. μ μœ μ™€ λŒ€κΈ°(Hold and Wait)

    • μžμ›μ„ λ³΄μœ ν•œ μƒνƒœμ—μ„œ λ‹€λ₯Έ μžμ›μ„ μš”μ²­
    synchronized (account1) {
        synchronized (account2) {
            // 두 κ³„μ’Œλ₯Ό λͺ¨λ‘ ν•„μš”λ‘œ ν•˜λŠ” μž‘μ—…
        }
    }
  3. 비선점(No Preemption)

    • λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€μ˜ μžμ›μ„ κ°•μ œλ‘œ 빼앗을 수 μ—†μŒ
    // μžμ›μ„ κ°•μ œλ‘œ ν•΄μ œν•  수 μ—†λŠ” 상황
    public void processData() {
        lock.lock();  // ν•œλ²ˆ νšλ“ν•œ 락은 직접 ν•΄μ œν•˜κΈ° μ „κΉŒμ§€ μœ μ§€
        try {
            // 처리 둜직
        } finally {
            lock.unlock();
        }
    }
  4. μˆœν™˜ λŒ€κΈ°(Circular Wait)

    • ν”„λ‘œμ„ΈμŠ€κ°„ μžμ› μš”μ²­μ΄ μˆœν™˜ ν˜•νƒœλ‘œ λ°œμƒ
    // μˆœν™˜ λŒ€κΈ° λ°œμƒ κ°€λŠ₯ μ½”λ“œ
    Thread 1: lock(A) β†’ wait(B)
    Thread 2: lock(B) β†’ wait(A)

3. κ΅μ°©μƒνƒœ ν•΄κ²° 방법

3.1 예방(Prevention)

// μˆœμ„œν™”λœ 락 νšλ“μœΌλ‘œ κ΅μ°©μƒνƒœ 예방
public class Account {
    private static final Object tieLock = new Object();
    
    public void transfer(Account from, Account to, double amount) {
        // κ³„μ’Œ IDλ₯Ό κΈ°μ€€μœΌλ‘œ 락 νšλ“ μˆœμ„œ 정함
        if (from.getId() < to.getId()) {
            synchronized (from) {
                synchronized (to) {
                    // 이체 둜직
                }
            }
        } else if (from.getId() > to.getId()) {
            synchronized (to) {
                synchronized (from) {
                    // 이체 둜직
                }
            }
        } else {
            synchronized (tieLock) {
                synchronized (from) {
                    synchronized (to) {
                        // 이체 둜직
                    }
                }
            }
        }
    }
}

3.2 νšŒν”Ό(Avoidance)

// 은행원 μ•Œκ³ λ¦¬μ¦˜ κ΅¬ν˜„ μ˜ˆμ‹œ
public class BankerAlgorithm {
    private int[] available;
    private int[][] maximum;
    private int[][] allocation;
    private int[][] need;
    
    public boolean isSafeState(int[] request, int processId) {
        // μ•ˆμ „ μƒνƒœ 검사 둜직
        return true; // μ•ˆμ „ν•œ κ²½μš°μ—λ§Œ μžμ› ν• λ‹Ή
    }
}

3.3 탐지 및 볡ꡬ(Detection and Recovery)

// λ°λ“œλ½ 탐지 μ˜ˆμ‹œ
public class DeadlockDetector {
    private Set<Thread> findDeadlockedThreads() {
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        long[] threadIds = threadBean.findDeadlockedThreads();
        Set<Thread> deadlockedThreads = new HashSet<>();
        if (threadIds != null) {
            ThreadInfo[] threadInfos = threadBean.getThreadInfo(threadIds);
            for (ThreadInfo threadInfo : threadInfos) {
                Thread thread = findMatchingThread(threadInfo.getThreadId());
                if (thread != null) {
                    deadlockedThreads.add(thread);
                }
            }
        }
        return deadlockedThreads;
    }
}

4. μ‹€μ œ μ‚¬μš© μ˜ˆμ‹œ

4.1 Lock μˆœμ„œν™”

public class SafeBankTransfer {
    private final Lock lock = new ReentrantLock();
    
    public void transfer(Account from, Account to, double amount) {
        Lock firstLock = from.getId() < to.getId() ? from.getLock() : to.getLock();
        Lock secondLock = from.getId() < to.getId() ? to.getLock() : from.getLock();
        
        firstLock.lock();
        try {
            secondLock.lock();
            try {
                // μ•ˆμ „ν•œ 이체 둜직
            } finally {
                secondLock.unlock();
            }
        } finally {
            firstLock.unlock();
        }
    }
}

4.2 νƒ€μž„μ•„μ›ƒ μ‚¬μš©

public class TimeoutLocking {
    private final Lock lock = new ReentrantLock();
    
    public boolean executeWithTimeout() {
        try {
            if (lock.tryLock(1000, TimeUnit.MILLISECONDS)) {
                try {
                    // μž‘μ—… μˆ˜ν–‰
                    return true;
                } finally {
                    lock.unlock();
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return false;
    }
}

근거 자료

1. ν•™μˆ  자료

2. 기술 λ¬Έμ„œ

3. 운영체제 κ΅κ³Όμ„œ

4. 싀무 κ΄€λ ¨ 자료

μ‹€μ œ λ©΄μ ‘ λŒ€λΉ„ μΆ”κ°€ 질문

  1. "μ‹€μ œ ν”„λ‘œμ νŠΈμ—μ„œ λ°λ“œλ½μ„ κ²½ν—˜ν•œ 적이 μžˆλ‚˜μš”? μ–΄λ–»κ²Œ ν•΄κ²°ν•˜μ…¨λ‚˜μš”?"

  2. "λ°λ“œλ½κ³Ό 라이브락(Livelock)의 차이점은 λ¬΄μ—‡μΈκ°€μš”?"

  3. "λΆ„μ‚° μ‹œμŠ€ν…œμ—μ„œμ˜ λ°λ“œλ½μ€ μ–΄λ–»κ²Œ ν•΄κ²°ν•  수 μžˆμ„κΉŒμš”?"

  4. "λ°μ΄ν„°λ² μ΄μŠ€ νŠΈλžœμž­μ…˜μ—μ„œ λ°œμƒν•˜λŠ” λ°λ“œλ½μ€ μ–΄λ–»κ²Œ ν•΄κ²°ν•˜μ‹œλ‚˜μš”?"

μ‹€μ œ λ©΄μ ‘μ—μ„œλŠ” 이둠적인 μ§€μ‹λΏλ§Œ μ•„λ‹ˆλΌ, μ‹€μ œ κ²½ν—˜ν•œ λ°λ“œλ½ 상황과 ν•΄κ²° 방법에 λŒ€ν•΄ ꡬ체적으둜 μ„€λͺ…ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.