博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pintos project (2) Project 1 Thread -Mission 1 Code
阅读量:6000 次
发布时间:2019-06-20

本文共 2951 字,大约阅读时间需要 9 分钟。

 分析了一下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 must   be turned on. */voidtimer_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 scheduled   again until awoken by thread_unblock().   This function must be called with interrupts turned off.  It   is usually a better idea to use one of the synchronization   primitives in synch.h. */voidthread_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. */voidthread_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 */voidblocked_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() to   make the running thread ready.)   This function does not preempt the running thread.  This can   be important: if the caller had disabled interrupts itself,   it may expect that it can atomically unblock a thread and   update other data. */voidthread_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

你可能感兴趣的文章
20145209 刘一阳 《网络对抗》实验四:恶意代码分析
查看>>
个人学期总结
查看>>
CodeForces 985E Pencils and Boxes
查看>>
为什么Elasticsearch查询变得这么慢了?
查看>>
Cetos 中添加bbr服务
查看>>
win7_64位操作系统安装python3.6.3遇到的问题和解决方法
查看>>
WPF操作邮箱,发送邮件
查看>>
kvm在线磁盘扩展
查看>>
libvirt-adabddad
查看>>
apache的配置及检查
查看>>
关于解决 从相册中选择照片后无法剪切图片以及无法加载图片的问题
查看>>
node.js中使用http模块创建服务器和客户端
查看>>
Away3D基础教程(六):支持双面交互的PlaneGeometry
查看>>
(十五)Centos之安装jdk
查看>>
RISC-V: custom instruction and its simulation(转)
查看>>
博客园个性时钟,Play with me !!!
查看>>
HDU 5366 The mook jong
查看>>
Unity ScriptableObject自定义属性显示
查看>>
【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】
查看>>
ORACLE内存管理之ASMM AMM
查看>>