标题对于我想要做的事情是不言自明的,下面是我当前的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
pid_t pid ;
int fd[2], fd2[2];
pipe(fd);
pipe(fd2);
pid = fork();
if(pid == 0) {
close(1);
dup(fd[1]);
close(fd[0]);
close(fd[1]);
char *exp[] = {"cat", "filename.txt", NULL};
execvp("cat", exp);
exit(EXIT_FAILURE);
}
else if(pid > 0) {
close(1);
dup(fd2[1]);
close(fd2[0]);
close(fd2[1]);
char *exp[] = {"grep", "-w", "stringname", NULL};
execvp(exp[0], exp);
pid_t pid2=fork();
close(0);
dup(fd[0]);
close (fd[1]);
close(fd[0]);
char *exp2[] = {"grep", "-c", "stringname", NULL};
execvp(exp2[0], exp2);
exit(EXIT_FAILURE);
}
else {
printf("Error in forking");
exit(EXIT_FAILURE);
}
close(fd[0]);
close(fd[1]);
close(fd2[0]);
close(fd2[1]);
return 0;
}目前,该程序正在编译,但没有执行(它在执行过程中卡在某个地方,我没有任何输出),对于我做错了什么以及如何修复它有任何帮助吗?
发布于 2020-04-09 14:28:59
代码中存在一些问题:
execvp(exp[0], exp);将在第二个fork()之前执行。这是一个逻辑错误。我用这些更改重写了您的代码,使用dup2使代码变得更干净:
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int pipes[4];
// Create two pipes
pipe(pipes);
pipe(pipes + 2);
// Execute cat
if (fork() == 0) {
// Replace output with write end of first pipe
dup2(pipes[1], 1);
// Close all ends of pipes (we already copied it with `dup2`)
close(pipes[0]);
close(pipes[1]);
close(pipes[2]);
close(pipes[3]);
char *exp[] = {"cat", "filename.txt", NULL};
execvp(exp[0], exp);
perror("cat failed");
exit(EXIT_FAILURE);
} else {
// Execute first grep
if (fork() == 0) {
// Replace input with read end of 1st pipe
dup2(pipes[0], 0);
// Replace output with write end of 2nd pipe
dup2(pipes[3], 1);
// Close all ends of pipes
close(pipes[0]);
close(pipes[1]);
close(pipes[2]);
close(pipes[3]);
char *exp[] = {"grep", "-w", "stringname", NULL};
execvp(exp[0], exp);
perror("first grep failed");
exit(EXIT_FAILURE);
} else {
// Execute second grep
if (fork() == 0) {
// Replace input with read end of 2nd pipe
dup2(pipes[2], 0);
// Close all ends of pipes
close(pipes[0]);
close(pipes[1]);
close(pipes[2]);
close(pipes[3]);
char *exp[] = {"grep", "-c", "stringname", NULL};
execvp(exp[0], exp);
perror("second grep failed");
exit(EXIT_FAILURE);
}
}
}
// Close all ends of pipes
close(pipes[0]);
close(pipes[1]);
close(pipes[2]);
close(pipes[3]);
for (int i = 0; i < 3; i++) {
wait(NULL);
}
return 0;
}https://stackoverflow.com/questions/61122501
复制相似问题