mit6.s081-21-Lab1/ Xv6 and Unix utilities

2023-05-16

sleep

Implement the UNIX program sleep for xv6; 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.

描述:实现一个提供sleep功能的unix程序。

解决思路:系统已经实现了sleep函数,直接调用即可。

代码:

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

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

  if(argc != 2){
    fprintf(2, "Usage: sleep times\n");
    exit(1);
  }
  
  ticks_num = atoi(argv[1]);
  sleep(ticks_num);

  exit(0);
}

pingpong

Write a program that uses UNIX system calls to ''ping-pong'' a byte between two processes over a pair of pipes, one for each direction. The parent should send a byte to the child; the child should print " : received ping", where is its process ID, write the byte on the pipe to the parent, and exit; the parent should read the byte from the child, print " : received pong", and exit. Your solution should be in the file user/pingpong.c.

描述:使用管道(pipe)实现ping-pong(即使用pipe实现父进程子进程之间的通信)

解决思路:fork + pipe + write/read即可,比较简单,要注意pipe会创建两个fd(读/写),fork后子进程会继承文件描述符。

代码:

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

int
main(int argc, char *argv[])
{
  int p[2]; // read, write
  char buf[2];
  if(argc != 1){
    fprintf(2, "Usage: pingpong\n");
    exit(1);
  }

  pipe(p);
  if (fork() == 0) { // child
    read(p[0], buf, 1);
    fprintf(1, "%d: received ping\n", getpid());
    write(p[1], "c", 1);
    exit(0);
  } else { // father
    write(p[1], "c", 1);
    read(p[0], buf, 1);
    fprintf(1, "%d: received pong\n", getpid());
  }

  exit(0);
}

primes

Write a concurrent version of prime sieve using pipes. This idea is due to Doug McIlroy, inventor of Unix pipes. The picture halfway down this page and the surrounding text explain how to do it. Your solution should be in the file user/primes.c.

描述:使用pipe实现素数筛。

sieve

解决思路:父进程产生2-35的数,然后子进程按以下算法进行筛选即可。

p = get a number from left neighbor
print p
loop:
    n = get a number from left neighbor
    if (p does not divide n)
        send n to right neighbor

代码:(若BUFSIZE过大会panic,排查后发现是stack只有一页。。。)

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

#define MAX_SEQ 35
#define BUFSIZE 100

int getline(int fd, char *buf, int max)
{
    int i, cc;
    char c;

    for (i = 0; i + 1 < max;)
    {
        cc = read(fd, &c, 1);
        if (cc < 1)
        {
            return 0;
        }
        if (c == '\n' || c == '\r')
            break;
        buf[i++] = c;
    }
    buf[i] = '\0';
    return i;
}

int getnum(char *buf, int *pos)
{
    int num = 0, i = *pos;
    while(buf[i] >= '0' && buf[i] <= '9') {
        num = num * 10 + buf[i] - '0';
        i++;
    }
    *pos = i - 1;
    return num;
}

int primer(int read_fd)
{
    char buf[BUFSIZE];
    int len = getline(read_fd, buf, BUFSIZE);
    // printf("%s\n", buf);
    close(read_fd);

    int pipe_fd[2];
    pipe(pipe_fd);

    int i = 0, first_print_flag = 1;
    int is_have = 0;
    int first_num = getnum(buf, &i), num_tmp;
    printf("prime %d\n", first_num);
    for (i = 0; i < len; ++i) {
        if (buf[i] >= '0' && buf[i] <= '9') {
            num_tmp = getnum(buf, &i);
            if (num_tmp % first_num == 0) {
                continue;
            }
            is_have = 1;
            break;
        }
    }
    if (is_have) {
        if (fork() == 0) { // child
            close(pipe_fd[1]);
            primer(pipe_fd[0]);
            exit(0);
        } else {
            close(pipe_fd[0]);
            for (i = 0; i < len; ++i) {
                if (buf[i] >= '0' && buf[i] <= '9') {
                    num_tmp = getnum(buf, &i);
                    if (num_tmp % first_num == 0) {
                        continue;
                    }
                    if (first_print_flag) {
                        fprintf(pipe_fd[1], "%d", num_tmp);
                        first_print_flag = 0;
                    } else {
                        fprintf(pipe_fd[1], " %d", num_tmp);
                    }
                }
            }
            fprintf(pipe_fd[1], "\n", num_tmp);
            close(pipe_fd[1]);
            wait(0);
        }
    } else {
        close(pipe_fd[0]);
        close(pipe_fd[1]);
    }
    return 0;
}

int main(int argc, char *argv[])
{
    int pipe_fd[2]; // read, write
    if (argc != 1)
    {
        fprintf(2, "Usage: primes\n");
        exit(1);
    }

    pipe(pipe_fd);
    if (fork() == 0)
    { // child
        close(pipe_fd[1]);
        primer(pipe_fd[0]);
        exit(0);
    }
    else
    { // father
        close(pipe_fd[0]);
        for (int i = 2; i <= MAX_SEQ; ++i)
        {
            fprintf(pipe_fd[1], "%d", i);
            if (i != MAX_SEQ)
            {
                fprintf(pipe_fd[1], " ", i);
            }
            else
            {
                fprintf(pipe_fd[1], "\n", i);
            }
        }
        close(pipe_fd[1]);
        wait(0);
    }

    exit(0);
}

find

Write a simple version of the UNIX find program: find all the files in a directory tree with a specific name. Your solution should be in the file user/find.c.

描述:实现find。

解决思路:魔改ls即可,改成dfs实现。

代码:

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

char*
fmtname(char *path)
{
  char *p;

  // Find first character after last slash.
  for(p=path+strlen(path); p >= path && *p != '/'; p--)
    ;
  p++;
  return p;
}

void
find(char *path, char *target)
{
  struct stat st;
  char buf[512], *p;
  int fd;
  struct dirent de;

  if (stat(path, &st) < 0) {
    fprintf(2, "find: cannot stat %s\n", path);
    return;
  }

  switch(st.type){
  case T_FILE:
    if (strcmp(fmtname(path), target) == 0) {
      printf("%s\n", path);
    }
    break;

  case T_DIR:
    if((fd = open(path, 0)) < 0){
      fprintf(2, "find: cannot open %s\n", path);
      return;
    }

    strcpy(buf, path);
    p = buf+strlen(buf);
    *p++ = '/';

    // many records
    while(read(fd, &de, sizeof(de)) == sizeof(de)){
      if(de.inum == 0)
        continue;
      
      if (!strcmp(".", de.name) || !strcmp("..",  de.name)) {
        continue;
      }

      memmove(p, de.name, strlen(de.name));
      p[strlen(de.name)] = 0;
      find(buf, target);
    }
    close(fd);
    break;
  }
}

int
main(int argc, char *argv[])
{
  if(argc != 3){
    fprintf(2, "Usage: find path keyword\n");
    exit(1);
  }

  find(argv[1], argv[2]);

  exit(0);
}

xargs

Write a simple version of the UNIX xargs program: read lines from the standard input and run a command for each line, supplying the line as arguments to the command. Your solution should be in the file user/xargs.c.

描述:实现xargs。

解决思路:对每行进行处理,因此加上上面的getline函数然后搭配上fork和exec就行(和shell类似)。

代码:

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

#define MAX_LEN 512

int getline(char *buf, int max)
{
  int i, cc;
  char c;

  for(i=0; i+1 < max; ){
    cc = read(0, &c, 1);
    if(cc < 1) {
      return 0;
    }
    if(c == '\n' || c == '\r')
      break;
    buf[i++] = c;
  }
  buf[i] = '\0';
  printf("str: %s\n", buf);
  return 1;
}

int
main(int argc, char *argv[])
{
  // echo hello too | xargs echo bye
  char buffer[MAX_LEN];
  char* argv_tmp[MAXARG];
  // minus "xargs"
  memcpy(argv_tmp, argv + 1, (argc - 1) * sizeof(char*));
  while (getline(buffer, MAX_LEN))
  {
    if (fork() == 0) {
      argv_tmp[argc - 1] = buffer;
      exec(argv_tmp[0], argv_tmp);
      exit(0);
    } else {
      wait(0);
    }
  }
  exit(0);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

mit6.s081-21-Lab1/ Xv6 and Unix utilities 的相关文章

随机推荐

  • 照相机成像原理 数码相机的成像原理

    照相机成像原理 数码相机的成像原理 1 1 数码相机 的成像原理 当打开相机的电源开关后 xff0c 主控程序芯片开始检查整个相机 xff0c 确定各个部件是否处于可工作状态 如果一切正常 xff0c 相机将处于待命状态 xff1b 若某一
  • MySQL 单表查询

    创建数据库并插入数据 创建表 xff0c 数据类型请自行查询 CREATE TABLE fruits id INT NOT NULL sid INT NOT NULL NAME CHAR 255 NOT NULL price DECIMAL
  • react ant Design pro Umi 项目左上角icon不显示

    今天本地运行项目没有问题 xff0c 打包发到远端发现logo icon不显示了 然后找了找资料说 LayoutSettings 哪里logo用链接图片可以本地图片有时候会加载异常 解决方法 xff1a 找到 app tsx 加个logo
  • ZeroMQ发布-订阅模式的应用(C++)

    我使用的ZeroMQ版本是4 2 0 xff0c 应用的是其发布 订阅模式 应该知道的细节 xff1a PUB SUB套接字是慢连接 xff0c 你无法得知SUB是何时开始接收消息的 就算你先打开了SUB套接字 xff0c 后打开PUB发送
  • Ubuntu 问题记录(1)| 关于卸载以源码方式安装的库

    Ubuntu 使用源码安装 lib 后 xff0c 如果要卸载 xff0c 则在 lib build 路径下使用 sudo make uninstall 之后再用 sudo updatedb 更新一下 lib 库 xff0c 再使用 loc
  • 注意字符数组最后会自动加\0

    今天做了一道考研题 规定数组大小为200 但是我没注意到后尾需要加 0 后来果断没有A过去 很伤心 反复不断地尝试怎么都不行 后来经一位仁兄点拨 瞬间豁然 include lt iostream gt include lt cstdio g
  • 在TypeScript中使用parseInt()

    在使用angular写一些东西的时候 xff0c 需要用到parseInt 方法来将时间戳转换成时分秒 xx时 xx分 xx秒 的格式 xff0c 但是因为angular所使用的是Typescript xff0c 而 parseInt st
  • CAS6.2.x ~ 准备(1)

    前言 CAS 企业单点登录 xff0c 目前最新版本是6 2 x Apereo 的 Central Authentication Service xff0c 通常称为CAS CAS是用于web的企业多语言单点登录解决方案 xff0c 并试图
  • MySql 安装,root初始化密码设置

    MySQL下载地址 xff1a https dev mysql com downloads mysql 2 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下 我的解压目录是 C mysql 8 0 12 winx64 34
  • Python游戏项目--外星人入侵(一)

    一 安装Pygame 在终端输入 xff1a pip install user pygame 二 开始游戏项目 xff08 1 xff09 创建Pygame窗口及响应用户输入 创建一个名为alien invasion py 的文件 xff0
  • SpringSecurity OAuth2 获取Token端点TokenEndpoint、Token授权TokenGranter接口 详解

    1 前言 在 授权服务器是如何实现授权的呢 xff1f 中 xff0c 我们可以了解到服务端实现授权的流程 xff0c 同时知道 xff0c 当授权端点AuthorizationEndpoint生成授权码时 xff0c 就会重定向到客户端的
  • Make文件中赋值等号的几种类型(:=,?=,=)

    今天有一位以前仅做过Android APP开发的同学突然间问我 xff0c 说Makefile中经常可以看见 xff1a 冒号等号 61 问号等号 61 和直接等号 61 这究竟有什么区别呢 xff1f 欢迎转载 xff0c 但是请注明原出
  • 支持nvidia GPU 的硬件编解码的ffmpeg编译记要

    支持nvidia GPU 的硬件编解码的ffmpeg编译记要 中间目录 xff1a out 1 x264 下载x264 stable zip unzip x264 stable zip cd x264 stable configure en
  • 软件工程学习笔记——第三周:结构化分析方法-1

    结构化分析方法的概念 结构化分析模型 结构化分析过程
  • GBK编码表

    说明 xff1a 比如第一个 34 顿号 34 的编码就是A1A2 GBK 汉字内码扩展规范 编码表 二 全国信息技术标准化技术委员会 汉字内码扩展规范 GBK Chinese Internal Code Specification 1 0
  • SSH 使用过程中,出现的问题以及解决办法

    1 ssh登陆提示 server unexpectedly closed network connection 在使用ssh登入Linux時 xff0c 卻發生了serverunexpectedly closed network conne
  • CentOS7安装vncserver

    1 关闭防火墙和selinux systemctl stop firewalld service setenforce 0 2 安装图形支持 yum groups install 34 GNOME Desktop 34 或yum group
  • Echarts tooltip加上单位并带着图例颜色

    模仿腾讯疫情地图 xff0c Y轴有个百分比 xff0c 也就是Y轴有单位 xff0c 使用JS代码如下 xff1a tooltip trigger 39 axis 39 formatter function params var relV
  • xv6调试

    窗口1作为xv6的运行窗口 make CPUS 61 1 qemu gdb 窗口2作为gdb调试窗口 gdb multiarch kernel kernel 进入gdb后执行 set confirm off set architecture
  • mit6.s081-21-Lab1/ Xv6 and Unix utilities

    sleep Implement the UNIX program sleep for xv6 your sleep should pause for a user specified number of ticks A tick is a