MIB 6.1810实验Xv6 and Unix utilities(3)pingpong

Mit6.S081-实验1-Xv6 and Unix utilities-pingpong问题_Isana_Yashiro的博客-CSDN博客

Write a user-level program that uses xv6 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 "<pid>: received ping", where <pid> 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 "<pid>: received pong", and exit. Your solution should be in the file user/pingpong.c.

难度:easy

题目:写一个pingpong.c 实现系统调用ping-pong,实现进程之间的管道通信。

管道(pipe)是一种最基本的进程间通信机制。

管道分为 读出端写入端 两个部分,进程可以向写端写入数据,也可以从读端读出数据。通过pipe系统调用,内核会为用户进程创建管道,同时返回两个文件描述符,用以描述管道的读写端,

cpp 复制代码
int p[2];
int ret;
ret = pipe(p); /*正常创建后,p[1]为管道写入端,p[0]为管道读出端*/ 

通过文件描述符,可以向管道中写入和读取数据

cpp 复制代码
int write = write(p[1], buffer, n);
int read = read(p[0], buffer, n);

进程通常只持有某个管道的读出端或者写入端,因此使用的时候需要将另一端关闭。

实现:

cd user

vim pingpong.c

make qemu

pingpong.c

cpp 复制代码
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int main() {
    int p[2];
    int pid;
    pipe(p);
    pid = fork();
    char text[10];
    if (pid == 0) {
        read(p[0],text,10);
        printf("%d: received %s\n", getpid(),text);
        write(p[1],"pong",10);
        exit(0);
    } else { 
        write(p[1],"ping",10);
        wait(0);//父进程阻塞,等待子进程读取
        read(p[0],text,10);
        printf("%d:received %s\n",getpid(),text);

        exit(0);
    }
}

补充:管道的使用示例

cpp 复制代码
int p[2];
pipe(p);
int pid=fork();
if(pid>0){
    close(p[READ]);
    write(p[1],...);
    close(p[WRITE]);
    wait(0);
}else{
    close(p[WRITE]);
    read(p[READ],...);
    close(p[READ]);
    exit(0);
}

p是2个整数的数组

pipe(p)创建一个一个管道,p表示文件标识符的数组,可以进行管道操作。

p[0]用于读取,p[1]用于写入。

fork()函数用来创建一个子进程。如果成功创建,将返回两次,在父进程中返回子进程的PID(大于0),在子进程中返回0。

read如果读不到数据就会进入阻塞状态。所以先执行父进程,在执行子进程。

输出乱序问题

所以如果不对两个字串读出和写入过程进行限制,会导致资源访问的冲突。因此,我们采取调用函数wait()的方式,保证同一时刻管道内只有一个资源的写入和读取。

注意:在linux系统中:父进程的wait()一定和子进程的exit()成对出现配合使用。同时,父进程也需要调用exit(0)

  • exit() :子进程调用 exit() 函数来正常终止自己的执行。exit() 函数将子进程的状态传递给父进程。子进程可以通过传递一个整数状态值来向父进程报告它的终止状态。这个状态值通常代表了子进程的结束状态,比如返回码或错误码。

  • wait() :父进程可以调用 wait() 函数等待子进程的结束。当父进程调用 wait() 时,它会被阻塞,直到它的一个子进程结束。一旦子进程结束,父进程就会收到关于子进程终止状态的信息。父进程可以通过 wait() 获取子进程的终止状态,并且清理子进程的资源。

相关推荐
phoenix09812 小时前
Linux入门DAY27
linux·运维·服务器
xw53 小时前
免费的个人网站托管-PinMe篇
服务器·前端
egoist20234 小时前
【Linux仓库】进程创建与进程终止【进程·柒】
linux·运维·服务器·进程创建·写时拷贝·进程终止
大锦终4 小时前
【Linux】文件系统
linux·服务器·c++
Kyln.Wu5 小时前
【python实用小脚本-190】Python一键删除PDF任意页:输入页码秒出干净文件——再也不用在线裁剪排队
服务器·python·pdf
枫叶丹46 小时前
【Qt开发】常用控件(五)
服务器·前端·qt
励志五个月成为嵌入式糕手7 小时前
0819 使用IP多路复用实现TCP并发服务器
java·服务器·tcp/ip
华纳云IDC服务商7 小时前
服务器Linux防火墙怎样实现访问控制
linux·运维·服务器
Viking_bird16 小时前
centos 7.5 + Hadoop 3.2.4 集群搭建
linux·运维·服务器·hadoop·centos
Lovyk19 小时前
基于 Ansible 与 Jinja2 模板的 LNMP 环境及 WordPress 自动化部署实践
linux·运维·服务器·自动化·ansible