mit 6.s081 Lab1 pingpong

学习记录

目录

题目:

1.Use pipe to create a pipe

2.Use fork to create a child

3.Use read to read from a pipe, and write to write to a pipe

出现乱序问题:


题目:

1.Use pipe to create a pipe

int pipe(int fd[2]);

pipe()函数创建管道。两个文件描述符fd[0]和fd[1]分别构成管道的两端。fd[1]写操作,fd[0]读操作,往fd[1]写入的数据可以从fd[0]读出。创建成功,返回0;若出错,返回-1。

管道是单向传输,题目要求父进程给子进程发送一个字节,之后子进程也要给父进程一个字节。因此要两个管道

int p1[2],p2[2];//读写文件描述符记录在该数组中
    // 创建管道,成功返回0,失败返回-1
    //管道是单向的,建两个管道,一个父向子传递信息,一个子向父传递信息
    //pipe(p1);
    //pipe(p2);
    if(pipe(p1)==-1||pipe(p2)==-1){
        printf("error:Can't create the pipe!");
        exit(1);
    }

2.Use fork to create a child

fork()函数会新生成一个进程,调用 fork 函数的进程为父进程,新生成的进程为子进程。在父进程中返回子进程的 pid,在子进程中返回 0,失败返回-1。

int pid;//pid_t定义进程号类型,不行就用int
pid=fork();//pid保存fork()返回进程号,fork()创建一个子进程。fork调用失败返回-1;返回0,当前运行进程为子进程

接着是if-else语句,判断是子进程还是父进程,亦或是进程创建失败。接下来的读写代码就写在里面。如果是在子进程中,则pid为0,若pid大于0,则在父进程中。

if(pid==-1){
        printf("error: 创建进程失败 \n");
        exit(1);
    }else if(pid==0){
        //child
        //即将进行的读写操作
        exit(0);//退出进程
    }else{
        //parent
        
        exit(0);
    }

3.Use read to read from a pipe, and write to write to a pipe

涉及write()和read()函数的用法。参考

write()函数

函数原型:ssize write(int fd,const void * buf,size_t count);

fd: 文件描述符。

buf: 定义的缓冲区,是一个指针,指向一段内存。

count: 写入文件的字节数。

返回值:成功返回写入文件的字节数,失败返回-1。

read()函数

函数原型: ssize read(int fd, void *buf, size_t count);

fd: 文件描述符,读到文件末尾返回0。

buf: 是接收数据的缓冲区。

count: 读取字节数的大小,不能超过buf的大小。

返回值:成功返回实际读取的字节数,失败返回-1。

另外,“子进程会继承父进程的文件描述符,从而可以访问同一个管道。”

我们可以关闭不需要的管道端。

具体理解可以参考这篇文章。下面是该文章部分很有启发的截图:

本题是利用两个管道实现双向通信。见上图的第三幅图。

fork出的子进程和父进程几乎一样,除了pid。所以父进程有pipe()创建的文件描述符p1[0]和p1[1]构成管道两端,子进程也有p1[0]和p1[1]。p1[1]是写端,p1[0]是读端。

1)父进程传输1字节给子进程:管道是父->子,父进程写数据,子进程读数据,因此,关闭父进程不用的读端和子进程不用的写端。

if(pid==-1){
        printf("error: 创建进程失败 \n");
        exit(1);
    }else if(pid==0){
        //child
        close(p1[1]);//关闭父->子的子的写端
        
        exit(0);
    }else{
        //parent
        close(p1[0]);//关闭父->子通道的父的读端
        
        exit(0);
    }

2) 子进程传输1字节给父进程:管道是子->父,子进程写数据,父进程读数据,因此,关闭子进程不用的读端和父进程不用的写端。

if(pid==-1){
        printf("error: 创建进程失败 \n");
        exit(1);
    }else if(pid==0){
        //child
        close(p1[1]);//关闭父->子的子的写端
        close(p2[0]);//关闭子->父的子的读端

        exit(0);
    }else{
        //parent
        close(p1[0]);//关闭父->子通道的父的读端
        close(p2[1]);//关闭子->父通道的父的写端

        exit(0);
    }

我的代码:

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
int main(int argc, char *argv[]){

    if(argc!=1){
        printf("error:the argument is wrong...");
    }

    //Use pipe to create a pipe
    int p1[2],p2[2];//读写文件描述符记录在该数组中
    // 创建管道,成功返回0,失败返回-1
    //管道是单向的,建两个管道,一个父向子传递信息,一个子向父传递信息
    //pipe(p1);
    //pipe(p2);
    if(pipe(p1)==-1||pipe(p2)==-1){
        printf("error:Can't create the pipe!");
        exit(1);
    }


    //Use fork to create a child
    int pid;//pid_t定义进程号类型,不行就用int
    pid=fork();//pid保存fork()返回进程号,fork()创建一个子进程。fork调用失败返回-1;返回0,当前运行进程为子进程
    char buf[]={'a'};//指向一段内存
    if(pid==-1){
        printf("error: 创建进程失败 \n");
        exit(1);
    //Use read to read from a pipe, and write to write to a pipe
    //父写子读,子写父读
    }else if(pid==0){
        //child
        close(p1[1]);//关闭父->子的子的写端
        close(p2[0]);//关闭子->父的子的读端
        read(p1[0],buf,1);//2.收到父进程传来的字节
        printf("%d: received ping\n",getpid());//Use getpid to find the process ID of the calling process
        write(p2[1],buf,1);//3.write the byte on the pipe to the parent,and exit
        exit(0);
    }else{
        //parent
        close(p1[0]);//关闭父->子通道的父的读端
        close(p2[1]);//关闭子->父通道的父的写端
        //1.The parent should send a byte to the child
        write(p1[1],buf,1);//题目要求传递一个字节 或strlen(buf)
        wait(0);//在父进程中读取管道信息之前应该等待子进程完成写入信息操作,所以在这里使用wait
        read(p2[0],buf,1);//4.the parent should read the byte from the child
        printf("%d: received pong\n",getpid());
        exit(0);
    }
}

复习一下xv6实验环境的相关操作

1.commit&push代码
2.打开docker,打开终端,打开容器 
docker start -i ubuntu
3.更新本地仓库
//进入原有代码目录
cd xv6_demo
//拉取远程仓库文件
git remote add origin <远程仓库地址>
git fetch origin
//远程仓库与本地文件合并
git merge origin/master
4.make qemu
5.测试程序
6.提交代码
./grade-lab-util <程序名>



出现乱序问题:

缺少wait(0).详见

wait(0)

wait(0)暂时停止目前进程的执行,直到信号来到或子进程结束,如果在调用wait(0)时子进程已经结束,则wait(0)会立即返回子进程结束状态值。

在父进程和子进程之间使用pipe管道传输数据时,需要等待子进程的传输完成后,父进程再进行读取。

修改之后输出就没问题了。

没问题辣( ̄▽ ̄)~■干杯□~( ̄▽ ̄)

  • 11
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值