41 lines
1.1 KiB
C
41 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
#include <ctype.h>
|
|
|
|
// 숫자인지 확인하는 헬퍼 함수
|
|
int is_number(const char *str) {
|
|
while (*str) {
|
|
if (!isdigit(*str)) return 0;
|
|
str++;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc != 2) {
|
|
fprintf(stderr, "Usage: sleep <seconds>\n");
|
|
return 1;
|
|
}
|
|
|
|
if (!is_number(argv[1])) {
|
|
fprintf(stderr, "sleep: invalid time interval '%s'\n", argv[1]);
|
|
return 1;
|
|
}
|
|
|
|
unsigned int seconds = (unsigned int)atoi(argv[1]);
|
|
|
|
// sleep() 시스템 콜 호출
|
|
// 리눅스 커널에서 프로세스 상태를 TASK_INTERRUPTIBLE로 변경하고 스케줄링에서 뺍니다.
|
|
// 시그널(Ctrl+C)이 오면 sleep은 즉시 깨어나고 남은 시간을 반환합니다.
|
|
unsigned int left = sleep(seconds);
|
|
|
|
// 만약 시그널에 의해 깨어났다면? (예: SIGINT)
|
|
// 쉘이 이미 처리했겠지만, 프로그램 입장에서는 남은 시간을 확인할 수 있습니다.
|
|
if (left > 0) {
|
|
printf("Sleep interrupted! %u seconds remaining.\n", left);
|
|
}
|
|
|
|
return 0;
|
|
} |