핵심 요약
워치독 타이머는 소프트웨어가 정해진 시간 안에 "살아 있다"는 신호(kick, ping)를 보내지 않으면 시스템을 리셋하는 하드웨어 타이머입니다. 프로그램이 멈추거나 무한 루프에 빠져도 사람 없이 시스템을 다시 살리는 것이 목적입니다.
워치독 타이머란 무엇이고, 왜 필요한가요?
Linux 커널 문서는 워치독 타이머(WDT)를 소프트웨어 오류가 생겼을 때 시스템을 리셋할 수 있는 하드웨어 회로라고 설명합니다. 사용자 공간의 데몬이 일정한 간격으로 /dev/watchdog를 통해 드라이버에 알려 주고, 이 알림이 끊기면 정해진 시간 뒤에 하드웨어가 시스템을 리셋합니다.
현장에 설치되어 사람이 직접 전원을 껐다 켤 수 없는 장치에서는 프로그램이 한 번 멈추면 장치가 계속 먹통 상태로 남습니다. 워치독은 이런 멈춤이나 무한 루프를 사람 없이 복구하기 위한 마지막 안전장치입니다.
워치독은 어떻게 동작하나요?
커널 문서는 워치독이 /dev/watchdog를 여는 순간부터 동작한다고 설명합니다. 정해진 시간(문서에서는 "timeout" 또는 "margin"이라고 부릅니다) 안에 핑을 보내지 않으면 시스템이 재부팅됩니다. 가장 단순한 핑은 장치에 아무 데이터나 쓰는 것입니다.
- 워치독이 시작되면 타이머가 카운트다운을 합니다.
- 소프트웨어가 timeout 안에 kick(ping)을 보내면 카운트다운이 처음부터 다시 시작됩니다.
- 프로그램이 멈춰 kick이 끊기면 카운트다운이 끝까지 진행됩니다.
- timeout이 만료되면 하드웨어가 시스템을 리셋합니다.

kick은 어디서 해야 하나요?
면접에서 자주 나오는 함정은 타이머 ISR 안에서 무조건 kick하는 구성입니다. 이렇게 하면 메인 루프가 멈춰도 타이머 인터럽트는 계속 동작하므로 kick이 계속 들어가고, 워치독이 영원히 작동하지 않습니다.
해결 방법은 각 태스크가 자신이 정상 동작했다는 "alive" 플래그를 세우고, 메인 루프가 모든 플래그를 확인한 뒤에만 kick하고 플래그를 지우는 것입니다. 하나라도 플래그가 서지 않으면 kick하지 않으므로 워치독이 시스템을 리셋합니다.
- 잘못된 방식: 타이머 ISR에서 무조건 kick → 메인 루프가 멈춰도 계속 kick됩니다.
- 올바른 방식: 각 태스크가 alive 플래그 설정 → 메인 루프가 전부 확인 → kick 후 플래그 초기화.

Linux에서는 워치독을 어떻게 쓰나요?
/dev/watchdog를 열어 시작하고, timeout 전에 write 또는 WDIOC_KEEPALIVE로 핑을 보내며, 종료할 때는 Magic Close로 닫습니다.- 시작과 핑: 장치를 열면 워치독이 시작됩니다. 아무 데이터나 write하는 것과
WDIOC_KEEPALIVEioctl은 정확히 같은 동작입니다. - Magic Close: 장치를 닫으면 보통 워치독이 멈추지만, Magic Close를 지원하는 드라이버는 닫기 직전에 매직 문자
'V'를 쓴 경우에만 워치독을 끕니다. 그렇지 않으면 데몬이 죽었다고 보고 timeout 뒤에 시스템을 재부팅합니다. - NOWAYOUT:
CONFIG_WATCHDOG_NOWAYOUT=Y(또는nowayout모듈 파라미터)이면 한 번 시작한 워치독을 끌 방법이 없습니다. - timeout 설정:
WDIOC_SETTIMEOUT은 초 단위로 지정하며, 드라이버는 하드웨어 단위 때문에 요청과 다를 수 있는 실제 timeout 값을 돌려줍니다. 현재 값은WDIOC_GETTIMEOUT으로 읽습니다. - Pretimeout: 일부 워치독은 리셋 전에 NMI/인터럽트를 발생시켜 Linux가 패닉 정보나 코어덤프를 기록하게 합니다.
WDIOC_SETPRETIMEOUT은 timeout "이전"의 초 단위 값입니다. 커널 문서의 예시로는 timeout 60, pretimeout 10이면 50초 시점에 발생합니다. 모든 드라이버가 지원하지는 않습니다. - 리셋 원인 확인:
WDIOC_GETBOOTSTATUS는 마지막 부팅 시점의 상태를 돌려주며,WDIOF_CARDRESET은 직전 재부팅이 워치독 때문이었음을 뜻합니다. 모든 장치가 지원하지는 않습니다.
커널 문서는 이 API가 ad hoc 성격이라 드라이버마다 일부만 구현한다고 밝힙니다. 즉 드라이버마다 지원 범위가 다르다는 점을 기억해야 합니다. 또한 문서 머리말은 Linux 2.4.18 시점의 상태를 설명하며 마지막 검토는 2007년이라고 적혀 있으므로, 실제 사용 전에 사용하는 커널과 드라이버를 확인해야 합니다.
아래는 문서 수준의 단순화한 예시입니다(실행하지 않았으며 출력은 없습니다).
int fd = open("/dev/watchdog", O_WRONLY);
while (1) {
ioctl(fd, WDIOC_KEEPALIVE, 0);
sleep(10);
}
/* 정상 종료 시: 닫기 직전에 매직 문자 'V'를 씁니다. */
write(fd, "V", 1);
close(fd);
systemd 서비스는 WatchdogSec=를 쓸 수 있으며, 이 경우 서비스가 sd_notify로 WATCHDOG=1 keep-alive를 보내야 합니다. 서비스 파일 키워드는 이전 글 「systemd Type= 정리|simple·exec·notify와 서비스 파일 핵심 키워드」를 참고해 주시기 바랍니다.
면접에서 자주 나오는 워치독 질문은 무엇인가요?
하드웨어 워치독과 소프트웨어 워치독은 무엇이 다른가요?
소프트웨어 워치독은 감시 대상과 같은 커널과 CPU 위에서 돌기 때문에 커널이나 CPU가 멈추면 함께 멈춥니다. 하드웨어 타이머는 독립적으로 카운트하므로 시스템이 완전히 멈춰도 리셋할 수 있습니다. 이는 일반적인 구조에 따른 설명입니다.
timeout은 어떻게 정하나요?
정해진 정답 숫자는 없습니다. 정상 상태에서 가장 오래 걸리는 루프나 부팅 경로보다 길어야 하고, 복구 요구 시간보다는 짧아야 합니다. 하드웨어가 허용하는 최대값도 확인해야 하며, WDIOC_SETTIMEOUT이 실제 적용된 값을 돌려준다는 점도 기억해야 합니다.
리셋 원인이 워치독인지 어떻게 확인하나요?
지원하는 장치라면 WDIOC_GETBOOTSTATUS로 마지막 부팅 상태를 읽고 WDIOF_CARDRESET 플래그를 확인합니다. 이 플래그가 있으면 마지막 재부팅이 워치독 때문이었다는 뜻입니다. 모든 장치가 지원하지는 않습니다.
디버깅 중에 워치독은 어떻게 되나요?
브레이크포인트에서 멈추면 kick도 멈추므로 예상하지 못한 리셋이 일어날 수 있습니다. 사용하는 시스템이 지원하는 방법으로 워치독을 끄거나 멈춰야 하지만, NOWAYOUT 설정에서는 끌 수 없습니다. gdb 사용법은 gdb 정리 글을 참고해 주시기 바랍니다.
pretimeout은 언제 유용한가요?
리셋 직전에 패닉 정보나 코어덤프를 남겨 원인을 추적할 수 있습니다. 다만 드라이버가 지원해야 사용할 수 있습니다.
함께 보면 좋은 글은 무엇인가요?
- gdb 정리 글
- systemd Type= 정리|simple·exec·notify와 서비스 파일 핵심 키워드
출처는 어디인가요?
- The Linux Watchdog driver API (Linux kernel docs) — 확인일 2026-10-21
- systemd.service 매뉴얼 (WatchdogSec=) — 확인일 2026-10-21
Summary
A watchdog timer is a hardware timer that resets the system if software does not send an "I'm alive" signal (a kick or ping) within a set time. Its purpose is to bring a system back on its own, without a human, even when a program hangs or falls into an infinite loop.
What is a watchdog timer, and why is it needed?
The Linux kernel documentation describes a watchdog timer (WDT) as a hardware circuit that can reset the system when software faults. A userspace daemon notifies the driver through /dev/watchdog at regular intervals, and if those notifications stop, the hardware resets the system after the timeout.
On a device installed in the field where nobody can power-cycle it, a single hang can leave it dead indefinitely. The watchdog is the last safety net that recovers from hangs and infinite loops without human help.
How does a watchdog work?
According to the kernel docs, the watchdog becomes active as soon as /dev/watchdog is opened, and the system reboots unless it is pinged within the period the docs call the "timeout" or "margin". The simplest ping is writing any data to the device.
- Once started, the timer counts down.
- If software kicks (pings) within the timeout, the countdown restarts from the beginning.
- If the program hangs and kicks stop, the countdown runs to the end.
- When the timeout expires, the hardware resets the system.

Where should the kick happen?
A classic interview trap is kicking unconditionally from a timer ISR. The timer interrupt keeps firing even when the main loop is stuck, so kicks keep arriving and the watchdog never fires.
The fix is to have each task set an "alive" flag, then have the main loop check all the flags, kick, and clear them. If any flag is missing, there is no kick and the watchdog resets the system.
- Wrong: kick unconditionally in the timer ISR, so kicks continue even if the main loop hangs.
- Right: each task sets its alive flag, the main loop checks all of them, then kicks and clears the flags.

How is a watchdog used on Linux?
/dev/watchdog to start it, ping with a write or WDIOC_KEEPALIVE before the timeout, and close it properly with Magic Close.- Start and ping: opening the device starts the watchdog. Writing any data and the
WDIOC_KEEPALIVEioctl have exactly the same effect. - Magic Close: closing the device normally stops the watchdog, but with Magic Close the driver disables it only if the magic character
'V'was written just before closing. Otherwise it assumes the daemon died and the system reboots after the timeout. - NOWAYOUT: with
CONFIG_WATCHDOG_NOWAYOUT=Y(or thenowayoutmodule parameter), a started watchdog cannot be disabled. - Setting the timeout:
WDIOC_SETTIMEOUTtakes seconds, and the driver returns the actual timeout, which may differ because of hardware granularity. Read it back withWDIOC_GETTIMEOUT. - Pretimeout: some watchdogs trigger an NMI or interrupt before the reset so Linux can record panic information or coredumps.
WDIOC_SETPRETIMEOUTis the number of seconds before the timeout. In the kernel doc's example, a timeout of 60 and a pretimeout of 10 fires at 50. Not all drivers support it. - Finding the reset cause:
WDIOC_GETBOOTSTATUSreturns the status at the last reboot, andWDIOF_CARDRESETmeans the last reboot was caused by the watchdog. Not all devices support it.
The kernel doc notes that the API is ad hoc and that drivers implement different parts of it, so support varies from driver to driver. Its header also says it describes the state of Linux 2.4.18 and was last reviewed in 2007, so check your own kernel and driver before relying on it.
Below is a simplified example at the doc's level (not run, no output).
int fd = open("/dev/watchdog", O_WRONLY);
while (1) {
ioctl(fd, WDIOC_KEEPALIVE, 0);
sleep(10);
}
/* On a clean shutdown: write the magic character 'V' just before closing. */
write(fd, "V", 1);
close(fd);
systemd services can use WatchdogSec=, in which case the service must send a WATCHDOG=1 keep-alive via sd_notify. For service-file keywords, see the earlier post "systemd Type= explained: simple, exec, notify and key service-file keywords".
What watchdog questions come up in interviews?
How do hardware and software watchdogs differ?
A software watchdog runs on the same kernel and CPU it is supervising, so it dies with them. A hardware timer counts independently and can still reset the system when everything has frozen. This is general reasoning about the architecture.
How do you choose the timeout?
There is no magic number. It should be longer than the worst-case normal loop or boot path, and short enough to meet your recovery requirements. Check the hardware maximum too, and remember that WDIOC_SETTIMEOUT returns the value actually applied.
How do you tell whether the last reset came from the watchdog?
If the device supports it, read WDIOC_GETBOOTSTATUS and check for WDIOF_CARDRESET, which means the last reboot was caused by the watchdog. Not all devices support it.
What happens to the watchdog while debugging?
A breakpoint stops the kicks, so an unexpected reset can occur. Disable or stop the watchdog with whatever method your system supports, but with NOWAYOUT that is not possible. For gdb, see my gdb post.
When is pretimeout useful?
It lets you capture panic information or a coredump just before the reset, which helps trace the cause. It works only if the driver supports it.
What related posts are worth reading?
- gdb post
- systemd Type= explained: simple, exec, notify and key service-file keywords
What are the sources?
- The Linux Watchdog driver API (Linux kernel docs) — checked 2026-10-21
- systemd.service man page (WatchdogSec=) — checked 2026-10-21
'Linux' 카테고리의 다른 글
| SDR(소프트웨어 정의 라디오)이란? RTL-SDR로 433MHz 센서 신호를 받아볼 때 순서 (0) | 2026.10.17 |
|---|---|
| 크로스 컴파일이란? ARM용 gcc로 빌드하고 QEMU에서 실행할 때 순서 | What is cross-compiling? Building with ARM gcc and running it in QEMU, step by step (0) | 2026.10.16 |
| 임베디드 리눅스에서 gdb로 크래시 원인은 어떻게 찾나? core dump·gdbserver 실전 (0) | 2026.10.13 |
| 임베디드 리눅스에서 /var 경로는 무엇을 하나? (0) | 2026.09.29 |
| systemd timer 설정, OnCalendar로 주기 실행하려면 (0) | 2026.09.28 |
























