操作系统实验Mit6.S081笔记 Lab7:Multithreading

Uthread: switching between threads (moderate)

要求:

在本练习中,您将为用户级线程系统设计上下文切换机制,然后实现它。为了让您开始,您的 xv6 有两个文件 user/uthread.c 和 user/uthread_switch.S,以及 Makefile 中用于构建 uthread 程序的规则。uthread.c 包含大部分用户级线程包,以及三个简单测试线程的代码。threading 包缺少一些用于创建线程和在线程之间切换的代码。

您的工作是制定一个计划来创建线程并保存/恢复寄存器以在线程之间切换,并实施该计划。完成后, make Grade应该表明您的解决方案通过了 uthread测试。

完成后,在xv6上运行uthread时应该会看到以下输出(三个线程可能以不同的顺序启动):
$ make qemu

$ uthread
thread_a started
thread_b started
thread_c started
thread_c 0
thread_a 0
thread_b 0
thread_c 1
thread_a 1
thread_b 1

thread_c 99
thread_a 99
thread_b 99
thread_c: exit after 100
thread_a: exit after 100
thread_b: exit after 100
thread_schedule: no runnable threads
$

这个输出来自三个测试线程,每个线程都有一个循环,打印一行,然后将 CPU 交给其他线程。

但是,此时,如果没有上下文切换代码,您将看不到任何输出。

您需要将代码添加到thread_create()和 thread_schedule()在用户/ uthread.c和thread_switch在用户/ uthread_switch.S。一个目标是确保当thread_schedule()第一次运行给定线程时,该线程在自己的堆栈上执行传递给thread_create()的函数。另一个目标是确保thread_switch保存被切换的线程的寄存器,恢复被切换到的线程的寄存器,并返回到后一个线程的指令中上次停止的点。您必须决定在哪里保存/恢复寄存器;修改 struct thread来保存寄存器是一个很好的计划。您需要在thread_schedule 中添加对thread_switch的调用;您可以将您需要的任何参数传递给thread_switch,但目的是从线程t切换到 next_thread。

提示:

thread_switch 只需要保存/恢复被调用者保存寄存器。为什么?
可以在user/uthread.asm 中看到uthread 的汇编代码,可能方便调试。
要测试您的代码,使用riscv64-linux-gnu-gdb单步执行thread_switch可能会有所帮助 。您可以通过以下方式开始:
(gdb) file user/_uthread
从user/_uthread读取符号…
(gdb) b uthread.c:60

这会在uthread.c 的第 60 行设置一个断点。断点可能(也可能不会)在您运行uthread之前被触发 。怎么会这样?

一旦您的 xv6 shell 运行,输入“uthread”,gdb 将在第 60 行中断。现在您可以输入如下命令来检查uthread的状态:

(gdb) p/x *next_thread
使用“x”,您可以检查内存位置的内容:
(gdb) x/x next_thread->stack
您可以跳到thread_switch的开头,因此:

(gdb) b thread_switch
(gdb) c
您可以使用以下方法单步组装说明:

(gdb) si

代码:
/*
目的:为用户级线程系统设计上下文切换机制
*/

/*
user/uthread.c

新增头文件引用
*/
#include "kernel/riscv.h"
#include "kernel/spinlock.h"
#include "kernel/param.h"
#include "kernel/proc.h"

/*
user/uthread.c

struct thread增加成员struct context,用于线程切换时保存/恢复寄存器信息。
此处struct context即kernel/proc.h中定义的struct 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 threadContext;
};

/*
user/uthread.c

修改thread_switch函数定义。
*/
extern void thread_switch(struct context*, struct context*);

/*
user/uthread.c

修改thread_create函数,
修改ra保证当thread_schedule()第一次运行给定线程时,该线程在自己的堆栈上执行传递给thread_create()的函数。
修改sp保证栈指针位于栈顶(地址最高)。
*/
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;
  // YOUR CODE HERE
  t->threadContext.ra = (uint64)func;
  t->threadContext.sp = (uint64)(t->stack) + STACK_SIZE;
}

/*
user/uthread.c

修改thread_schedule,调用thread_switch,保存当前线程上下文,恢复新线程上下文。
*/
  if (current_thread != next_thread) {         /* switch threads?  */
    next_thread->state = RUNNING;
    t = current_thread;
    current_thread = next_thread;
    /* YOUR CODE HERE
     * Invoke thread_switch to switch from t to next_thread:
     * thread_switch(??, ??);
     */
    thread_switch(&t->threadContext, &current_thread->threadContext);
  } else
    next_thread = 0;

/*
user/uthread_switch.S

实现保存/恢复寄存器
*/
thread_switch:
	/* YOUR CODE HERE */
    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 */


结果:

在这里插入图片描述

Using threads (moderate)

将使用哈希表探索线程和锁的并行编程。
此任务使用 UNIX pthread线程库。
文件notxv6/ph.c包含一个简单的哈希表,如果在单个线程中使用该表是正确的,但在多个线程中使用时则不正确。在您的 xv6 主目录(可能是~/xv6-labs-2020)中,输入以下内容:
$ make ph
$ ./ph 1
请注意,为了构建ph,Makefile 使用您操作系统的 gcc,而不是 6.S081 工具。ph的参数指定在哈希表上执行 put 和 get 操作的线程数。运行一段时间后,ph 1会产生类似这样的输出:
100000 puts, 3.991 seconds, 25056 puts/second
0: 0 keys missing
100000 gets, 3.981 seconds, 25118 gets/second

您看到的数字可能与此示例输出相差两倍或更多,具体取决于计算机的速度、是否有多个内核以及是否忙于做其他事情。

ph运行两个基准测试。首先,它通过调用put()向哈希表添加大量键,并以每秒 puts 打印达到的速率。它使用get()从哈希表中获取键。它打印作为 puts 的结果应该在哈希表中但丢失的数字键(在这种情况下为零),并打印它实现的每秒获取次数。

您可以通过给它一个大于 1 的参数来告诉ph同时使用来自多个线程的哈希表。尝试ph 2:
$ ./ph 2
100000 puts, 1.885 seconds, 53044 puts/second
1: 16579 keys missing
0: 16579 keys missing
200000 gets, 4.322 seconds, 46274 gets/second

此ph 2输出的第一行表明,当两个线程同时向哈希表添加条目时,它们的总插入速率为每秒 53,044 次。这大约是运行ph 1 的单线程速率的两倍 。这是大约 2 倍的出色“并行加速”,正如人们可能希望的那样(即每单位时间产生两倍工作量的内核数量的两倍)。
然而,两行说16579 个键丢失表明大量应该在哈希表中的键不存在。也就是说,puts 应该将这些键添加到哈希表中,但是出了点问题。看看notxv6/ph.c,特别是put() 和insert()。

为了避免这一系列事件,在put和get中插入 lock 和 unlock 语句在 notxv6/ph.c 中,这样两个线程中丢失的键数始终为 0。相关的 pthread 调用是:

pthread_mutex_t lock; // declare a lock
pthread_mutex_init(&lock, NULL); // initialize the lock
pthread_mutex_lock(&lock); // acquire lock
pthread_mutex_unlock(&lock); // release lock
当make Grade表示您的代码通过了ph_safe测试时,您就完成了,这需要两个线程的零丢失键。此时可以通过ph_fast测试。

不要忘记调用pthread_mutex_init()。首先用 1 个线程测试您的代码,然后用 2 个线程测试它。是否正确(即您是否消除了丢失的钥匙?)?相对于单线程版本,双线程版本是否实现了并行加速(即单位时间内的总工作量更多)?

在某些情况下,并发put()在它们在哈希表中读取或写入的内存中没有重叠,因此不需要锁来相互保护。您能否更改ph.c以利用这种情况为某些put()获得并行加速?提示:每个散列桶的锁怎么样?

代码:
/*
notxv6/ph.c
*/

//增加定义互斥锁,每个桶一个锁
pthread_mutex_t lock[NBUCKET];

//初始化互斥锁。
for(int i = 0;i < NBUCKET; i++){
  pthread_mutex_init(&lock[i], NULL);
}

//对put操作加锁。
static 
void put(int key, int value)
{
  int i = key % NBUCKET;
  pthread_mutex_lock(&lock[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(&lock[i]);
}

结果:

在这里插入图片描述

Barrier(moderate)

要求:

在这个任务中,你将实现一个屏障:应用程序中的一个点,所有参与的线程必须等待,直到所有其他参与的线程也到达该点。您将使用 pthread 条件变量,这是一种类似于 xv6 的睡眠和唤醒的序列协调技术。

你应该在一台真正的计算机上做这个作业(不是 xv6,不是 qemu)。

文件notxv6/barrier.c包含一个损坏的屏障。

$ make 屏障
$ ./barrier 2
屏障:notxv6/barrier.c:42:线程:断言 `i == t’ 失败。
2 指定在屏障上同步的线程数( barrier.c中的nthread)。每个线程执行一个循环。在每次循环迭代中,一个线程调用barrier(),然后休眠随机数微秒。断言触发,因为一个线程在另一个线程到达屏障之前离开屏障。所需的行为是每个线程都阻塞在barrier() 中,直到它们的所有n 个线程都调用了 barrier()。
您的目标是实现所需的屏障行为。除了您在ph赋值中看到的锁原语之外,您还需要以下新的 pthread 原语;查看 此处和 此处 了解详细信息。

pthread_cond_wait(&cond, &mutex); // 在 cond 上休眠,释放锁互斥锁,在唤醒时获取
pthread_cond_broadcast(&cond); // 唤醒在 cond 上休眠的每个线程
确保您的解决方案通过make Grade的屏障测试。

pthread_cond_wait在调用时释放互斥锁,并在返回前重新获取互斥锁。
我们已经给了你barrier_init()。您的工作是实现 barrier()以便不会发生恐慌。我们已经为您定义了结构体屏障;它的字段供您使用。

有两个问题使您的任务复杂化:

你必须处理一连串的障碍调用,每个调用我们都会调用一个回合。 bstate.round记录当前回合。每次所有线程都到达屏障时,您应该增加bstate.round。
您必须处理这样一种情况,即一个线程在其他线程退出屏障之前围绕循环进行比赛。特别是,您从一轮到下一轮重复使用bstate.nthread变量。确保 在前一轮仍在使用bstate.nthread时,离开屏障并在循环中竞争的线程不会增加bstate.nthread。
使用一个、两个或两个以上的线程测试您的代码。

代码:
/*
先是抢一把锁,然后到达线程数加1,如果还有其他线程没到,就等待,
pthread_cond_wait会先释放锁再sleep; 
如果都已经到了,轮数加1,唤醒其他所有线程。
*/
static void 
barrier()
{
  pthread_mutex_lock(&(bstate.barrier_mutex));
  bstate.nthread++;
  if(bstate.nthread < nthread){
      pthread_cond_wait(&(bstate.barrier_cond),&(bstate.barrier_mutex));
  }
  else if(bstate.nthread == nthread){
      bstate.nthread = 0;
      bstate.round++;
      pthread_cond_broadcast(&(bstate.barrier_cond));
  }
  pthread_mutex_unlock(&(bstate.barrier_mutex));
}
结果:

在这里插入图片描述

make grade:

在这里插入图片描述

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class MainWindow(QMainWindow): def init(self, user_id): super().init() self.user_id = user_id self.initUI() # 打开串口 self.ser = serial.Serial('COM7', 9600, timeout=1) def initUI(self): # 创建用于显示员工信息的控件 self.info_label = QLabel("员工信息", self) self.info_label.move(100, 50) self.info_label.setStyleSheet("font-size: 24px; color: black; background-color: #eee; border-radius: 10px;") self.id_label = QLabel("员工ID:", self) self.id_label.move(70, 100) self.id_label.setStyleSheet("font-size: 18px; color: black;") self.name_label = QLabel("姓名:", self) self.name_label.move(70, 150) self.name_label.setStyleSheet("font-size: 18px; color: black;") self.six_label = QLabel("性别:", self) self.six_label.move(70, 200) self.six_label.setStyleSheet("font-size: 18px; color: black;") self.sfz_label = QLabel("身份证:", self) self.sfz_label.move(70, 250) self.sfz_label.setStyleSheet("font-size: 18px; color: black;") self.tel_label = QLabel("电话:", self) self.tel_label.move(70, 300) self.tel_label.setStyleSheet("font-size: 18px; color: black;") self.setFixedSize(800, 500) self.setWindowTitle('员工信息') # 查询员工信息 def query_employee(self, id): conn = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', database='jj_tset') cursor = conn.cursor() cursor.execute("SELECT * FROM employee_table WHERE user_id='%s'" % id) result = cursor.fetchone() conn.close() return result # 读取数据 def read_data(self): data = self.ser.readline() if data: # 解析数据 id = data.decode().strip() # 查询员工信息 result = self.query_employee(id) if result: # 更新UI界面 self.id_label.setText("员工ID:" + result[0]) self.name_label.setText("姓名:" + str(result[1])) self.six_label.setText("性别:" + result[2]) self.sfz_label.setText("身份证:" + str(result[3])) self.tel_label.setText("电话:" + result[4]) print(result[0],result[1],result[2],result[3],result[4]) else: # 显示空白信息 self.id_label.setText("员工ID:") self.name_label.setText("姓名:") self.six_label.setText("性别:") self.sfz_label.setText("身份证:") self.tel_label.setText("电话:") # 定时读取数据 QTimer.singleShot(100, self.read_data) def closeEvent(self, event): # 关闭串口 self.ser.close()用多线程改写代码,防止主线程阻塞
05-27
Here's an example of how you could use multithreading to prevent the main thread from blocking: ```python from PyQt5.QtCore import Qt, QThread, QTimer, pyqtSignal class SerialThread(QThread): dataReceived = pyqtSignal(str) def __init__(self, parent=None): super().__init__(parent) self.ser = serial.Serial('COM7', 9600, timeout=1) self.running = False def run(self): self.running = True while self.running: data = self.ser.readline().decode().strip() if data: self.dataReceived.emit(data) def stop(self): self.running = False self.wait() self.ser.close() class MainWindow(QMainWindow): def __init__(self, user_id): super().__init__() self.user_id = user_id self.initUI() self.serialThread = SerialThread(self) self.serialThread.dataReceived.connect(self.handle_data) self.serialThread.start() def initUI(self): # Create UI elements here def handle_data(self, data): # Handle incoming data here result = self.query_employee(data) if result: self.id_label.setText("员工ID:" + result[0]) self.name_label.setText("姓名:" + str(result[1])) self.six_label.setText("性别:" + result[2]) self.sfz_label.setText("身份证:" + str(result[3])) self.tel_label.setText("电话:" + result[4]) print(result[0],result[1],result[2],result[3],result[4]) else: self.id_label.setText("员工ID:") self.name_label.setText("姓名:") self.six_label.setText("性别:") self.sfz_label.setText("身份证:") self.tel_label.setText("电话:") def query_employee(self, id): # Query employee information from database pass def closeEvent(self, event): self.serialThread.stop() ``` In this example, a `SerialThread` is created to handle the serial communication. The `dataReceived` signal is emitted whenever new data is available. The `handle_data` method is called whenever the signal is emitted, and this method updates the UI with the relevant employee information. The `SerialThread` is started in the `MainWindow` constructor, and stopped in the `closeEvent` method.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值