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

如何在c中创建一个linux管道示例-Howtocreatealinuxpipelineexampleinc

Iamtryingtolearnhowtousethepipe()commandinC,andtryingtocreateatestprogramtodupl

I am trying to learn how to use the pipe() command in C, and trying to create a test program to duplicate the functionality of ls | grep ".c", if I were to enter this into a linux terminal. If I enter this into the terminal, I only get test.c as a result.

我试图学习如何在C中使用pipe()命令,并尝试创建一个测试程序来复制ls的功能。 grep“.c”,如果我要将它输入linux终端。如果我将它输入终端,我只得到test.c。

My code is as follows:

我的代码如下:

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "fcntl.h"

int main(int argc, char** argv)
{
 int pipefd[2];
 int childpid,childpid2;
 char* cmd[3]={"ls",NULL,NULL};
 char* cmd2[3]={"grep",".c",NULL};
 pipe(pipefd);
 if(childpid=fork()){
   //parent
 }else{  
   //child
   //write
   close(pipefd[0]);
   dup2(pipefd[1],STDOUT_FILENO);
   execvp("ls", cmd);
 }
 if(childpid2=fork()){
 }
 else{
   close(pipefd[1]);
   dup2(pipefd[0],STDIN_FILENO);
   execvp("grep",cmd2);
 }
 close(pipefd[0]);
 close(pipefd[1]);
 return 0;
}

This code returns me the following results ($ is the terminal prompt):

此代码返回以下结果($是终端提示符):

$a.out
$test.c
(blank line)

The program doesn't complete, but hangs until I quit it. What problems do I have? How can I mimic the terminal? I'm new to C, and using a premade template of a program, so forgive me if there are glaring mistakes.

该程序没有完成,但挂起,直到我退出它。我有什么问题?我怎么能模仿终端?我是C的新手,并使用程序的预制模板,如果有明显的错误,请原谅我。

3 个解决方案

#1


3  

Try this:

#include 
#include 
#include 
#include 

int main(int argc, char** argv)
{
 int pipefd[2];
 int childpid,childpid2;
 char* cmd[3]={"ls",NULL,NULL};
 char* cmd2[3]={"grep",".c",NULL};
 pipe(pipefd);
 if(childpid=fork()){
   //parent
   close(pipefd[1]);
   dup2(pipefd[0],STDIN_FILENO);
   execvp("grep",cmd2);
 }else{  
   //child
   //write
   close(pipefd[0]);
   dup2(pipefd[1],STDOUT_FILENO);
   execvp("ls", cmd);
 }
 return 0;
}

#2


2  

Actually the program exits right away — in fact, the parent process exits before the children run, which is why there's a shell prompt before "test.c".

实际上程序会立即退出 - 事实上,父进程在子进程运行之前退出,这就是为什么在“test.c”之前有一个shell提示符。

You can improve things a bit by adding this in your parent:

您可以通过在父级中添加它来改进一些事情:

wait(childpid);
wait(childpid2);

which will make the parent exit after both children.

这将使父母在两个孩子之后退出。

#3


1  

Your program quits immediately, while your processes run in the background. They overwrite the prompt, and makes you think the program is still running even though the shell is waiting for your input (press enter or type a command blindly and see.

当您的进程在后台运行时,您的程序会立即退出。它们会覆盖提示,并让您认为程序仍在运行,即使shell正在等待您的输入(按回车键或盲目输入命令并查看。

You only see test.c because that's the only matching file in your directory (also note that you're checking for "filenames containing c anywhere except the first character", not "ending in .c" which would be grep '\.c$').

你只看到test.c,因为那是你目录中唯一匹配的文件(还要注意你要检查“除第一个字符以外的任何地方都包含c的文件名”,而不是“以.c结尾”,这将是grep'\ .c。 $“)。

The simple fix is to add:

简单的解决方法是添加:

wait(NULL); wait(NULL);

right before your return 0.

就在你回来之前0。


推荐阅读
author-avatar
婧氏橙奂
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有