栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Linux 3.0:使用管道标准输入/标准输出执行子进程

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux 3.0:使用管道标准输入/标准输出执行子进程

eerpini提供的代码无法正常工作。请注意,例如,之后将使用在父级中关闭的管端。看着

close(wpipefd[1]);

以及随后对该封闭描述符的写入。这只是换位,但它表明此代码从未使用过。以下是我测试过的版本。不幸的是,我更改了代码样式,因此这不接受作为eerpini代码的编辑。

唯一的结构更改是,我仅重定向子级中的I / O(请注意dup2调用仅在子级路径中。)这非常重要,因为否则父级的I /
O会混乱。感谢eerpini提出的最初答案,我在开发此答案时使用了它。

#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#define PIPE_READ 0#define PIPE_WRITE 1int createChild(const char* szCommand, char* const aArguments[], char* const aEnvironment[], const char* szMessage) {  int aStdinPipe[2];  int aStdoutPipe[2];  int nChild;  char nChar;  int nResult;  if (pipe(aStdinPipe) < 0) {    perror("allocating pipe for child input redirect");    return -1;  }  if (pipe(aStdoutPipe) < 0) {    close(aStdinPipe[PIPE_READ]);    close(aStdinPipe[PIPE_WRITE]);    perror("allocating pipe for child output redirect");    return -1;  }  nChild = fork();  if (0 == nChild) {    // child continues here    // redirect stdin    if (dup2(aStdinPipe[PIPE_READ], STDIN_FILENO) == -1) {      exit(errno);    }    // redirect stdout    if (dup2(aStdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1) {      exit(errno);    }    // redirect stderr    if (dup2(aStdoutPipe[PIPE_WRITE], STDERR_FILENO) == -1) {      exit(errno);    }    // all these are for use by parent only    close(aStdinPipe[PIPE_READ]);    close(aStdinPipe[PIPE_WRITE]);    close(aStdoutPipe[PIPE_READ]);    close(aStdoutPipe[PIPE_WRITE]);    // run child process image    // replace this with any exec* function find easier to use ("man exec")    nResult = execve(szCommand, aArguments, aEnvironment);    // if we get here at all, an error occurred, but we are in the child    // process, so just exit    exit(nResult);  } else if (nChild > 0) {    // parent continues here    // close unused file descriptors, these are for child only    close(aStdinPipe[PIPE_READ]);    close(aStdoutPipe[PIPE_WRITE]);    // Include error check here    if (NULL != szMessage) {      write(aStdinPipe[PIPE_WRITE], szMessage, strlen(szMessage));    }    // Just a char by char read here, you can change it accordingly    while (read(aStdoutPipe[PIPE_READ], &nChar, 1) == 1) {      write(STDOUT_FILENO, &nChar, 1);    }    // done with these in this example program, you would normally keep these    // open of course as long as you want to talk to the child    close(aStdinPipe[PIPE_WRITE]);    close(aStdoutPipe[PIPE_READ]);  } else {    // failed to create child    close(aStdinPipe[PIPE_READ]);    close(aStdinPipe[PIPE_WRITE]);    close(aStdoutPipe[PIPE_READ]);    close(aStdoutPipe[PIPE_WRITE]);  }  return nChild;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403045.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号