通过管道发送C命令行参数

通过管道发送C命令行参数,c,pipe,C,Pipe,我试图使用管道将我的命令行参数从子进程发送到父进程,但无法找出我做错了什么。我的代码如下。感谢您的帮助。提前谢谢 int main(int argc, char argv[]) pid_t child; int fd[2]; pipe(fd); if((child = fork() == 0) { int len = strlen(argv[1]); close(fd[0]; write(fd[1], argv[1], len)

我试图使用管道将我的命令行参数从子进程发送到父进程,但无法找出我做错了什么。我的代码如下。感谢您的帮助。提前谢谢

int main(int argc, char argv[])
   pid_t child;
   int fd[2];

   pipe(fd);
   if((child = fork() == 0)
   {
      int len = strlen(argv[1]);
      close(fd[0];
      write(fd[1], argv[1], len);
      exit(0);
   }
   else //Assuming process won't fail for now
   {
      char src[10]; //Just using 10 for now, no arguments have more than 10 characters
      read(fd[0], src, (strlen(src)));
      fprintf(stderr, "%s\n", src);
      close(fd[0]);
   }
}

您可能需要先关闭
else
块内的fd[1]

检查这个例子

#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
    int
    main(int argc, char *argv[])
    {
            int pipefd[2];
            pid_t cpid;
            char buf;
            if (argc != 2) {
            fprintf(stderr, "Usage: %s <string>\n", argv[0]);
            exit(EXIT_FAILURE);
            }
            if (pipe(pipefd) == -1) {
                    perror("pipe");
                    exit(EXIT_FAILURE);
            }
            cpid = fork();
            if (cpid == -1) {
                    perror("fork");
                    exit(EXIT_FAILURE);
            }
            if (cpid == 0) {    /* Child reads from pipe */
                    close(pipefd[1]);          /* Close unused write end */
                    while (read(pipefd[0], &buf, 1) > 0)
                            write(STDOUT_FILENO, &buf, 1);
                    write(STDOUT_FILENO, "\n", 1);
                    close(pipefd[0]);
                    _exit(EXIT_SUCCESS);
            } else {            /* Parent writes argv[1] to pipe */
                    close(pipefd[0]);          /* Close unused read end */
                    write(pipefd[1], argv[1], strlen(argv[1]));
                    close(pipefd[1]);          /* Reader will see EOF */
                    wait(NULL);                /* Wait for child */
                    exit(EXIT_SUCCESS);
            }
    }
#包括
#包括
#包括
#包括
#包括
int
main(int argc,char*argv[])
{
int-pipefd[2];
pid_t cpid;
焦炉;
如果(argc!=2){
fprintf(stderr,“用法:%s\n”,argv[0]);
退出(退出失败);
}
如果(管道(管道FD)=-1){
佩罗(“管道”);
退出(退出失败);
}
cpid=fork();
如果(cpid==-1){
佩罗尔(“福克”);
退出(退出失败);
}
如果(cpid==0){/*子级从管道读取*/
关闭(pipefd[1]);/*关闭未使用的写入端*/
while(读取(pipefd[0],&buf,1)>0)
写入(STDOUT_文件号和buf,1);
写入(标准输出文件号,“\n”,1);
关闭(pipefd[0]);
_退出(退出成功);
}else{/*父对象将argv[1]写入管道*/
关闭(pipefd[0]);/*关闭未使用的读取端*/
写入(pipefd[1],argv[1],strlen(argv[1]);
关闭(pipefd[1]);/*读卡器将看到EOF*/
等待(NULL);/*等待子项*/
退出(退出成功);
}
}

您假设
fork()
不会失败

但是关于
pipe()

假设两个都已成功完成,则需要重新查询正确关闭FD

您的if-else块应该是这样的

if((child = fork() == 0)
   {
      int len = strlen(argv[1]);
      close(fd[0]);//I assume this was your typo. otherwise it would not even get compiled
      write(fd[1], argv[1], len);
      close(fd[1]);
      exit(0);
   }
else //Assuming process won't fail for now
   {
      close(fd[1]);
      char src[10]; //Just using 10 for now, no arguments have more than 10 characters
      read(fd[0], src, (strlen(src)));
      fprintf(stderr, "%s\n", src);
      close(fd[0]);
   }

你犯了很多小错误,但据我所知,信不信由你,这可能是你真正的问题

read(fd[0], src, (strlen(src)));
我猜第一个字符是空的,您正在成功读取0字节

改为

  read(fd[0], src, (sizeof(src)));

在更大的项目中,确保以循环方式进行读写。您不能保证读写您指定的内容。

不,我可能需要在其中,但它没有解决它。在写入fd[1]后,您也需要关闭它,我想我不确定它会有多大的不同,但我不允许使用等待系统调用。我提供的代码部分只是我正在处理的一个更大项目的一部分,但我无法正确地获取参数。我不使用wait系统调用会有所不同吗?我认为您需要某种同步机制来进行一般通信。好的,谢谢您的帮助,我会继续尝试。我使用的是Minix,应该只使用write、close、fork和管道调用就可以完成。它可以工作,所以它可以传递整数,但当我切换到字符串时,它就崩溃了。“没有参数超过10个字符”意味着一个增广可以是“1234567890”,它需要
char src[10+1]
src[10]='\0'
fprintf(stderr,%s\n,src)之前