- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
对于一项作业,我应该实现 linux 终端。我的终端应该支持管道参数。喜欢的用户可以输入:ls | grep ".cpp"
到目前为止我所做的是:
pid = fork();
if(pid==0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp("/bin/ls","ls");
}
wait(NULL);
pid=fork();
if(pid==0)
{
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp("/bin/grep","grep",".cpp");
}
我的第一个 child 工作得很好,将 ls 的输出写入之前声明的管道,但是,我的第二个 child 运行 grep,但显然无法从管道获取输入。为什么会这样?当我运行它时,我的输出是 /根/操作系统 $ ls |搜索
就这样卡住了
最佳答案
不要忽略编译器警告:
$ gcc lol.c
lol.c: In function ‘main’:
lol.c:14:5: warning: not enough variable arguments to fit a sentinel [-Wformat=]
execlp("/bin/ls","ls");
^
lol.c:23:5: warning: missing sentinel in function call [-Wformat=]
execlp("/bin/grep","grep",".cpp");
^
这是man execlp
:
The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
始终使用 -Werror
进行编译,以便在出现警告时编译失败,最好还使用 -Wall
来获取所有潜在问题的警告。
这是您的程序,其中添加了 main 方法和哨兵:
#include <unistd.h>
#include <sys/wait.h>
int main() {
int fd[2];
int pid;
pipe(fd);
pid = fork();
if(pid==0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp("/bin/ls","ls", NULL);
}
wait(NULL);
pid=fork();
if(pid==0)
{
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp("/bin/grep","grep",".cpp", NULL);
}
return 0;
}
下面是它现在的运行方式:
$ gcc -Wall -Werror lol.c -o lol
$ ./lol
foo.cpp
关于c++ - 无法使用 execlp 从管道读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35586947/
我正在学习进程间通信......这是让我烦恼的代码 #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"
我是一名优秀的程序员,十分优秀!