aboutsummaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-12 17:12:41 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-12 17:12:41 +0200
commit65006d0c14d3efa647b3c866ab54bdb1749fa31d (patch)
tree716de58438e96bde6a5553a8dd2a44620f529617 /src/eval
parent9446bb78646bef4e3449a59a9e01118b025402bf (diff)
downloadminishell-65006d0c14d3efa647b3c866ab54bdb1749fa31d.tar.gz
minishell-65006d0c14d3efa647b3c866ab54bdb1749fa31d.tar.bz2
minishell-65006d0c14d3efa647b3c866ab54bdb1749fa31d.zip
Added concurrent pipeline (not working with minishell_test for some obscure reason)
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/cmd.c37
-rw-r--r--src/eval/op.c38
2 files changed, 56 insertions, 19 deletions
diff --git a/src/eval/cmd.c b/src/eval/cmd.c
index 44f22b6..9745632 100644
--- a/src/eval/cmd.c
+++ b/src/eval/cmd.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
-/* Updated: 2020/09/11 19:10:20 by charles ### ########.fr */
+/* Updated: 2020/09/12 17:04:23 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,7 @@
pid_t g_child_pid = -1;
int g_last_status = 0;
+
void token_debug(void *v);
/*
@@ -25,16 +26,22 @@ void token_debug(void *v);
*/
int fork_wrap(
- int fds[2],
- void *passed,
- int (*wrapped)(void *param))
+ int fds[2],
+ void *passed,
+ int (*wrapped)(void *param),
+ pid_t *child_pid
+)
{
int status;
- pid_t child_pid;
+ bool waiting;
+ pid_t pid;
- if ((child_pid = fork()) == -1)
+ waiting = child_pid == NULL;
+ if (waiting)
+ child_pid = &pid;
+ if ((*child_pid = fork()) == -1)
return (EVAL_FATAL);
- if (child_pid == 0)
+ if (*child_pid == 0)
{
if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) ||
(fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1))
@@ -43,10 +50,15 @@ int fork_wrap(
exit(EXIT_FAILURE);
exit(status);
}
- g_child_pid = child_pid;
- wait(&child_pid);
- close(fds[FD_WRITE]);
- return (WEXITSTATUS(child_pid));
+ g_child_pid = *child_pid;
+ if (waiting)
+ {
+ waitpid(*child_pid, child_pid, 0);
+ close(fds[FD_WRITE]);
+ /* close(fds[FD_READ]); */
+ return (WEXITSTATUS(*child_pid));
+ }
+ return (0);
}
int forked_cmd(void *void_param)
@@ -104,7 +116,6 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
param.builtin = builtin_search_func(argv[0]);
if (param.builtin == NULL)
{
- // check env local for PATH
param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]);
if (param.exec_path == NULL)
{
@@ -118,7 +129,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
param.argv = argv;
param.env = env;
- status = fork_wrap(fds, &param, &forked_cmd);
+ status = fork_wrap(fds, &param, &forked_cmd, NULL);
ft_split_destroy(argv);
g_last_status = status;
return (status);
diff --git a/src/eval/op.c b/src/eval/op.c
index 9a37736..a39e380 100644
--- a/src/eval/op.c
+++ b/src/eval/op.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 15:27:22 by charles #+# #+# */
-/* Updated: 2020/09/10 20:28:57 by charles ### ########.fr */
+/* Updated: 2020/09/12 17:00:11 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,6 +28,26 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
pipe(p);
left_fds[FD_WRITE] = p[FD_WRITE];
right_fds[FD_READ] = p[FD_READ];
+
+ pid_t left_pid;
+ pid_t right_pid;
+ eval_forked(left_fds, env, path, ast->op.left, &left_pid);
+ eval_forked(right_fds, env, path, ast->op.right, &right_pid);
+
+ pid_t finished;
+ finished = wait(NULL);
+ close(p[FD_READ]);
+ close(p[FD_WRITE]);
+ if (finished == left_pid)
+ {
+ /* waitpid(right_pid, &right_pid, 0); */
+ }
+ else if (finished == right_pid)
+ {
+ kill(left_pid, SIGKILL);
+ /* waitpid(left_pid, &left_pid, 0); */
+ }
+ return (0);
}
if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL)
return (EVAL_FATAL);
@@ -41,7 +61,7 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast)
int wrapped_eval(void *void_param)
{
- t_fork_param_parent *param;
+ t_fork_param_args *param;
param = void_param;
return (eval(param->fds, param->env, param->path, param->ast));
@@ -49,18 +69,24 @@ int wrapped_eval(void *void_param)
int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast)
{
- t_fork_param_parent param;
int status;
if ((status = redir_extract(&ast->redirs, env, fds)) != 0)
return (status);
+ ast->tag ^= AST_PARENT;
+ return (eval_forked(fds, env, path, ast->parent_ast, NULL));
+}
+
+int eval_forked(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid)
+{
+ t_fork_param_args param;
+
param.fds[0] = fds[0];
param.fds[1] = fds[1];
param.env = env;
param.path = path;
- ast->tag ^= AST_PARENT;
- param.ast = ast->parent_ast;
- return (fork_wrap(fds, &param, wrapped_eval));
+ param.ast = ast;
+ return (fork_wrap(fds, &param, wrapped_eval, child_pid));
}
int eval(int fds[2], t_env env, t_path path, t_ast *ast)