上一篇博客 分析了一下Mission 1中的代码,现在我们来开始正式实现。

首先,给线程结构体 struct thread{}加上ticks_blocked成员,在threads/thread.h中

  

 /* Record the time the thread has been blocked. */int64_t ticks_blocked;

在线程创建的时候,ticks_blocked应该被初始化为0,在threads/thread.c中,找到thread_create()函数,添加如下代码:

/*Set default ticks_blocked = 0*/t->ticks_blocked = 0;

修改timer_sleep()函数:

/* Sleeps for approximately TICKS timer ticks.  Interrupts mustbe turned on. */
void
timer_sleep (int64_t ticks)
{if (ticks <= 0){return;}ASSERT (intr_get_level () == INTR_ON);enum intr_level old_level = intr_disable ();struct thread *current_thread = thread_current ();current_thread->ticks_blocked = ticks;thread_block ();intr_set_level (old_level);
}

在这里我们调用了一个thread_block()函数,在threads/thread.c中修改:

/* Puts the current thread to sleep.  It will not be scheduledagain until awoken by thread_unblock().This function must be called with interrupts turned off.  Itis usually a better idea to use one of the synchronizationprimitives in synch.h. */
void
thread_block (void)
{ASSERT (!intr_context ());ASSERT (intr_get_level () == INTR_OFF);thread_current ()->status = THREAD_BLOCKED;schedule ();
}

然后,修改时钟中断处理函数,找到devices/timer.中的timer_interrupt()函数,添加代码

  thread_foreach (blocked_thread_check, NULL);

这里的thread_foreach()函数,即对每个函数都执行blocked_thread_check(),在threads/thread.c中修改。

/* Invoke function 'func' on all threads, passing along 'aux'.This function must be called with interrupts off. */
void
thread_foreach (thread_action_func *func, void *aux)
{struct list_elem *e;ASSERT (intr_get_level () == INTR_OFF);for (e = list_begin (&all_list); e != list_end (&all_list);e = list_next (e)){struct thread *t = list_entry (e, struct thread, allelem);func (t, aux);}
}

最后,给thread添加一个blocked_thread_check()方法:

先声明

void blocked_thread_check (struct thread *, void * UNUSED);

然后在thread.c中添加:

/* Check the blocked thread */
void
blocked_thread_check (struct thread *t, void *aux UNUSED)
{if (t->status == THREAD_BLOCKED && t->ticks_blocked > 0){t->ticks_blocked--;if (t->ticks_blocked == 0){thread_unblock(t);}}
}

这里又修改了一个thread_unblock()函数,作用是将线程丢到ready队列中:

/* Transitions a blocked thread T to the ready-to-run state.This is an error if T is not blocked.  (Use thread_yield() tomake the running thread ready.)This function does not preempt the running thread.  This canbe important: if the caller had disabled interrupts itself,it may expect that it can atomically unblock a thread andupdate other data. */
void
thread_unblock (struct thread *t)
{enum intr_level old_level;ASSERT (is_thread (t));old_level = intr_disable ();ASSERT (t->status == THREAD_BLOCKED);list_push_back (&ready_list, &t->elem);t->status = THREAD_READY;intr_set_level (old_level);
}

这样一来,timer_sleep()的唤醒机制我们就实现了,将更改过的代码push上去。

中间发现自己犯了几个小错误,fix bug之后才跑通。

具体的代码可以在我的Github中找到。

转载于:https://www.cnblogs.com/crayygy/p/4537019.html

pintos project (2) Project 1 Thread -Mission 1 Code相关推荐

  1. eclipse 创建项目报项目存在的解决方案(Invalid project name: Project xxx already exists)...

    2019独角兽企业重金招聘Python工程师标准>>> eclipse Invalid project name: Project "xxx" already e ...

  2. Android project依赖project配置说明

    Android studio 项目(Project)依赖(非Module) 0. 前言 对于Module 级别的依赖大家都知道,今天说下Android Studio下的项目依赖. 场景: A Proj ...

  3. 通过Eclipse创建一个Project ,Java Project 和Tomcat Project 生成的目录和文件

    今天研究发现的,呵呵,也写下来了. 今天通过Eclipse创建了一个Project ,一个Java Project 和一个Tomcat Project,具体发现如下: 一. Project :生成一个 ...

  4. Project: Individual Project - Word frequency program----11061192zmx

    Description & Requirements http://www.cnblogs.com/jiel/p/3311400.html 项目时间估计 理解项目要求: 1小时 构建项目逻辑: ...

  5. 创建maven parent project module project

    1.命令方式: 1)Create the top-level root: mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.arc ...

  6. java project voco_Adobe Project VoCo这款软件在哪里下载?

    刚查了一下: ...真是啥都敢写. 答题: 首先,这款软件不存在. 参考: Let's Get Experimental: Behind the Adobe MAX SneaksWhile these ...

  7. ios Flipper Thread 10: EXC_BAD_ACCESS (code=1, address=0x10001)

    每次debug都卡在这个位置(X509_NAME_add_entry_by_txt),realease模式没有问题 查看podfile use_flipper!({ 'Flipper-Folly' = ...

  8. iOS Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)崩溃错误

    崩溃在main函数里,控制台没打印相关崩溃信息,可能是僵尸对象问题,某些方法访问到了已经释放了的内存. 打开僵尸对象调试:Xcode菜单选择Product -> Scheme -> Edi ...

  9. 作业提交系统2.0(marven项目、project/module模式、数据库连接池

    作业提交系统2.0(marven项目.project/module模式.数据库连接池) Maven 使用project/module模式 数据库连接池 改造后的项目github地址 Maven Apa ...

最新文章

  1. Sublime Text3配置Lua运行环境
  2. 693. Binary Number with Alternating Bits -LeetCode
  3. 系统也需和谐共存——Win7与XP安装同一盘符方法解析
  4. 信息架构、结构图、流程图
  5. 8421BCD码与十进制之间的转换
  6. Maven超级详细安装教程ovo
  7. Mosquitto安装配置websockets
  8. 【程序员节特别推送】搭建一个与技术无关的博客网站(Java后台)
  9. 周期信号的博里叶级数表示(连续时间)
  10. Laravel的ORM模型的find(),findOrFail(),first(),firstOrFail(),get(),list(),toArray()之间的区别是什么?
  11. 关于房价问题的看法。
  12. 音频线是什么 音频线如何连接
  13. Calendar日期类获取上月同期需求
  14. lamda函数的简介
  15. java的io和nio例子
  16. Java性能优化怎么做好
  17. python中tk窗口刷新_用按钮刷新tk窗口
  18. java工程师面试英文自我介绍_软件工程师面试英语自我介绍范文
  19. 生物学重复好不好--看看样本相关性
  20. 7.3 嵌入式实训学习的第一天

热门文章

  1. 固件是通用的吗_冷镦和冷挤压是一回事吗,两者有什么区别?
  2. Nginx 安装与使用
  3. [JAVA]定时任务之-Quartz使用篇
  4. 你有关注自己的简历吗?优秀的程序员简历是怎样炼成的?
  5. 洛谷P3386 【模板】二分图匹配
  6. Java中跳出多层循环的简单方法
  7. error C2146: 语法错误 : 缺少“;”(在标识符“PVOID64”的前面)[转]
  8. 论ARMv7 Thumb-2指令集的性能(含Thumb指令集介绍)【转载】
  9. Linux 命令(61)—— ldd 命令
  10. C++ RTTI 简介