mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 08:06:32 +00:00
add ipc pipe (#28)
This commit is contained in:
@ -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
|
||||
|
@ -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},
|
||||
|
||||
};
|
||||
// 总共的内建命令数量
|
||||
|
32
user/apps/shell/cmd_test.c
Normal file
32
user/apps/shell/cmd_test.c
Normal 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;
|
||||
}
|
4
user/apps/shell/cmd_test.h
Normal file
4
user/apps/shell/cmd_test.h
Normal file
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include "cmd.h"
|
||||
int shell_pipe_test(int argc, char **argv);
|
Reference in New Issue
Block a user