2021 XV6 2:系统调用

1.trace

首先需要在usys.pl中加个入口,Makefile中得改一下,然后就是user.h中添加定义。proc.h的PCB里边得加个mask,然后fork里边要加一个mask继承。

在分发系统调用号的时候判断。

在syscall.c里边改成这样:

void syscall(void)
{
  int num;
  struct proc *p = myproc();

  num = p->trapframe->a7;
  if (num > 0 && num < NELEM(syscalls) && syscalls[num])
  {
    p->trapframe->a0 = syscalls[num]();
    if ((1 << num) & (p->mask))
    {
      printf("%d: syscall %s -> %d\n", p->pid, NAME[num], p->trapframe->a0);
    }
  }
  else
  {
    printf("%d %s: unknown sys call %d\n",
           p->pid, p->name, num);
    p->trapframe->a0 = -1;
  }
}

当然,得加个在sysproc.c里边加个系统调用的函数:

uint64
sys_trace(void)
{
  int mask;
  if (argint(0, &mask) < 0)
  {
    return -1;
  }
  myproc()->mask = mask;
  return 0;
}

2.sysinfo

写两个调用函数。

第一个是在kalloc.c写统计空闲页的:

uint64
freememNum(void)
{
  uint64 num=0;
  struct run *p=kmem.freelist;
  while (p)
  {
    num++;
    p=p->next;
  }
  return num*PGSIZE;
}

第二个是在proc.c统计非UNUSED进程数目的:

uint64
procNum(void)
{
  uint64 num=0;
  struct proc *p;
  for (p = proc; p < &proc[NPROC]; p++)
  {
    if (p->state==UNUSED)
    {
      num++;
    }
  }
  return NPROC-num;
}

最后在sysproc.c中实现sysinfo系统调用,拷贝到用户空间:

uint64
sys_sysinfo(void)
{
  struct sysinfo info;
  struct proc *p = myproc();
  
  info.freemem=freememNum();
  info.nproc=procNum();
  uint64 addr;
  if (argaddr(0, &addr)<0)
  {
    return -1;
  }
  if(copyout(p->pagetable, addr, (char *)&info, sizeof(info)) < 0)
      return -1;
  return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值