5
C#은 고수준 언어로써 **하드웨어 직접 제어(예: 인터럽트, ISR, 인터럽트 벡터 테이블)**는 지원하지 않습니다. 하지만 이 개념은 임베디드 시스템, 운영체제, 또는 커널 수준에서 매우 중요한 요소입니다.
그럼에도 불구하고 C# 개발자가 **”인터럽트와 유사한 동작”**을 이해하거나 모델링할 수는 있습니다. 아래에서 각 개념을 설명하고 C# 관점에서 어떻게 대응하거나 구현할 수 있는지 예시로 보여드리겠습니다.
📌 1. 인터럽트 벡터 테이블 (Interrupt Vector Table)
✔️ 설명:
- 하드웨어 인터럽트가 발생하면 CPU는 해당 인터럽트에 대한 처리 루틴(ISR)의 주소를 저장한 테이블을 참조
- 이 테이블은 인터럽트 번호와 해당 ISR 함수 포인터의 매핑 구조
✔️ C# 관점:
- C#에서는 실제 주소를 저장하지 않지만, 딕셔너리 형태로 ISR을 관리하는 구조로 흉내낼 수 있음
Dictionary<int, Action> interruptVectorTable = new Dictionary<int, Action>();
// ISR 등록
interruptVectorTable[0x01] = () => Console.WriteLine("Keyboard Interrupt");
interruptVectorTable[0x02] = () => Console.WriteLine("Timer Interrupt");
// 인터럽트 발생 시 처리
void HandleInterrupt(int interruptNumber)
{
if (interruptVectorTable.TryGetValue(interruptNumber, out var isr))
isr.Invoke();
else
Console.WriteLine("Unknown interrupt");
}
// 예시 호출
HandleInterrupt(0x01); // Keyboard Interrupt
HandleInterrupt(0x02); // Timer Interrupt
📌 2. 인터럽트 서비스 루틴 (ISR: Interrupt Service Routine)
✔️ 설명:
- 실제 인터럽트가 발생했을 때 실행되는 함수
- 하드웨어 요청을 신속히 처리하고 다시 주 실행 루틴으로 복귀
✔️ C# 관점:
- 이벤트 기반 처리, 타이머 콜백, 신호 기반 이벤트 핸들러 등을 ISR처럼 간주 가능
public class InterruptSimulator
{
public event Action OnKeyboardInterrupt;
public event Action OnTimerInterrupt;
public void SimulateInterrupt(string type)
{
switch (type)
{
case "keyboard":
OnKeyboardInterrupt?.Invoke();
break;
case "timer":
OnTimerInterrupt?.Invoke();
break;
}
}
}
// 사용
var sim = new InterruptSimulator();
sim.OnKeyboardInterrupt += () => Console.WriteLine("ISR: 키보드 인터럽트 처리");
sim.OnTimerInterrupt += () => Console.WriteLine("ISR: 타이머 인터럽트 처리");
sim.SimulateInterrupt("keyboard");
sim.SimulateInterrupt("timer");
📌 3. 인터럽트 유형
유형 | 설명 | C# 유사 개념 |
---|---|---|
하드웨어 인터럽트 | 외부 장치에서 CPU로 발생 (키보드, 마우스 등) | UI 이벤트, 키보드 입력 이벤트 |
소프트웨어 인터럽트 | 프로그램 내에서 명령어로 의도적으로 발생 | Thread.Abort() , 예외 처리 |
타이머 인터럽트 | 일정 시간 간격마다 발생하는 인터럽트 | System.Timers.Timer 의 Elapsed 이벤트 |
예외 인터럽트 | 0으로 나누기 등 비정상적인 명령 처리 중 발생 | try-catch 예외 처리 |
📌 예제:
try
{
int x = 0;
int y = 10 / x; // 예외 인터럽트 유사 상황
}
catch (DivideByZeroException ex)
{
Console.WriteLine("ISR: 0으로 나누기 예외 처리");
}
✅ 요약: C#에서 인터럽트 개념 대응
인터럽트 개념 | 하드웨어 의미 | C# 대응 개념 |
---|---|---|
인터럽트 벡터 테이블 | 인터럽트 번호 → ISR 주소 매핑 | Dictionary<int, Action> 또는 이벤트 테이블 |
인터럽트 서비스 루틴 | 인터럽트 발생 시 실행되는 함수 | 이벤트 핸들러, 타이머 콜백, Action 델리게이트 |
하드웨어 인터럽트 | 외부 장치의 신호 | UI 이벤트, 디바이스 입력 등 |
소프트웨어 인터럽트 | 명령어 또는 프로그램이 발생시킴 | 예외 발생, 사용자 정의 이벤트 |
타이머 인터럽트 | 시간 기반 인터럽트 | System.Timers.Timer , Task.Delay |
🧩 확장: 실시간 시스템 시뮬레이션
C#으로 인터럽트 기반 시스템을 시뮬레이션할 수 있습니다. 예: PLC, RTOS 개념.
using System.Timers;
Timer systemTimer = new Timer(1000); // 1초마다 인터럽트 발생
systemTimer.Elapsed += (sender, e) => Console.WriteLine("ISR: 주기적 타이머 인터럽트 발생");
systemTimer.Start();