C-当我这样做时出现故障;cmd | cmd | cmd;退出“;
我必须用C语言重新编码一个shell,当我尝试这样做时,例如,C-当我这样做时出现故障;cmd | cmd | cmd;退出“;,c,shell,segmentation-fault,recode,C,Shell,Segmentation Fault,Recode,我必须用C语言重新编码一个shell,当我尝试这样做时,例如,ls | ls | ls;退出,我有一个分段错误。(malloc.c:3630:_int_malloc:Assertion`(unsigned long)(size)>=(unsigned long)(nb)`失败。 中止) 我的退出在我的ls | ls | ls结束之前运行,但是如果只有一个管道,它的工作就完成了。 如果你有一个想法,或者你想要更多的信息,请告诉我 以下是exec a(多)管道的功能: void exec_管道(ch
ls | ls | ls;退出
,我有一个分段错误。(malloc.c:3630:_int_malloc:Assertion`(unsigned long)(size)>=(unsigned long)(nb)`失败。
中止)
我的退出在我的ls | ls | ls
结束之前运行,但是如果只有一个管道,它的工作就完成了。
如果你有一个想法,或者你想要更多的信息,请告诉我
以下是exec a(多)管道的功能:
void exec_管道(char*multi)
{
字符**cmd;
国际*pfd;
int-pn;
智力状态;
int i;
pfd=NULL;
i=0;
pn=计数管道(多个);
cmd=my_str_to_wordtabpipe(多个);
pfd=初始pfd(pfd,pn);
执行管道指令(pfd、pn、cmd);
关闭pfd(pfd,pn);
而(i
首先在调试器中运行以实际定位崩溃。我知道它崩溃的原因,但我不知道如何解决它:我的“ls | ls | ls”在退出开始时还没有完成。您希望您的子进程在运行下一个命令之前退出?是的,我有一个带有“pn”管道的字符*,我用fork在一个循环中执行它们,然后我就有了这样的结果:虽然(ivoid exec_pipe(char *multi)
{
char **cmd;
int *pfd;
int pn;
int status;
int i;
pfd = NULL;
i = 0;
pn = count_pipe(multi);
cmd = my_str_to_wordtabpipe(multi);
pfd = init_pfd(pfd, pn);
exec_pipe_cmd(pfd, pn, cmd);
close_pfd(pfd, pn);
while (i < pn + 1)
{
wait(&status);
i++;
}
destroy_tabstr(&cmd);
free(pfd);
}
void exec_pipe_cmd(int *pfd, int pn, char **cmd)
{
int i;
int j;
i = 0;
j = 0;
while (cmd[i])
{
if (cmd[i][0] == '~')
if (!(cmd[i] = put_home(cmd[i])))
my_e_printf("No $HOME variable set.\n");
if (cmd[i])
cmd[i] = get_alias(cmd[i]);
fork_pipe(&cmd[i], pfd, pn, j);
j += 2;
i++;
}
}
void dup_pipe(char **cmd, int *pfd, int j)
{
if (*(cmd + 1))
if (dup2(pfd[j + 1], 1) < 0)
exit(-1);
if (j != 0)
if (dup2(pfd[j - 2], 0) < 0)
exit(-1);
}
void fork_pipe(char **cmd, int *pfd, int pn, int j)
{
char **tmp;
pid_t pid;
char *path;
tmp = my_str_to_wordtab(cmd[0]);
if ((pid = fork()) == 0)
{
path = get_cmd_path(tmp[0]);
if (my_strcmp(path, tmp[0]) == 0 && path[0] != '.'
&& path[0] != '~' && path[0] != '/')
path = NULL;
dup_pipe(cmd, pfd, j);
close_pfd(pfd, pn);
if (exec_builtins(tmp))
exit(0);
else if (!path || execve(path, tmp, g_env) == -1)
{
my_e_printf("%s: Command not found.\n", tmp[0]);
exit(-1);
}
}
destroy_tabstr(&tmp);
}
int count_pipe(char *str)
{
int k;
int i;
i = 0;
k = 0;
while (str[i])
{
if (str[i] == '|')
k++;
i++;
}
return (k);
}
int *init_pfd(int *pfd, int pn)
{
int i;
i = 0;
if ((pfd = malloc(sizeof(int) * (pn * 2))) == NULL)
exit(-1);
while (i < pn)
{
if (pipe(pfd + i * 2) < 0)
my_printf("An error occured when pipe creation\n");
i++;
}
return (pfd);
}
void close_pfd(int *pfd, int pn)
{
int i;
i = 0;
while (i < 2 * pn)
{
close(pfd[i]);
i++;
}
}
int count_pipe(char *str)
{
int k;
int i;
i = 0;
k = 0;
while (str[i])
{
if (str[i] == '|')
k++;
i++;
}
return (k);
}
int *init_pfd(int *pfd, int pn)
{
int i;
i = 0;
if ((pfd = malloc(sizeof(int) * (pn * 2))) == NULL)
exit(-1);
while (i < pn)
{
if (pipe(pfd + i * 2) < 0)
my_printf("An error occured when pipe creation\n");
i++;
}
return (pfd);
}
void close_pfd(int *pfd, int pn)
{
int i;
i = 0;
while (i < 2 * pn)
{
close(pfd[i]);
i++;
}
}