自测之Lesson10:管道
题目:建立双向管道,实现:父进程向子进程传送一个字符串,子进程对该字符串进行处理(小写字母转为大写字母)后再传回父进程。
实现代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> void TestPipeDouble()
{
pid_t pid;
int fd_A[2], fd_B[2];
if (pipe(fd_A) || pipe(fd_B)) { // 创建管道A和管道B
perror("pipe fail");
return;
}
char szBuf[1024];
pid = fork();
if (pid == 0) {
close(fd_A[1]);
close(fd_B[0]);
printf("Child pid:%d\n", getpid());
while(1) {
memset(szBuf, 0, 1024);
read(fd_A[0], szBuf, 1024);
int i;
for (i = 0; i < strlen(szBuf); ++i) {
if (szBuf[i] <= 'z' && szBuf[i] >= 'a') {
szBuf[i] += ('A' - 'a');
}
}
write(fd_B[1], szBuf, strlen(szBuf));
}
close(fd_A[0]);
close(fd_B[1]);
}
else {
close(fd_A[0]);
close(fd_B[1]);
printf("Father pid:%d\n", getpid());
while(1) {
usleep(1000);
printf("Send:");
scanf("%s", szBuf);
write(fd_A[1], szBuf, strlen(szBuf));
memset(szBuf, 0, 1024);
read(fd_B[0], szBuf, 1024);
printf("Recv:%s\n", szBuf);
}
close(fd_A[1]);
close(fd_B[0]);
} } int main()
{
TestPipeDouble();
return 0;
}
题目:基于管道,并借助于dup2、exec函数族,实现命令“ps -ef | grep pipe”。
实现代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> // 相当于“ps -ef | grep pipe”
void TestPipeRedir() // 重定向
{
int fd[2];
pid_t pid;
if(pipe(fd) == -1) {
perror("pipe failed");
return;
}
pid = fork();
if (pid == 0) {
dup2(fd[1], STDOUT_FILENO); // 将本应输出至stdout的“ls -l”,转而输出至管道写端
close(fd[0]);
close(fd[1]);
execlp("ps", "ps", "-ef", NULL); // 参数列表以NULL结尾
}
else {
dup2(fd[0], STDIN_FILENO); // grep本应由stdin读入“ls -l”, 转而从管道读端读入
close(fd[0]);
close(fd[1]);
execlp("grep", "grep", "pipe", NULL);
}
} int main()
{
TestPipeRedir();
return 0;
}
题目:使用popen函数实现命令“ps -ef | grep pipe”。
实现代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h> /* 使用sort对数组arr进行排序 */
void TestPipePopen_easy()
{
int arr[] = {2, 3, 7, 3, 5, 10, 9};
FILE *pFile = popen("sort -n", "w"); // 当前进程将arr传入子进程sort
int i;
for (i = 0; i != sizeof(arr) / sizeof(int); i++) {
fprintf(pFile, "%d\n", arr[i]); // 父进程通过pFile来发送数据至子进程
}
pclose(pFile); // 一旦pclose,即开始执行sort命令
} /* 实现命令ps -ef | grep pipe */
void TestPipePopen_complex()
{
FILE *pRead = popen("ps -ef", "r"); // 子进程ps将数据写入当前进程
FILE *pWrite = popen("grep pipe", "w"); // 当前进程将数据写入子进程grep
char szBuf[1024];
while(fgets(szBuf, 1024, pRead)) // 逐行读取
{
fputs(szBuf, pWrite); }
pclose(pRead);
pclose(pWrite);
} int main()
{
TestPipePopen_easy();
TestPipePopen_complex();
return 0;
}
题目:通过命名管道实现任意两个进程间通信(一个进程在读,另一个在写)。
实现代码:
/*
* 命名管道
* 进程间通信————任意两个进程
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h> void ReadFifo(int fd)
{
char szBuf[1024];
while (1) {
memset(szBuf, 0, 1024);
read(fd, szBuf, 1024);
printf("Process[PID:%d] Recv:%s\n", getpid(), szBuf);
if (strcmp(szBuf, "exit") == 0) {
return;
}
}
} void WriteFifo(int fd)
{
char szBuf[1024];
while (1) {
setbuf(stdout, NULL);
printf("Process[PID:%d]Send:", getpid());
scanf("%s", szBuf);
write(fd, szBuf, strlen(szBuf));
if (strcmp("exit", szBuf) == 0) {
return;
}
}
} int main(int argc, char **argv)
{
if (argc != 3) {
printf("usage: %s [r | w] filename\n", argv[0]);
printf("\tr: read the fifo\n");
printf("\tw: write the fifo\n");
printf("\tfilename: the fifo file name\n");
return -1;
}
int fd;
struct stat info;
stat(argv[2], &info);
if (!S_ISFIFO(info.st_mode)) { // 判断是否为管道文件
printf("this file is not fifo\n");
return -1;
}
if (argv[1][0] == 'r') {
fd = open(argv[2], O_RDONLY); // 打开管道文件
if (fd < 0){
perror("open fail");
return -1;
}
ReadFifo(fd);
}
else if (argv[1][0] == 'w') {
fd = open(argv[2], O_WRONLY);
if (fd < 0) {
perror("open fail");
return -1;
}
WriteFifo(fd);
}
close(fd);
return 0;
}
自测之Lesson10:管道的更多相关文章
- MySQL mysqlslap压测
200 ? "200px" : this.width)!important;} --> 介绍 mysqlslap是mysql自带的一个性能压测工具:mysqlslap用于和其 ...
- [软件测试]网站压测工具Webbench源码分析
一.我与webbench二三事 Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能.Webbench ...
- VS 2013驱动开发 + Windbg + VM双机调试(亲测+详解)
------------VS 2013驱动开发 + Windbg + VM双机调试(亲测+详解)------------- WIN10已上线,随之而来的是VS2015:微软在 "WDK760 ...
- 网站(Web)压测工具Webbench源码分析
一.我与webbench二三事 Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能.Webbench ...
- 性能测试:压测中TPS上不去的几种原因分析(就是思路要说清楚)
转https://www.cnblogs.com/imyalost/p/8309468.html 先来解释下什么叫TPS: TPS(Transaction Per Second):每秒事务数,指服务器 ...
- [apue] 测试管道容量的一些疑问
所谓管道的容量,指不消费(读)的情况下,最大能写入的数据量.有两种方式来测试一个管道的容量: 1)使用阻塞写,每次写一个字节,并打印写入的总字节数,最后写入阻塞时,上次打印的就是管道的容量: 2)使用 ...
- redis之管道
Redis 的消息交互当我们使用客户端对 Redis 进行一次操作时,如下图所示,客户端将请求传送给服务器,服务器处理完毕后,再将响应回复给客户端.这要花费一个网络数据包来回的时间. 如果连续执行多条 ...
- webbench网站测压工具源码分析
/* * (C) Radim Kolar 1997-2004 * This is free software, see GNU Public License version 2 for * detai ...
- 多测师讲解接口测试 _理论基础知识001_高级讲师肖sir
前言: 我们今天进入接口测试的学习! 今天学习的内容是偏向理论 接口理论 了解接口测试(1) 一.什么是接口测试? 接口统称api,即程序与程序之间的对接.交接.交互.是测试系统组件间接口的一种测试. ...
随机推荐
- 竞赛题解 - NOIP2018 赛道修建
\(\mathcal {NOIP2018}\) 赛道修建 - 竞赛题解 额--考试的时候大概猜到正解,但是时间不够了,不敢写,就写了骗分QwQ 现在把坑填好了~ 题目 (Copy from 洛谷) 题 ...
- Centos7验证Kickstart文件是否完整方法
1.1 功能简介 CentOS 7 包含 ksvalidator 命令行程序,可使用该程序进行确认Kickstart文件.这个工具是 pykickstart 软件包的一部分.要安装pykicks ...
- 利用tornado使请求实现异步非阻塞
基本IO模型 网上搜了很多关于同步异步,阻塞非阻塞的说法,理解还是不能很透彻,有必要买书看下. 参考:使用异步 I/O 大大提高应用程序的性能 怎样理解阻塞非阻塞与同步异步的区别? 同步和异步:主要关 ...
- 【Keil】Keil5的安装和破...
档案的话网上很多的,另外要看你开发的是哪种内核的芯片 如果是STC的,就安装C51 如果是STM的,就安装MDK 当然市面上有很多芯片的,我也没用过那么多种,这里也就不列举了 至于注册机,就是...恩 ...
- 第三篇 : vi编辑器配置与基本操作
目录 一.vi编辑器的配置 二.一般模式下的常用操作 一.vi编辑器的配置 配置文件位置 #配置文件virc(vi);vimrc(vim) cd /etc/vim //配置文件有在这目录的,也有可能是 ...
- python学习——初始面向对象
一.讲在前面 编程的世界中有三大体系,面向过程.面向函数和面向对象编程.而面向过程的编程就包括了面向函数编程,接下来说一下面向对象.假如 ,你现在是一家游戏公司的开发人员,现在需要你开发一款叫做< ...
- STM32_1 搭建工程框架
搭建系统框架 -- 创建系统文件夹 -- 拷贝stm32库文件 -- 将文件添加至工程 -- 配置工程环境 1. 创建工程文件夹 找一个工程目录,我就在 stm32/Code 下创建一个模板工程Tem ...
- 数据结构之链表及其Java实现
数据的存储一般分线性存储结构和链式存储结构两种.前者是一种顺序的存储方式,在内存中用一块连续的内存空间存储数据,即逻辑上相连的物理位置相邻,比较常见的就是数组:后者是一种链式存储方式,不保证顺序性,逻 ...
- Making AJAX Applications Crawlable
https://developers.google.com/webmasters/ajax-crawling/
- Hibernate怎么用
一.为什么用Hibernate? [核心:对象关系映射] Hibernate是对jdbc的轻量级封装,可以简化数据库连接操作, 在该框架之前,数据库的操作步骤是: 1.根据连接字串获取连接 2.执行s ...