课后作业6.1:块设备访问

第1关:编写一个显示文件内容的程序

用visual打开os/linux-0.11-lab/b,创建mycat.c程序

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#define MSG_LEN 80
#define FILE_FROM "/usr/root/hello.c"

int main()
{
  int fd, num;
  char msg[MSG_LEN+1];

  /* welcome */
  printf("\n\rHi, press ENTER to continue...\n\r");
  read(0, msg, 1);

  /* open and read the file */
  if ((fd=open(FILE_FROM,O_RDONLY,0)) < 0) {
    printf("Open file error.\n\r");
    return 1;
  }
  if ((num = read(fd, msg, MSG_LEN))<0) {
    printf("Read file error.\n\r");
    return 1;
  }
  close(fd);

  /* display on screen */
  msg[num] = '\0';
  printf("Content of file \"%s\":\n\r---\n\r%s---\n\r", FILE_FROM, msg);

  return 0;
}

打开0内核虚拟机输入:

  • mdir b:mycat.c
  • mcopy b:mycat.c .
  • uemacs /etc/rc

 编辑文件,保存的快捷键是 Ctrl-x,s ,退出的快捷键是 Ctrl-x, Ctrl-c

 然后输入

  • gcc mycat.c
  • ./a.out

即可测评(测评失败可多测几遍)。

第2关:版本 0 内核中文件的读取过程分析

 进入gdb调试之后

  • b do_execve
  • c
  • commands 2
  • p current->start_code+filename
  • end
  • c
  • c
  • b sys_read
  • c
  • c
  • b add_request
  • c
  • p *dev
  • p *req
  • b wake_up
  • c
  • b sched.c:141
  • c
  • c
  • c
  • p c

 

 

 

第四关.txt:

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值