add ipc pipe (#28)

This commit is contained in:
zzy666-hw
2022-08-20 21:47:41 +08:00
committed by GitHub
parent fd9d001d23
commit 554b73ec99
16 changed files with 283 additions and 5 deletions

View File

@ -1,4 +1,4 @@
all: shell.o cmd.o cmd_help.o
all: shell.o cmd.o cmd_help.o cmd_test.o
ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/shell $(shell find . -name "*.o") $(shell find $(sys_libs_dir) -name "*.o") -T shell.lds
@ -9,5 +9,8 @@ shell.o: shell.c
cmd.o: cmd.c
gcc $(CFLAGS) -c cmd.c -o cmd.o
cmd_test.o: cmd_test.c
gcc $(CFLAGS) -c cmd_test.c -o cmd_test.o
cmd_help.o: cmd_help.c
gcc $(CFLAGS) -c cmd_help.c -o cmd_help.o

View File

@ -12,6 +12,7 @@
#include <libc/sys/wait.h>
#include <libc/sys/stat.h>
#include "cmd_help.h"
#include "cmd_test.h"
// 当前工作目录在main_loop中初始化
char *shell_current_path = NULL;
@ -34,6 +35,7 @@ struct built_in_cmd_t shell_cmds[] =
{"about", shell_cmd_about},
{"free", shell_cmd_free},
{"help", shell_help},
{"pipe", shell_pipe_test},
};
// 总共的内建命令数量

View File

@ -0,0 +1,32 @@
#include "cmd_test.h"
#include <libc/stdio.h>
#include <libc/stdlib.h>
#include <libc/string.h>
#include <libc/unistd.h>
int shell_pipe_test(int argc, char **argv)
{
int ret = -1;
int fd[2];
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;
}
pid = fork();
if (0 == pid) {
// close(fd[0]);
ret = write(fd[1], msg, strlen(msg));
exit(0);
} else {
// close(fd[1]);
ret = read(fd[0], buf, sizeof(buf));
printf("parent read %d bytes data: %s\n", ret, buf);
}
return 0;
}

View File

@ -0,0 +1,4 @@
#pragma once
#include "cmd.h"
int shell_pipe_test(int argc, char **argv);