|
我要写一个程序,产生2个进程,进程2接管进程1的IO
下面是我的程序,一直不成功,不知道什么原因,请高手解答
以下这个编译成cal
#include <stdio.h>
int main(){
int a,b;
for( ;; ){
scanf("%d%d", &a, &b);
printf("%d\n", a+b);
}
return 0;
}
以下这个程序做IO接管的工作,不知道问题出在哪了
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <sys/reg.h>
#include <unistd.h>
int main(){
pid_t pid;
int pipe_fdr[2];
int pipe_fdw[2];
if( pipe(pipe_fdr) == -1 ){
perror("pipe failed!\n");
exit(0);
}
if( pipe(pipe_fdw) == -1 ){
perror("fork failed!\n");
exit(0);
}
pid = fork();
if( pid == 0 ){
if( dup2(pipe_fdr[0], 0) == -1 ){
perror("dup failed!\n");
exit(0);
}
if( dup2(pipe_fdw[1], 1) == -1 ){
perror("dup failed!\n");
exit(0);
}
if(execl("./cal", "cal", NULL) <0 ){
printf("Error!!\n");
}
}else{
while(1){
wait(NULL);
FILE* in = fdopen(pipe_fdr[1], "w");
FILE* out = fdopen(pipe_fdw[0], "r");
char line[4096];
fgets(line, 4096, stdin);
fprintf(in, line);
fgets(line, 4096, out);
printf(line);
}
}
return 0;
} |
|