匿名管道重构&增加IrqArch trait以及IrqFlags及其守卫 (#253)

* 实现匿名管道

* 增加IrqArch trait以及IrqFlags及其守卫

---------

Co-authored-by: longjin <longjin@RinGoTek.cn>
This commit is contained in:
hanjiezhou
2023-04-23 21:05:10 +08:00
committed by GitHub
parent 8a1e95abb5
commit f678331a33
14 changed files with 508 additions and 43 deletions

View File

@ -1,32 +1,83 @@
#include "cmd_test.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define buf_SIZE 256 // 定义消息的最大长度
int shell_pipe_test(int argc, char **argv)
{
int ret = -1;
int fd[2];
int fd[2], i, n;
pid_t pid;
char buf[512] = {0};
char *msg = "hello world";
ret = pipe(fd);
if (-1 == ret) {
printf("failed to create pipe\n");
return -1;
int ret = pipe(fd); // 创建一个管道
if (ret < 0)
{
printf("pipe error");
exit(1);
}
pid = fork();
if (0 == pid) {
// close(fd[0]);
ret = write(fd[1], msg, strlen(msg));
pid = fork(); // 创建一个子进程
if (pid < 0)
{
printf("fork error");
exit(1);
}
if (pid == 0)
{ // 子进程
close(fd[1]); // 关闭管道的写端
for (i = 0; i < 3; i++)
{ // 循环三次
char buf[buf_SIZE] = {0};
n = read(fd[0], buf, buf_SIZE); // 从管道的读端读取一条消息
if (n > 0)
{
printf("Child process received message: %s\n", buf); // 打印收到的消息
if (strcmp(buf, "quit") == 0)
{ // 如果收到的消息是"quit"
printf("Child process exits.\n"); // 打印退出信息
break; // 跳出循环
}
else
{ // 如果收到的消息不是"quit"
printf("Child process is doing something...\n"); // 模拟子进程做一些操作
usleep(100);
}
}
}
close(fd[0]); // 关闭管道的读端
exit(0);
} else {
// close(fd[1]);
ret = read(fd[0], buf, sizeof(buf));
printf("parent read %d bytes data: %s\n", ret, buf);
}
else
{ // 父进程
close(fd[0]); // 关闭管道的读端
for (i = 0; i < 3; i++)
{ // 循环三次
char *msg = "hello world";
if (i == 1)
{
msg = "how are you";
usleep(1000);
}
if (i == 2)
{
msg = "quit";
usleep(1000);
}
n = strlen(msg);
printf("Parent process send:%s\n", msg);
write(fd[1], msg, n); // 向管道的写端写入一条消息
if (strcmp(msg, "quit") == 0)
{ // 如果发送的消息是"quit"
printf("Parent process exits.\n"); // 打印退出信息
break; // 跳出循环
}
}
close(fd[1]); // 关闭管道的写端
wait(NULL); // 等待子进程结束
}
return 0;
}

View File

@ -16,8 +16,3 @@ int mstat(struct mstat_t *stat)
{
return syscall_invoke(SYS_MSTAT, (uint64_t)stat, 0, 0, 0, 0, 0, 0, 0);
}
int pipe(int *fd)
{
return syscall_invoke(SYS_PIPE, (uint64_t)fd, 0, 0,0,0,0,0,0);
}

View File

@ -65,7 +65,14 @@ pid_t fork(void)
{
return (pid_t)syscall_invoke(SYS_FORK, 0, 0, 0, 0, 0, 0, 0, 0);
}
/**
* @brief 调用匿名管道
*
* @return int 如果失败返回负数
*/
int pipe(int fd[2]){
return (int)syscall_invoke(SYS_PIPE, fd, 0, 0, 0, 0, 0, 0, 0);
}
/**
* @brief fork当前进程但是与父进程共享VM、flags、fd
*