Skip to content

Commit

Permalink
Refactor pdata pids
Browse files Browse the repository at this point in the history
  • Loading branch information
cfareste committed Jul 15, 2024
1 parent 47ba80c commit 030279d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
6 changes: 2 additions & 4 deletions inc/pdata_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: cfidalgo <cfidalgo@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 21:57:34 by arcanava #+# #+# */
/* Updated: 2024/07/12 17:27:27 by cfidalgo ### ########.fr */
/* Updated: 2024/07/15 15:59:56 by cfidalgo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,11 +15,9 @@

# include "token.h"
# include "context.h"
# include <stdlib.h>

typedef struct s_pdata
{
pid_t *pids;
int std_fds[2];
int fds[2];
int pipe_fds[2];
Expand All @@ -32,6 +30,6 @@ void close_pdata_fds(t_pdata *pdata);

void save_backup_stdfds(t_pdata *p_data);

void initialize_pdata(t_pdata *p_data, t_token *token);
void initialize_pdata(t_pdata *p_data);

#endif
6 changes: 2 additions & 4 deletions src/executor/builtins/utils_builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: cfidalgo <cfidalgo@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 21:58:46 by arcanava #+# #+# */
/* Updated: 2024/07/12 17:38:02 by cfidalgo ### ########.fr */
/* Updated: 2024/07/15 16:00:51 by cfidalgo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -57,7 +57,7 @@ void execute_cmd_builtin(t_pdata *pdata, t_token *token, t_context *context)
ft_bzero(&new_pdata, sizeof(t_pdata*));
if (!pdata)
{
initialize_pdata(&new_pdata, token);
initialize_pdata(&new_pdata);
pdata = &new_pdata;
}
listen_signals(SUBPROCESS, SUBPROCESS);
Expand All @@ -71,8 +71,6 @@ void execute_cmd_builtin(t_pdata *pdata, t_token *token, t_context *context)
context->err_code = execute_builtin(token->args[0], token, context);
redirect_fds(pdata->std_fds[READ_FD], pdata->std_fds[WRITE_FD]);
close_pdata_fds(pdata);
if (new_pdata.pids)
free(pdata->pids);
}

int check_invalid_chars(char *identifier)
Expand Down
37 changes: 18 additions & 19 deletions src/executor/executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: cfidalgo <cfidalgo@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 21:58:56 by arcanava #+# #+# */
/* Updated: 2024/07/14 20:48:09 by cfidalgo ### ########.fr */
/* Updated: 2024/07/15 16:05:26 by cfidalgo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,7 +32,7 @@ void execute_subshell(t_pdata *pdata, t_token *token, t_context *context)
ft_bzero(&new_pdata, sizeof(t_pdata*));
if (!pdata)
{
initialize_pdata(&new_pdata, token);
initialize_pdata(&new_pdata);
pdata = &new_pdata;
}
save_backup_stdfds(pdata);
Expand All @@ -56,8 +56,6 @@ void execute_subshell(t_pdata *pdata, t_token *token, t_context *context)
context->err_code = wait_child_processes(pid, 1);
redirect_fds(pdata->std_fds[READ_FD], pdata->std_fds[WRITE_FD]);
close_pdata_fds(pdata);
if (new_pdata.pids)
free(pdata->pids);
}

void execute_pipe_temp(t_pdata *pdata, t_token *token, t_context *context)
Expand All @@ -76,46 +74,47 @@ void execute_pipe(t_token *token, t_context *context)
{
t_token *curr_token;
t_pdata pdata;
pid_t *pids;
int i;
int last_idx;
int last;

i = 0;
last_idx = token->tokens.amount - 1;
last = token->tokens.amount - 1;
curr_token = token->tokens.token;
initialize_pdata(&pdata, token);
pids = safe_calloc((token->tokens.amount + 1) * sizeof(pid_t));
initialize_pdata(&pdata);
while (i < token->tokens.amount && curr_token)
{
if (i < token->tokens.amount - 1 && pipe(pdata.pipe_fds) == -1)
syserr(EMFILE);
parse_fds(i, token->tokens.amount, &pdata, curr_token);
pdata.pids[i] = fork();
if (pdata.pids[i] == -1)
pids[i] = fork();
if (pids[i] == -1)
syserr(EAGAIN);
else if (pdata.pids[i] == 0)
else if (pids[i] == 0)
execute_pipe_temp(&pdata, curr_token, context);
close_pipe(pdata.fds);
curr_token = curr_token->next;
i++;
}
context->err_code = \
wait_child_processes(pdata.pids[last_idx], token->tokens.amount);
free(pdata.pids);
context->err_code = wait_child_processes(pids[last], token->tokens.amount);
free(pids);
}

void execute_cmd(t_token *token, t_context *context)
{
t_pdata pdata;
pid_t pid;

initialize_pdata(&pdata, token);
initialize_pdata(&pdata);
parse_fds(0, 1, &pdata, token);
pdata.pids[0] = fork();
if (pdata.pids[0] == -1)
pid = fork();
if (pid == -1)
syserr(EAGAIN);
else if (pdata.pids[0] == 0)
else if (pid == 0)
execute_command(&pdata, token, context);
close_pipe(pdata.fds);
context->err_code = wait_child_processes(pdata.pids[0], 1);
free(pdata.pids);
context->err_code = wait_child_processes(pid, 1);
}

void execute_list(t_token *token, t_context *context)
Expand Down
6 changes: 2 additions & 4 deletions src/executor/pdata_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: cfidalgo <cfidalgo@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 21:59:04 by arcanava #+# #+# */
/* Updated: 2024/07/14 20:15:06 by cfidalgo ### ########.fr */
/* Updated: 2024/07/15 15:59:43 by cfidalgo ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -59,9 +59,8 @@ void save_backup_stdfds(t_pdata *p_data)
syserr(EBUSY);
}

// TODO: Dont initialize the pids here and do it in the needed function
// TODO: Safe initialization (if p_data is null, create one, or some approach like that, to prevent doing null checks)
void initialize_pdata(t_pdata *p_data, t_token *token)
void initialize_pdata(t_pdata *p_data)
{
ft_bzero(p_data, sizeof(t_pdata));
p_data->last_pipe = -1;
Expand All @@ -71,5 +70,4 @@ void initialize_pdata(t_pdata *p_data, t_token *token)
p_data->pipe_fds[WRITE_FD] = -1;
p_data->std_fds[READ_FD] = -1;
p_data->std_fds[WRITE_FD] = -1;
p_data->pids = safe_calloc((token->tokens.amount + 1) * sizeof(pid_t));
}

0 comments on commit 030279d

Please sign in to comment.