MIT6.828_HW8_User-level threads

MIT6.828_HW8_User-level threads

uthread creates two threads and switches back and forth between them. Each thread prints “my thread …” and then yields to give the other thread a chance to run.

thread结构体以及线程指针位置的理解,是完成这个 HW 的关键。

struct thread {
  int        sp;                /* saved stack pointer */
  char stack[STACK_SIZE];       /* the thread's stack */
  int        state;             /* FREE, RUNNING, RUNNABLE */
};

线程结构体指针

经过LAB4的洗礼,在根据给出的提示,写出实现代码并不难。不过在实现过程发现,在 .S 文件中竟然可以直接使用 .c 文件中的变量current_thread等,而且在讲义中还特意强调uthread_switch.S ends with .S,这引起了我的好奇,毕竟一直都没有探究汇编的细节。

  • .s: 汇编语言源程序;汇编
    • 小写的s文件,在后期阶段不在进行预处理操作,所以我们不能在这里面写预处理的语句在里面
  • .S:汇编语言源程序;预处理,汇编
    • 大写的S文件,还会进行预处理、汇编等操作,所以我们可以在这里面加入预处理的命令
	.globl thread_switch
thread_switch:
	/* YOUR CODE HERE */
	// C语言函数调用会压入下一条语句的 eip.
	pushal
	// eax 指向 sp
	movl current_thread, %eax
	// save sp
	movl %esp, (%eax)

	
	movl next_thread, %eax
	movl %eax, current_thread
	// restore sp
	movl (%eax), %esp
	
	popal

	movl $0x0, next_thread

	// popal 后esp指向return address
	ret				/* pop return address from stack */



	/* pop return address from stack */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值