MIT6.S081学习总结-lab1: Xv6 and Unix utilities

本文详细记录了MIT操作系统课程6.S081的第一个实验——Xv6和Unix实用程序,涵盖了实现sleep系统调用、父子进程pingpong通信、素数生成器、find命令以及xargs命令的过程,深入探讨了管道通信和进程交互。
摘要由CSDN通过智能技术生成

最近学习MIT比较有名的操作系统课程6.S081,这门课程主要亮点就是设计精巧的lab了。这里记录一下lab1:Xv6 and Unix utilities.

1.sleep

用系统调用实现sleep

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

int
main(int argc, char* argv[])
{
   
    if (argc <= 1) {
   
        fprintf(2, "sleep: missing operand\n");
        exit(1);
    }
    int i;
    for (i = 1; i < argc; ++i) {
   
        int t = atoi(argv[i]);
        sleep(t);
    }
    exit(0);
}

2.pingpong

父子进程用管道通信,父进程发一个字节给子进程,子进程收到输出pid: received ping,然后发送一个字节给父进程,父进程收到后输出pid: received pong
用两个管道即可

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

int 
main(int argc, char* argv[])
{
   
    int pipe1[2];
    int pipe2[2];
    pipe(pipe1);
    pipe(pipe2);
    int pid = fork();
    if (pid == 0) {
     // child
        close(pipe1[1]);
        close(pipe2[0]);
        int childPid = getpid();
        char buf[8];
        int n = read(pipe1[0], buf, 8);
        if (n > 0) {
   
            fprintf(1, "%d: received ping\n", childPid);
            write(pipe2[1], "0", 1);
        }
        close(pipe1[0]);
        close(pipe2[1]);
    } else {
   
        close(pipe1[0]);
        close(pipe2[1]
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值