update: userprog (tee, tsh, sleep, hex2bin)

This commit is contained in:
2026-01-01 04:22:30 +00:00
parent cf722d75ff
commit 79f46aa971
22 changed files with 1196 additions and 47 deletions
+15
View File
@@ -0,0 +1,15 @@
TARGET = sleep
SRCS = sleep.c
CC = gcc
CFLAGS = -static -Wall
OUT_BIN ?= $(TARGET)
all: $(OUT_BIN)
$(OUT_BIN): $(SRCS)
@echo " [CC] Compiling to $(OUT_BIN)..."
$(CC) $(CFLAGS) -o $@ $^
clean:
rm -f $(TARGET)
+41
View File
@@ -0,0 +1,41 @@
#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;
}