Skip to content

Conversation

smallcjy
Copy link
Contributor

@smallcjy smallcjy commented Apr 6, 2024

实现思想及过程

  • Alarm定时器封装,创建alarm结构体,包含timer和timeout(停止时间),方法:new函数和timer方法的封装;
  • 创建timerfunc函数,创建SYS_ALARM信号并在定时器触发时使用signal_send发送信号;
  • 在pcb创建一个新的对象alarm,供存放旧alarm定时器的信息,类型为Arc<Mutex<Option>>
  • 进程初始化时,把alarm初始化为包含None的锁的Arc指针
  • sys_alarm系统调用设计思想:首先获取当前进程的pcb的alarm定时器的引用,查看其Option值是否为None,若是运行alarm_init初始化,然后启动定时器;若不是,获取其截止时间和当前时间的差值(即剩余时间)并将其设置为None值(删除旧的定时器),初始化并启动新定时器。最后返回剩余时间
  • jiffies和秒数之间的转换暂时按照固定的形式转换
  • setitimer系统调用和alarm系统调用共用一个alarm定时器,setitimer尚未实现。
    测试程序
  • 设置三个测试用例,第一个创建一个5秒的alarm,查看是否接受到alarm发送的信号;第二个创建一个5秒的alarm,查看其返回值是否为上一个alarm的剩余时间,第三个测试在创建一个2秒的alarm后创建一个5秒的alarm,查看其返回值是否为上个闹钟的剩余时间并取消未执行的闹钟,查看新闹钟是否执行。

Copy link
Member

@fslongjin fslongjin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提交之前要先make fmt

@@ -693,6 +709,7 @@ impl ProcessControlBlock {
children: RwLock::new(Vec::new()),
wait_queue: WaitQueue::default(),
thread: RwLock::new(ThreadInfo::new()),
alarm_timer: Arc::new(Mutex::new(None)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么不是Option<Arc<AlarmTimer>>?
目前这种写法的封装性不好。这些处理是否可变的操作,应当封装到里面去。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉会不会存在多线程同时访问alarm产生数据竞争,Arc<Mutex<>>能支持内部可变

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要把Mutex封装到AlarmTimer里面去。而不是暴露出来。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

了解🫡

}

impl AlarmTimer {
pub fn new(timer_func: Box<dyn TimerFunction>, expire_time: u64) -> Mutex<Option<Self>>{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个expire的是jiffies吗?如果是的话把变量名改为expired_jifffies

@@ -0,0 +1,14 @@
# DragonOS Rust-Application Template
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改名称


//test1: alarm系统调用能否正常运行
unsafe {
syscall(37,5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要直接写系统调用号。
原因: #682 (comment)

@fslongjin
Copy link
Member

#662

@smallcjy
Copy link
Contributor Author

smallcjy commented Apr 6, 2024

okok马上修改

Copy link
Member

@chiichen chiichen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在想这些持续时间都用Duration表示是不是方便一点,不用搞这些s, ms, ns的转换了。后面可以考虑移植一下 std 里的 instatnt 和duaration,结合起来就方便很多了,这种东西还是挺容易出错的

Comment on lines +167 to +168
//这里的second是以jiddies为单位
//Todo:秒转换成jiddies
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

打错了?应该是jiffies

}

//初始化目标进程的alarm定时器
pub fn alarm_timer_init(pid: Pid, time_out: u64) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同样的,这里如果是jiffies的话要体现出来,如果不是的话就写time_out_sec

@chiichen
Copy link
Member

chiichen commented Apr 7, 2024

与主线代码有冲突,要解决一下。还有,就是开发不要直接用自己仓库的master开发,要新建一个分支

@smallcjy
Copy link
Contributor Author

smallcjy commented Apr 8, 2024

确实直接用秒在计算剩余秒数时可能会有误差

@smallcjy smallcjy closed this Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants