forked from StevenBaby/onix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f11454c
commit 670417a
Showing
10 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters