1: 实现Sleep

  • https://pdos.lcs.mit.edu/6.828/2024/labs/util.html

任务描述

Implement a user-level sleep program for xv6, along the lines of the UNIX sleep command. Your sleep should pause for a user-specified number of ticks. A tick is a notion of time defined by the xv6 kernel, namely the time between two interrupts from the timer chip. Your solution should be in the file user/sleep.c.

实现一个用户级别的睡眠程序,停止指定的ticks

具体实现

代码如下:

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int main(int argc, char *argv[]) {
  int ticks;

  if (argc != 2) {
    fprintf(2, "Usage: sleep ticks\n");
    exit(1);
  }

  ticks = atoi(argv[1]);
  if (sleep(ticks) < 0) {
    fprintf(2, "sleep: %d failed\n", ticks);
    exit(1);
  }

  exit(0);
}

在Makefile中的UPROGS中加入一行内容: $U/_sleep\

_%: %.o $(ULIB) //这是一个 模式规则,它表示 _% 目标是由 %.o 和 $(ULIB) 依赖的文件构成。% 是一个模式匹配符,表示任意字符串。规则中的目标文件名必须以 _ 开头,并且与依赖的目标文件名一致,后缀是 .o。$(ULIB) 是某个库文件(可能是一个用户库文件),这个库是链接时的依赖。
	$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^ T //$U/user.ld 指定链接脚本,$U 可能是一个目录或路径,user.ld 是链接脚本,它定义了如何组织最终的输出文件(可能是将代码映射到特定的内存区域)。-o $@ 指定输出文件,$@ 是目标文件的名称。$^ 是依赖文件的列表,在这个规则中,它包括 .o 文件和 $(ULIB)(库文件)。
	$(OBJDUMP) -S $@ > $*.asm
	$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $*.sym