MIT6.s081实验7学习记录

Lab6: Multithreading

这个实验主要是完善用户态线程的一些功能,包括线程创建、切换、调度等,还有在多线程的机器上做一些实验来保证多进程提速和隔离。首先切换分支到实验7。

$ git fetch
$ git checkout thread
$ make clean

Uthread: switching between threads (moderate)

这个实验是要完成用户态的线程切换,课上讲到,在内核中进程切换要使用swtch函数来保存和恢复 callee-save registers 寄存器的值,原因就是此外的一些寄存器是要被其他线程使用的,值肯定会被修改。 callee-save registers 对于线程切换和恢复是最关键的寄存器。线程除了独享的寄存器和栈之外,都是共享的部分,所以不需要全部切换,所以在用户级线程切换时,只需要保存寄存器和栈即可。

首先要写一个thread_switch 的汇编文件,至于为什么是汇编,因为这个函数主要作用就是对寄存器进行操作。可以完全仿照内核中 swtch.S 来写。

.globl thread_switch
thread_switch:
		sd ra, 0(a0)
        sd sp, 8(a0)
        sd s0, 16(a0)
        sd s1, 24(a0)
        sd s2, 32(a0)
        sd s3, 40(a0)
        sd s4, 48(a0)
        sd s5, 56(a0)
        sd s6, 64(a0)
        sd s7, 72(a0)
        sd s8, 80(a0)
        sd s9, 88(a0)
        sd s10, 96(a0)
        sd s11, 104(a0)

        ld ra, 0(a1)
        ld sp, 8(a1)
        ld s0, 16(a1)
        ld s1, 24(a1)
        ld s2, 32(a1)
        ld s3, 40(a1)
        ld s4, 48(a1)
        ld s5, 56(a1)
        ld s6, 64(a1)
        ld s7, 72(a1)
        ld s8, 80(a1)
        ld s9, 88(a1)
        ld s10, 96(a1)
        ld s11, 104(a1)
		ret    /* return to ra */

然后需要修改uthread.c 文件里面的一些函数。首先是添加一个结构体 context,用来存放寄存器值。

struct context {
  uint64 ra;
  uint64 sp;

  // callee-saved
  uint64 s0;
  uint64 s1;
  uint64 s2;
  uint64 s3;
  uint64 s4;
  uint64 s5;
  uint64 s6;
  uint64 s7;
  uint64 s8;
  uint64 s9;
  uint64 s10;
  uint64 s11;
};

然后把它加到线程结构体中。

struct thread {
  char       stack[STACK_SIZE]; /* the thread's stack */
  int        state;             /* FREE, RUNNING, RUNNABLE */
  struct context context;  //加到这里

};

接下来就是要在线程创建是初始化返回地址和栈指针。然后在线程调度是调用切换函数 thread_switch 即可。这部分可以仿照 /kernel/proc.c 的127-128行来写。

void 
thread_create(void (*func)())
{
  struct thread *t;

  for (t = all_thread; t < all_thread + MAX_THREAD; t++) {
    if (t->state == FREE) break;
  }
  t->state = RUNNABLE;//找到一个可用的线程,并且设置为就绪态,可以被切换,使用CPU
  // YOUR CODE HERE
  t->context.ra = (uint64)func;  //设置好返回地址
  t->context.sp = (uint64)&t->stack + STACK_SIZE;  //设置好栈指针
  
}

//把这个函数参数类型修改一下
extern void thread_switch(struct context* old, struct context* new);

void 
thread_schedule(void)
{
  ......

  /* Find another runnable thread. */
  if (current_thread != next_thread) {         /* switch threads?  */
    next_thread->state = RUNNING;
    t = current_thread; //这是旧
    current_thread = next_thread; //这是新
     thread_switch(&t->context, &next_thread->context);  //加到这里
  } else
    next_thread = 0;
}

目前就修改完毕了。

Using threads (moderate)

这个实验是确保多进程的隔离。当多个进程同时向 bucket放数据时,如果进程不是隔离的,也就是说都可以访问到同一个索引,那么就可能出现最终只有一个进程的值被放进去了,造成了键值覆盖。因此需要使用进程锁来避免这一情况。


pthread_mutex_t mutex; //定义一个进程锁

static 
void put(int key, int value)
{
  pthread_mutex_lock(&mutex); //上锁
  int i = key % NBUCKET;
  // is the key already present?
  struct entry *e = 0;
  for (e = table[i]; e != 0; e = e->next) {
    if (e->key == key)
      break;
  }
  if(e){
    // update the existing key.
    e->value = value;
  } else {
    // the new is new.
    insert(key, value, &table[i], table[i]);
  }
  pthread_mutex_unlock(&mutex); //解锁
}
static struct entry*
get(int key)
{
  
  int i = key % NBUCKET;
  pthread_mutex_lock(&mutex); //上锁
  struct entry *e = 0;
  for (e = table[i]; e != 0; e = e->next) {
    if (e->key == key) break;
  }
  pthread_mutex_unlock(&mutex); //解锁
  return e;
}

然后需要在main函数中进行初始化。

int
main(int argc, char *argv[])
{
  pthread_t *tha;
  void *value;
  double t1, t0;
  pthread_mutex_init(&mutex, NULL); //初始化
  ......

目前隔离已经实现了,运行可以发现不会出现丢失key的情况了,但是速度还是比较慢的,这里主要原因就是只有一个锁。如下所示。

== Test ph_safe == make: 'ph' is up to date.
ph_safe: OK (20.0s) 
== Test ph_fast == make: 'ph' is up to date.
ph_fast: FAIL (31.4s) 
    Parallel put() speedup is less than 1.25x

这个锁会直接把整个操作都限制,所以改进思路就是每个bucket都有一个单独的锁。由于哈希表中,不同的 bucket 是互不影响的,一个 bucket 处于修改未完全的状态并不影响 put 和 get 对其他 bucket 的操作,所以实际上只需要确保两个线程不会同时操作同一个 bucket 即可,并不需要确保不会同时操作整个哈希表。

pthread_mutex_t mutexs[NBUCKET]; //为每一个BUCKET都创建一个锁

static 
void put(int key, int value)
{
  
  int i = key % NBUCKET;
  pthread_mutex_lock(&mutexs[i]); //上锁
  // is the key already present?
  struct entry *e = 0;
  for (e = table[i]; e != 0; e = e->next) {
    if (e->key == key)
      break;
  }
  if(e){
    // update the existing key.
    e->value = value;
  } else {
    // the new is new.
    
    insert(key, value, &table[i], table[i]);
  }
  pthread_mutex_unlock(&mutexs[i]); //解锁
}

static struct entry*
get(int key)
{
  
  int i = key % NBUCKET;
  pthread_mutex_lock(&mutexs[i]); //上锁
  struct entry *e = 0;
  for (e = table[i]; e != 0; e = e->next) {
    if (e->key == key) break;
  }
  pthread_mutex_unlock(&mutexs[i]); //解锁
  return e;
}

还要记得在main里面全部初始化一下。

int
main(int argc, char *argv[])
{
  pthread_t *tha;
  void *value;
  double t1, t0;
  
  //初始化所有的线程锁
  for(int i = 0; i < NBUCKET;i++){
    pthread_mutex_init(&mutexs[i], NULL);
  }
  

然后测试发现已经可以通过ph_fast了。

== Test ph_safe == gcc -o ph -g -O2 notxv6/ph.c -pthread
ph_safe: OK (10.9s) 
== Test ph_fast == make: 'ph' is up to date.
ph_fast: OK (22.1s) 

Barrier(moderate)

最后一个实验是要加一个“屏障”,这个屏障的作用是要做一个线程同步,也就是当所有的(nthread)进程都出发了barrier,再唤醒所有的线程。没达到这个目的,所有线程遇到barrier后都进入休眠。

唤醒所有进程和休眠主要使用下面两个函数。

pthread_cond_broadcast(&bstate.barrier_cond); //唤醒所有
pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex);//休眠

因此,只需要在每次调用 barrier() 函数时进行计数(round)并休眠上锁,直到其等于 nthread ,唤醒所有线程。

注意:为了保证 nthread 变量不会出现多线程间的覆盖修改,所以也要上进程锁。

static void 
barrier(void)
{
  pthread_mutex_lock(&bstate.barrier_mutex); //上锁
  if(++bstate.nthread < nthread) {
    pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex);
  } else {
    bstate.nthread = 0;
    bstate.round++;
    pthread_cond_broadcast(&bstate.barrier_cond);
  }
  pthread_mutex_unlock(&bstate.barrier_mutex); //解锁
}

然后将所有实验都进行测试,全部通过。

jimmy@ubuntu:~/xv6-test/xv6-labs-2020$ ./grade-lab-thread 
make: 'kernel/kernel' is up to date.
== Test uthread == uthread: OK (2.6s) 
== Test answers-thread.txt == answers-thread.txt: OK 
== Test ph_safe == gcc -o ph -g -O2 notxv6/ph.c -pthread
ph_safe: OK (10.9s) 
== Test ph_fast == make: 'ph' is up to date.
ph_fast: OK (22.1s) 
== Test barrier == make: 'barrier' is up to date.
barrier: OK (104.1s) 
== Test time == 
time: OK 
Score: 60/60

总结

这个实验总体来说难度不高,主要在于理解线程间的切换、隔离、同步的方法。
[1]. https://pdos.csail.mit.edu/6.S081/2020/labs/thread.html

  • 14
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值