Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将标准输出重定向到管道写入端_C_Exec_Dup - Fatal编程技术网

C 将标准输出重定向到管道写入端

C 将标准输出重定向到管道写入端,c,exec,dup,C,Exec,Dup,我正在写一个小程序,下面是它应该做的 在主进程中,我必须创建一个新的程序,该程序应该执行另一个只执行printf(“文本”)的程序。我想重定向stdout上的管道写入端,主进程应该从其管道读取并在stdout上打印它。我编写了代码,但当父进程尝试从管道中读取时,我一次又一次地遇到分段错误 #include <sys/types.h> #include <stdio.h> #include <string.h> #include <unistd.h>

我正在写一个小程序,下面是它应该做的

在主进程中,我必须创建一个新的程序,该程序应该执行另一个只执行printf(“文本”)的程序。我想重定向stdout上的管道写入端,主进程应该从其管道读取并在stdout上打印它。我编写了代码,但当父进程尝试从管道中读取时,我一次又一次地遇到分段错误

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

void write_to(FILE *f){
  char buf[50];
  fprintf(f,"KOMA");
}

int main(){
  int cpPipe[2];
  int child1_fd;
  int child2_fd;

  if(pipe(cpPipe) == -1){

    fprintf(stderr,"ERROR PIPE creation");
    exit(1);

  }else{printf("pipe couldn't be created\n");}

  child1_fd = fork();

  if(child1_fd < 0){
    fprintf(stderr, " CHILD creation error");
    exit(1);
  }

  if(child1_fd == 0){
    printf("*CHILD*\n");
    char program[] = "./Damn";
    int dupK;
    printf("stdout %d \n", STDOUT_FILENO);
    printf("stdin %d \n", STDIN_FILENO);
    printf("pipe1 %d \n", cpPipe[1]);
    printf("pipe0 %d \n", cpPipe[0]);

    // closing pipe write
    close(cpPipe[0]);
    close(1);
    dup(cpPipe[1]);

    printf("and");

    close(cpPipe[1]);
    exit(0);
  }else{
    printf("*Parent*\n");
    char *p;
    char *buf;
    FILE *pipe_read;

    close(cpPipe[1]);
    pipe_read = fdopen(cpPipe[0],"r");

    while((buf = fgets(p,30,pipe_read)) != NULL){
      printf("buf %s \n", buf);
    }

    wait();
    printf("Child is done\n");
    fclose(pipe_read);

    exit(0);
  }
}
#包括
#包括
#包括
#包括
#包括
无效写入(文件*f){
char-buf[50];
fprintf(f,“KOMA”);
}
int main(){
int cpPipe[2];
国际儿童基金会;
国际儿童基金会;
如果(管道(cpPipe)=-1){
fprintf(stderr,“错误管道创建”);
出口(1);
}else{printf(“无法创建管道”);}
child1_fd=fork();
if(child1_fd<0){
fprintf(stderr,“子创建错误”);
出口(1);
}
如果(child1_fd==0){
printf(“*子项*\n”);
字符程序[]=“/该死”;
int-dupK;
printf(“标准输出%d\n”,标准输出文件号);
printf(“标准输入%d\n”,标准输入文件号);
printf(“管道1%d\n”,cpPipe[1]);
printf(“管道0%d\n”,cpPipe[0]);
//关闭管道写入
关闭(cpPipe[0]);
关闭(1);
dup(cpPipe[1]);
printf(“和”);
关闭(cpPipe[1]);
出口(0);
}否则{
printf(“*父项*\n”);
char*p;
char*buf;
文件*管道读取;
关闭(cpPipe[1]);
管道读取=fdopen(管道[0],“r”);
而((buf=fgets(p,30,管道读取))!=NULL){
printf(“基本单位%s\n”,基本单位);
}
等待();
printf(“子项已完成\n”);
fclose(管道读数);
出口(0);
}
}

将stdout重定向到管道写入端时,是否必须关闭管道写入端?

Uhm,。。。分段错误的原因如下:

buf = fgets(p,30,pipe_read);
p是一个指针,它指向的根本不是重要的地方。它的内容是执行时堆栈中的任何内容,您永远不会初始化它。你需要它指向你可以使用的内存块!为其分配
malloc()
调用的返回,或将其声明为
charp[LEN]

编辑:您还将重新打开已打开的文件描述符。查看
fgets
pipe
上的文档,我认为您对它们的工作原理感到困惑

这就是说,函数的流程有点混乱。试着澄清它!记住,代码是用来表达意图和功能的想法。试着用铅笔和纸来组织你的程序,然后把它写成实际的代码:)


干杯

嗯,。。。分段错误的原因如下:

buf = fgets(p,30,pipe_read);
p是一个指针,它指向的根本不是重要的地方。它的内容是执行时堆栈中的任何内容,您永远不会初始化它。你需要它指向你可以使用的内存块!为其分配
malloc()
调用的返回,或将其声明为
charp[LEN]

编辑:您还将重新打开已打开的文件描述符。查看
fgets
pipe
上的文档,我认为您对它们的工作原理感到困惑

这就是说,函数的流程有点混乱。试着澄清它!记住,代码是用来表达意图和功能的想法。试着用铅笔和纸来组织你的程序,然后把它写成实际的代码:)

干杯

将stdout重定向到管道写入端时,是否必须关闭管道写入端

一般来说,是的,因为当有一个进程的管道写入端打开时,读取管道的进程将不会得到EOF,并将挂起。当然,关闭不打算使用的文件描述符也很方便

您的代码还在成功路径中显示“无法创建管道”

将stdout重定向到管道写入端时,是否必须关闭管道写入端

一般来说,是的,因为当有一个进程的管道写入端打开时,读取管道的进程将不会得到EOF,并将挂起。当然,关闭不打算使用的文件描述符也很方便


您的代码还在成功路径中显示“无法创建管道”。

是的,这是一个错误,我很匆忙。谢谢。@Robin:当我修复代码的副本时——删除了未使用的函数和变量——当我使用本地缓冲区修复分段错误,并正确诊断为“p”时,它似乎工作正常。显然,您仍然需要添加'exec()`来使用另一个程序,但这是相对简单的。是的,这是一个错误,我很匆忙。谢谢。@Robin:当我修复代码的副本时——删除了未使用的函数和变量——当我使用本地缓冲区修复分段错误,并正确诊断为“p”时,它似乎工作正常。显然,您仍然需要添加'exec()`来使用另一个程序,但这是相对简单的。谢谢。我发现了fgets和p的问题。关于描述符,我不知道你的意思。我当时很忙,这是fork、exec和pipe 24小时学习的成果。:)非常感谢。我发现了fgets和p的问题。关于描述符,我不知道你的意思。我当时很忙,这是fork、exec和pipe 24小时学习的成果。:)