我想把一个bash脚本(sc.sh
),这是在这个程序,并包含下面的行,在同一目录输出到C程序(cprog
) ;执行cprog
作品,但我不知道为什么在bash脚本没有启动:叉执行和管道与bash脚本
timeout 5 cat /dev/urandom
,这是主要的程序:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char* argv[])
{
int fd[2];
pid_t pid1, pid2;
char * input[] = {"/bin/bash", "sc.sh", argv[1], NULL};
char * output[] = {"./cprog", argv[1], NULL};
pipe(fd);
pid1 = fork();
if (pid1 == 0) {
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
execv(input[0], input);
return 1;
}
pid2 = fork();
if (pid2 == 0) {
dup2(fd[0], STDIN_FILENO);
close(fd[1]);
execv(output[0], output);
return 1;
}
close(fd[0]);
close(fd[1]);
waitpid(pid1, NULL, WNOHANG);
waitpid(pid2, NULL, WNOHANG);
return 0;
}
Szymon,你应该考虑使用缩进。 :) – ZbyszekKr
你应该检查函数调用的返回值。 –
你需要'bash -c'。 – EOF