/*7.“grep –o”不会输出包含某字符串的行,而只会把匹配的字符串输出(有几个匹配就输出几次,且每行输出一个匹配字符串),
因而可以用 “grep –o xxx file | wc -l”来统计文件file中“xxx”字符串出现了多少次。
请用多进程pipe程序实现该功能(此前先用which命令查一下grep命令和wc命令在哪个目录),
要求:创建一个文本文件为zhangsan,自己在该文件中输入若干数目的字符串“zhangsan”,将其作为操作的原材料。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<unistd.h>
#define MSGSIZE 24
int main(int argc,char *argv[])
{
int status;
int pid[2],pipe_fd[2];
char *prog1_argv[]={"/bin/grep","-o",argv[1],argv[2],NULL};
char *prog2_argv[]={"/usr/bin/wc","-l",NULL};
//create the pipe
if(pipe(pipe_fd)<0)
{
perror("pipe failed\n");
exit(errno);
}
//兄弟之间管道
if((pid[0]=fork())<0)
{
perror("fork failed\n");
exit(errno);
}
//pid[0]==0,即进入子进程
操作系统 管道及其实现
最新推荐文章于 2024-04-23 16:04:15 发布
本文介绍了操作系统中管道的概念,并提供了多个关于UNIX和Linux管道应用的教程链接,包括Shell实现、命令解析、简易Shell实现以及不同类型的管道通信实验。
摘要由CSDN通过智能技术生成