- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经编写了自己的 shell 并编写了一个处理三重管道的函数,但是我的 shell 在 execve 之后退出时遇到了问题。我认为问题是我需要多花点时间?但我不完全确定在哪里,因为这可以完美地执行管道程序。此实现中也没有使用 wait(2),不确定这是否也与它有关。谢谢
int fd[2];
int fd2[2];
if (pipe(fd) == -1)
{
perror("ERROR CREATING PIPE");
return;
}
pid_t pid = fork();
if (args4[0] != NULL)
{
switch(pid)
{
case -1:
printf("%s\n", "fail to fork");
return;
case 0:
if (pipe(fd2) == -1)
{
perror("ERROR CREATING PIPE");
return;
}
switch(pid = fork())
{
case -1:
printf("%s\n", "fail to fork");
return;
break;
case 0:
if ( dup2(fd2[1], STDOUT_FILENO) == -1 )
{
printf("%s\n", "fail to dup");
return;
}
close(fd2[0]);
close(fd2[1]);
execve(args2[0], args2, environ);
exit(1);
default:
if ( dup2(fd2[0], STDIN_FILENO) == -1 )
{
printf("%s\n", "fail to dup");
return;
}
if ( dup2(fd[1], STDOUT_FILENO) == -1 )
{
printf("%s\n", "fail to dup");
return;
}
close(fd2[0]);
close(fd2[1]);
execve(args3[0], args3, environ);
exit(2);
}
exit(3);
default:
if ( dup2(fd[0], STDIN_FILENO) == -1)
{
printf("%s\n", "fail to dup");
return;
}
close(fd[0]);
close(fd[1]);
printf("%s\n", "4");
execve(args4[0], args4, environ);
exit(4);
}
}
最佳答案
你 fork 了两次,所以你有 3 个进程。它们中的每一个都被 execve 创建的相应进程替换。你永远不会到达 exit() 语句,因为 execve() 不返回(成功时)。这是您在没有管道的情况下重写的代码(您清楚地了解如何设置管道)并且没有不必要的语句:
pid_t pid = fork();
if (pid) {
execve(args4[0], args4, environ);
}
else {
pid = fork();
if (pid) {
execve(args3[0], args3, environ);
}
else {
execve(args2[0], args2, environ);
}
}
通过上面重写的代码,您可以更容易地看到您将获得如下三个进程:
参数2[0] | args3[0] | arg4[0]
关于c - 如何阻止 execve 退出原始程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151478/
该程序在嵌入式 Linux 上执行(内核版本:3.4.0)。在调用execve()之前,还调用了setgid()和setuid()切换到另一个用户(tstuser).用户存在,必要的条目在 /etc/
我使用 execv 和参数:$PATH 调用 echo,输出是 $PATH,而不是 $PATH 环境变量的实际值,这是我在 bash 中执行相同操作时得到的值。 为什么会发生这种情况?我传递了所有正确
我在使用此 execve 命令时遇到问题。我可以在我的程序中使用它来运行大多数其他命令,但如果我尝试执行 man ls 之类的操作,我会收到此错误。 man: can't execute pager:
在 C 程序中,main 函数通过查看 argc 知道在 argv 中传递了多少参数。我似乎不清楚 syscall execve 如何知道传递给它的参数数量。有人可以帮助解释 execve 如何处理参
我是新手,所以我可能对表达不准确。我需要用汇编程序在 C 中进行系统调用“execve”。我不使用任何库。不起作用的部分是 char *nul=(char *)0; char *argv[] = {
我的教授编写了一个程序,它使用 execve() 打印工作目录 (pwd),但我不明白其中的参数。 pid_t pid = fork(); if(pid int main(int argc, cha
我有一个名为“test”的程序,它执行另一个名为“hello”的程序。收到所需程序的名称后,我的程序似乎等待更多输入才能显示“hello”程序代码。示例代码片段 #include #include
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
我在使用 C 程序时遇到了一些神秘的情况,该程序在我的电脑上运行良好,但当我在我正在使用的服务器上编译它时却失败了。基本上execve执行失败。原来的程序不太大,所以我开始削减一些部分,以尝试了解问题
我正在尝试在 Linux 计算机上测试绝对路径以查找程序所在的位置,以便我可以使用特定参数运行它。问题是,当我找到它时,我不断向正确的路径添加更多字符串,并通过释放动态分配的内存来导致内存泄漏。堆栈转
我必须执行一个路径未知的命令。我可以到达环境路径,但如何使用它们?例如,如果有 5 个不同的路径,我应该在 execv 中尝试哪一个? /usr/lib/lightdm/lightdm:/usr/lo
我对此困惑了一段时间,现在我需要一些帮助。我正在尝试创建一个循环,该循环将 fork 子进程并通过 execve() 调用“echo hello”。 #include #include #inc
我的程序中有这个: execv (programname, (char **)argv); 我不确定命令是否真的被正确执行了。我怎样才能知道?这是在后台运行吗? 最佳答案 我强烈建议你买一本与你正在
我必须制作一个自定义 shell 作为学校项目,但我遇到了困难: int exec_shell(char **argv) // { if (execve(argv[0], (char **)a
我试图找到我当前的 PATH 以在我的 execv() 命令中使用它,但我仍然无法弄清楚如何找到任何提示的路径? 最佳答案 获取当前PATH variable从您的环境中(参见 environ(7)
了解 Linux 内核说 execve() 调用每个 linux_binfmt 对象的 load_binary(),并且 load_binary() Invokes the start_thread(
我们有一个从 inittab 启动的 stub ,execv 是我们的进程。 (ARM Linux 内核 2.6.25) 测试进程时,只有从 inittab 启动并执行时才会失败。如果在命令行上启动,
程序应该做什么的例子: ./executable ls -l 应该做同样的事 ls -l 所以基本上它是一个无用的 shell 命令解释器。 我创建了一个包含/bin/commandname 的字符串
我想编写一个程序来执行 Linux ls 命令。我真的只想输入 ls 而不是 /bin/ls,所以我想使用 execve (execvp 不是一个选项)。 我试过: char *env[] = { "
我有一项大学作业,我需要从管道中读取 bash 命令并执行该命令。我正在考虑使用 execv*,因为我可以通过以空格作为分隔符来创建缓冲区。问题是我不能使用 STL 库,而且我不知道应该如何创建这个缓
我是一名优秀的程序员,十分优秀!