Skip to content

Commit

Permalink
✨ 128 闹钟
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenBaby committed Apr 6, 2023
1 parent f11454c commit 670417a
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/07 任务管理/128 闹钟.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 闹钟

利用信号 `SIGALRM` [^alarm] 实现闹钟。使得程序可以在特定的时间之后执行某函数。

```c++
// 设置闹钟
int alarm(int sec);
```
## 参考
[^alarm]: <https://man7.org/linux/man-pages/man2/alarm.2.html>
22 changes: 22 additions & 0 deletions src/builtin/alarm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <onix/syscall.h>
#include <onix/stdio.h>
#include <onix/signal.h>

static int signal_handler(int sig)
{
printf("pid %d when %d signal %d happened \a\n", getpid(), time(), sig);
signal(SIGALRM, (int)signal_handler);
alarm(2);
}

int main(int argc, char const *argv[])
{
signal(SIGALRM, (int)signal_handler);
alarm(2);
while (true)
{
printf("pid %d when %d sleeping 1 second\n", getpid(), time());
sleep(1000);
}
return 0;
}
4 changes: 4 additions & 0 deletions src/include/onix/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef enum syscall_t
SYS_NR_GETPID = 20,
SYS_NR_MOUNT = 21,
SYS_NR_UMOUNT = 22,
SYS_NR_ALARM = 27,
SYS_NR_FSTAT = 28,
SYS_NR_STTY = 31,
SYS_NR_GTTY = 32,
Expand Down Expand Up @@ -177,4 +178,7 @@ int gtty();
// 发送信号
int kill(pid_t pid, int signal);

// 设置闹钟
int alarm(int sec);

#endif
1 change: 1 addition & 0 deletions src/include/onix/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct task_t
struct file_t *files[TASK_FILE_NR]; // 进程文件表
u32 signal; // 进程信号位图
u32 blocked; // 进程信号屏蔽位图
struct timer_t *alarm; // 闹钟定时器
sigaction_t actions[MAXSIG]; // 信号处理函数
u32 magic; // 内核魔数,用于检测栈溢出
} task_t;
Expand Down
20 changes: 20 additions & 0 deletions src/kernel/alarm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <onix/task.h>
#include <onix/timer.h>

extern int sys_kill();

static void task_alarm(timer_t *timer)
{
timer->task->alarm = NULL;
sys_kill(timer->task->pid, SIGALRM);
}

int sys_alarm(int sec)
{
task_t *task = running_task();
if (task->alarm)
{
timer_put(task->alarm);
}
task->alarm = timer_add(sec * 1000, task_alarm, 0);
}
3 changes: 3 additions & 0 deletions src/kernel/gate.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ extern int sys_sgetmask();
extern int sys_ssetmask();
extern int sys_sigaction();

extern int sys_alarm();

extern int sys_mkfs();

void syscall_init()
Expand Down Expand Up @@ -184,4 +186,5 @@ void syscall_init()
syscall_table[SYS_NR_SGETMASK] = sys_sgetmask;
syscall_table[SYS_NR_SSETMASK] = sys_ssetmask;
syscall_table[SYS_NR_SIGACTION] = sys_sigaction;
syscall_table[SYS_NR_ALARM] = sys_alarm;
}
5 changes: 4 additions & 1 deletion src/kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ void task_unblock(task_t *task, int reason)
{
assert(!get_interrupt_state());

list_remove(&task->node);
if (task->node.next)
{
list_remove(&task->node);
}

assert(task->node.next == NULL);
assert(task->node.prev == NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void timer_put(timer_t *timer)

void default_timeout(timer_t *timer)
{
assert(timer->task->node.next);
// assert(timer->task->node.next);
task_unblock(timer->task, -ETIME);
}

Expand Down
5 changes: 5 additions & 0 deletions src/lib/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ int kill(pid_t pid, int signal)
return _syscall2(SYS_NR_KILL, pid, signal);
}

int alarm(int sec)
{
return _syscall1(SYS_NR_ALARM, sec);
}

void yield()
{
_syscall0(SYS_NR_YIELD);
Expand Down
2 changes: 2 additions & 0 deletions src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ BUILTIN_APPS := \
$(BUILD)/builtin/dup.out \
$(BUILD)/builtin/err.out \
$(BUILD)/builtin/count.out \
$(BUILD)/builtin/alarm.out \

$(BUILD)/builtin/%.out: $(BUILD)/builtin/%.o \
$(BUILD)/lib/libc.o \
Expand Down Expand Up @@ -98,6 +99,7 @@ $(BUILD)/kernel.bin: \
$(BUILD)/kernel/system.o \
$(BUILD)/kernel/execve.o \
$(BUILD)/kernel/signal.o \
$(BUILD)/kernel/alarm.o \
$(BUILD)/fs/super.o \
$(BUILD)/fs/bmap.o \
$(BUILD)/fs/inode.o \
Expand Down

0 comments on commit 670417a

Please sign in to comment.