- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何使第二个 execlp("sort","sort,",(char *)0)
输出到显示器?
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "errno.h"
int main()
{
int parprocID = getppid();//parent process id
int c1Toc2[2];
int c2Toc1[2];
pipe(c1Toc2);//pipe 1, to child 2
pipe(c2Toc1);//pipe 2, to child 1
int cpid = 0;//child 1 pid
int cpid2 = 0;//child 2 pid
int child1,child2;//Child variable
printf("Parent Process id is: %d.\n\n", parprocID);//parent pid introduction.
child1 = fork();//creation of child 1
if(child1 == 0)
{
cpid = getpid();
printf("Child 1 created by process: %d has an id of %d.\n\n", parprocID, getpid());
dup2(c1Toc2[1], STDOUT_FILENO);//redirect from stdout to pipe
execlp("ls", "ls","-al","/bin", (char *)0);
exit(0);
}
else
{
child2=fork(); //creation of child 2
if(child2 == 0)
{
cpid2 = getpid();
printf("Child 2 created by process: %d has an id of %d.\n", parprocID, getpid());
dup2(c1Toc2[0], STDIN_FILENO);
execlp("sort", "sort", (char *)0);
exit(0);
close(c1Toc2[0]);
close(c1Toc2[1]);
}
}
sleep(3);
printf("PARENT: PROCESS waiting on children to complete\n");
wait(NULL);
wait(NULL);
printf("Final print statement before exit\n");
exit(0);
}
child 1 有 execlps
系统调用,所以 child 1 执行 ls 程序。dup2
将输出重定向到管道。我认为 child 2 execlp
对 ls 进行排序,但代码只是挂起,我不确定我是否做对了,我希望 child 2 显示 child 1 重定向的内容。接下来会发生什么或我哪里出错了?
最佳答案
父进程和子进程 2(sort)也打开了管道的写入端,因此 sort 正在等待其标准输入的更多输入:a close(c1Toc2[1]);
之前 sleep(3);
和之前 execlp("sort", "sort", (char *)0);
必须添加。
关于c - 如何使用execlp()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918270/
我正在学习进程间通信......这是让我烦恼的代码 #include #include #include int main(void) { int pfds[2]; pipe(pfd
#include #include #include int main(){ pid_t pid; pid = fork(); if(pid<0){ fprintf(stderr, "fo
我正在编写一个程序,其界面如下:myprog file1 file2 c 这个程序创建了两个子进程,P2 用 execlp 打开 file2,在这个文件上创建一个 grep -c 以创建 c 并将结果
我正在制作一个程序,其中我必须 fork 4 个程序,然后这些程序将执行。我的问题是如何等待执行的 child 。当子进程执行时,Wait() 不起作用 最佳答案 My problem is how
我正在尝试执行类似于以下内容的文件: ./foo bar baz band 哪里 executable = "foo" path_executable =" bar baz band" 我正在使用 s
例如,我正在尝试创建一个简单的程序,它将从参数运行 shell 命令 ./run date +"%r" 07:56:05 PM 但我不知道怎么做。我尝试这个,但它不起作用。我很困惑,完全不明白 ex
我在 execlp 方面遇到问题。当我不知道如何正确地将命令从指针数组重定向到 execlp 时。例如我想使用 ls -l | sort -n 我的程序只需要“ls”和“sort” int
我正在尝试编译 2 个可执行文件。其中一台是采样器,另一台是收集器。必须从收集器的子级调用采样器。采样器一将一些数据写入共享内存,收集器应从共享内存中读取数据。我正在使用 execlp 来调用 Sam
这是exec()系统调用的非常简单的示例。在这里,我尝试调用 execlp() 两次。但是,我没有得到异常(exception)的输出。它仅显示当前目录的第一次调用的输出。 #include #in
我正在做一个涉及管道和执行的简单作业,这是代码。 #include #include int main(void){ int out[2]; pipe(out); char
这个问题已经有答案了: How to get the return value of a program ran via calling a member of the exec family of
我正在使用 execlp 运行 wc 命令,使用文件作为额外参数来读取字数。这个 unix 命令: wc -l HelloWorld.class 输出:7 HelloWorld.class 但是在我使
我正在尝试使用 execlp 返回多行输出,但我不确定这样做的确切方法。我试过了 #include #include #include using namespace std; int main
我无法通过 execlp 执行二进制文件. chdir("/home/foo/bar/baz/MB/"); execlp("bash", "bash", "./foobarbaz 1", NULL);
嗨,有人能解释一下为什么我应该在关闭管道后使用 execlp 吗? 这是一个例子: if( cid == 0) {//Only child cid can run this code char
我正在用 C 语言创建一个应用程序,我必须使用命令 execlp 来执行 firefox,但每次执行它时我都会“丢失”我当前的终端,但是在 execlp 我仍然需要使用以前的终端,所以我的问题是:有没
exec() 系统调用的这个非常简单的例子。在这里,我尝试调用 execlp() 两次。但是,我没有得到异常输出。它仅显示对当前目录的第一次调用的输出。 #include #include int
我最近遇到了这样的问题: 如果你在一个函数中使用execlp,并且下面还有一些代码,那么在什么情况下会执行这些代码? 例如: execlp("ps","ps","-u","username",(cha
我正在尝试使用 fork 和 execlp 运行一个非常简单的程序,但它并没有像我预期的那样工作。目前,我的工作目录中有一个名为“1”的文件。所以命令 rm 1* 应该删除它。然而,当通过 execl
我想从 C 文件运行 execlp() 并将结果写入某个输出文件。我用这条线: buff = "./cgi-bin/smth"; execlp(buff, buff, "> /cgi-bin/tmp"
我是一名优秀的程序员,十分优秀!