热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Ubuntu在C中进行shell处理的特定管道命令-SpecificpipecommandinUbuntu'sshellhandlinginC

ImtryingtosimulateapipebehavioronUbuntusTerminal,forexamplethecommand:echohello|

I'm trying to simulate a pipe behavior on Ubuntu's Terminal, for example the command:
"echo hello | wc".
Please assume I got the tokens from stdin, handled everything correctly and now These are the commands I "received" from the user who typed them in the shell for me to handle.
I'm trying to create two processes. Using a pipe, in the first process, I point the file descriptor of the writing edge of the pipe to stdout. The second process should read into stdin with the reading edge of the pipe what execvp(..) returned.?
Here is the code I wrote:

我正在尝试模拟Ubuntu终端上的管道行为,例如命令:“echo hello | wc”。请假设我从stdin获得了令牌,正确处理了所有内容现在这些是我从shell中输入的“我收到”的命令供我处理。我正在尝试创建两个进程。使用管道,在第一个过程中,我将管道写边的文件描述符指向stdout。第二个进程应该读入带有execvp(..)返回的管道读边的stdin。这是我写的代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
  char* fcmd[] = {"echo", "hello", NULL};
  char* scmd[] = {"wc", NULL};
  pid_t pid;
  int pipe_des[2];
  int i;
  pipe(pipe_des);
  for(i = 0; i <2; i++)
  {
      pid = fork(); 
      if (pid ==0)
      {
          switch (i)
          {
              case 0: // FIRST CHILD
              {
                  dup2(pipe_des[1], STDOUT_FILENO);
                  close(pipe_des[0]);
                  execvp(fcmd[0], fcmd);
                  exit(0);
              }
              case 1: //SECOND CHILD
              {
                  dup2(pipe_des[0], STDIN_FILENO);
                  close(pipe_des[1]);
                  execvp(scmd[0], scmd);
                  exit(0);
              }
            }
        }
        else if (pid <0)
            exit(EXIT_FAILURE);
return EXIT_SUCCESS;
}

I get: " amirla@ubuntu:~/Desktop/os/class/ex4$ 1 1 6 "
Like it should, but why he's printing the bash cwd first? The pipe seems to work because I get what I should, according to the length of the word I'm sending with the echo command(in the main()). After that the cursor just waits on the line below for another command without showing me the bash pwd. (maybe stdin is waiting?)
I've looked in many posts on here as well as on other websites and I still can't seem to find a solution to my problem. Any help would be appreciated. Thanks in advance. Note: Please Ignore checking for errors, I've delete them to make the code shorter so assume they exist.

我得到:“amirla @ ubuntu:〜/ Desktop / os / class / ex4 $ 1 1 6”就像它应该的那样,但为什么他会先打印bash cwd?根据我使用echo命令发送的单词的长度(在main()中),管道似乎可以工作,因为我得到了我应该得到的东西。之后,光标只是在下面的行上等待另一个命令而没有向我显示bash pwd。 (也许stdin正在等待?)我在这里以及其他网站上查了很多帖子,我似乎仍无法找到问题的解决方案。任何帮助,将不胜感激。提前致谢。注意:请忽略检查错误,我删除它们以使代码更短,因此假设它们存在。

1 个解决方案

#1


Why do I get a prompt before the output?

为什么我在输出之前得到提示?

Your main process doesn't wait for the children to finish. What you see is:

您的主要过程不会等待孩子们完成。你看到的是:

  1. Main starts
  2. Main creates children
  3. 主要创造孩子

  4. Main exits
  5. BASH prints prompt
  6. BASH打印提示

  7. Children start their work
  8. 孩子们开始工作

To prevent this, you need to wait for the children. See How to wait until all child processes called by fork() complete?

为了防止这种情况,您需要等待孩子们。请参阅如何等待fork()调用的所有子进程完成?

In your case, it's enough to add

在你的情况下,它足以添加

 waitpid(-1, NULL, 0);

after the loop.

循环之后。


推荐阅读
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • UMTS基础知识汇总
    协议框架23G接口UMTS实体EntityNameDescriptionAuCAuthenticationCenterCBCCellBroadcastCenterC-RNCCon ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • Python脚本编写创建输出数据库并添加模型和场数据的方法
    本文介绍了使用Python脚本编写创建输出数据库并添加模型数据和场数据的方法。首先导入相应模块,然后创建输出数据库并添加材料属性、截面、部件实例、分析步和帧、节点和单元等对象。接着向输出数据库中添加场数据和历程数据,本例中只添加了节点位移。最后保存数据库文件并关闭文件。文章还提供了部分代码和Abaqus操作步骤。另外,作者还建立了关于Abaqus的学习交流群,欢迎加入并提问。 ... [详细]
  • 很多时候在注册一些比较重要的帐号,或者使用一些比较重要的接口的时候,需要使用到随机字符串,为了方便,我们设计这个脚本需要注意 ... [详细]
  • 题目描述Takuru是一名情报强者,所以他想利用他强大的情报搜集能力来当中间商赚差价。Takuru的计划是让Hinae帮他去市场上买一个商品,然后再以另一个价格卖掉它。Takur ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • 31.项目部署
    目录1一些概念1.1项目部署1.2WSGI1.3uWSGI1.4Nginx2安装环境与迁移项目2.1项目内容2.2项目配置2.2.1DEBUG2.2.2STAT ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
author-avatar
zjymeimei706
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有