PIPE

1. 管道介绍

管道是最早的一种进程间通信方式,这种实现有一定使用限制:

设计目标:

  1. 进程之间通过字节流的方式进行通信!
  2. 管道是以半双工方式工作的

设计限制:
       参与通信的进程之间应具有亲缘关系(以便进程间共享Pipe文件)

管道说明:
       两个进程为了实现通信,两个进程应该拥有共享的资源-----这里就是使用pipe函数创建的管道设备
       父进程调用pipe函数创建管道设备,紧接着通过fork出新的进程,子进程会继承父进程的管道文件描述符
在这里插入图片描述

2. 管道接口

  1. 创建管道
#include <unistd.h>

int pipe(int pipefd[2]);

CONFORMING TO:
pipe(): POSIX.1-2001, POSIX.1-2008.
  1. 读写管道
#include <unistd.h>

ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);

  1. 文件描述符说明
           管道在内核中的表示是一种管道类型的文件!它像极了普通文件那样,可以读,可以写。唯一区别在于普通文件使用一个fd文件描述符既可以读,又可以写;但是管道需要两个文件描述符,一个用于读,一个用于写

2.1 非典型用法(原理演示)

非典型的数据流:
发送者: 主进程 接收者:主进程
主进程发一个Hello pipe!给管道设备,然后主进程自己读取

在这里插入图片描述

#include <iostream>
#include <cstring>
using namespace std;
extern "C"
{
       #include <unistd.h>
      /*
	POSIX.1-2001, POSIX.1-2008
	int pipe(int pipefd[2]);

        RETURN VALUE:
        On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
      */
}


int main(void)
{
	int pipes[2];
	
	int res = pipe(pipes);
	if(-1 == res)
	{
		cout << "pipe creation failed" << endl;
		return -1;
	}
	
	char const* str = "Hello Pipe!";
	res = write(pipes[1],str,strlen(str) + 1);
	if(-1 == res)
	{
		cout << "pipe write failed" << endl;
		return -1;
	}

	char read_buff[1024]; 

	res = read(pipes[0],read_buff,1024);
	if(-1 ==  res)
	{
		cout << "pipe read failed" << endl;
		return -1;
	}

	cout << "Got you! " << read_buff << endl;
	return 0;
}

输出:Got you! Hello Pipe!

2.2 典型用法(原理演示)

典型数据流:
发送者:父进程 接受者:子进程

主进程发一个Hello son!给管道设备,子进程读取这个信息

备注:
主进程用不到fd[0],子进程用不到fd[1],最好把他们关闭(也可以不这样)

在这里插入图片描述



#include <iostream>
#include <cstring>
using namespace std;
extern "C"
{
       #include <unistd.h>	
       #include <sys/types.h>
       #include <sys/wait.h>

      /*
	POSIX.1-2001, POSIX.1-2008
	int pipe(int pipefd[2]);

        RETURN VALUE:
        On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
      */
}


int main(void)
{
	int pipes[2];
	
	int res = pipe(pipes);
	if(-1 == res)
	{
		cout << "pipe creation failed" << endl;
		return -1;
	}
		
	pid_t pid = fork();
	if(pid == -1)
	{
		cout << "Err: fork failed,exiting!" << endl;
		return -1;
		
	}
	
	if(pid == 0)
	{
		//child
		char read_buff[1024]; 
		close(pipes[1]);

		res = read(pipes[0],read_buff,1024);
		if(-1 ==  res)
		{
			cout << "pipe read failed" << endl;
			return -1;
		}
		
		cout << "Got you! " << read_buff << endl;
		close(pipes[0]);
	}
	else
	{
		//father
		close(pipes[0]);
		char const* str = "Hello Son!";
		res = write(pipes[1],str,strlen(str) + 1);
		if(-1 == res)
		{
			cout << "pipe write failed" << endl;
			return -1;
		}
		close(pipes[1]);
		int status = 0;
		/*回收子进程*/
		pid = wait(&status);
		if(-1 == pid)
		{
			cout << "wait for child exit failed" << endl;
		}
	}
	
	return 0;
}

输出:Got you! Hello Son!

2.3 设计注意问题

  1. pipes[0]称为读端,pipes[1]称为写端
          当与管道设备关联的所有读端都已经关闭,写端进行写操作会产生SIGPIPE信号给写进程(如果进程忽略或者捕获此信号,write返回-1,errno设置为EPIPE)
          当与管道关联的所有写端都已经关闭,读端进行读操作会返回0,即什么也读取不到

  2. 管道设备的内核缓冲是有限制的 PIPE_BUFF用来标识其大小
          POSIX规定,对管道的一次写操作的字节个数n<PIPE_BUFF应当是原子的
    但是n > PIPE_BUFF,数据可能会其它进程写入的数据交差(这是不可接受的),代码设计应当尽量保证 n < PIPE_BUFF

获取最大的缓冲SIZE:我的系统上是4096

#include <iostream>
#include <cstring>
using namespace std;
extern "C"
{
       #include <unistd.h>	

/*	
       POSIX.1-2001, POSIX.1-2008.
       long fpathconf(int fd, int name);
       long pathconf(const char *path, int name);
*/
}

int main()
{
	int pipes[2];
	
	int res = pipe(pipes);
	if(-1 == res)
	{
		c*斜体样式*out << "pipe creation failed" << endl;
		return -1;
	}
	cout << fpathconf(pipes[0],_PC_PIPE_BUF) << endl;
	cout << fpathconf(pipes[1],_PC_PIPE_BUF) << endl;
	return 0;
}

3. 管道应用

3.1 shell 管道线

下图是管道进程间通信的一种应用
ls命令读取当前目录的文件列表,并把数据打印到标准输出 STDOUT_FILENO
但是shell非常取巧的把STDOUT_NO的文件描述符指向一个管道设备
并且把more命令的标准输入STDIN_FILENO指向同一个管道设备,最终实现了ls进程的输出递送个more进程!!!
ls -l | more

3.2 设计一个管道线支持shell

架构如下:
ls命令标准输出重定向到管道设备,more命令标准输入重定向到管道设备
在这里插入图片描述
设计图下:
在这里插入图片描述
设计有两个缺陷:
其一、shell进程应当读取用户键入的命令行,然后执行对应命令,这里在code里写死了执行ls和cat的组合
其二、shell进程设置left cmd 进程为一个独立的进程组和前台进程组,left cmd进程应该等待shell 进程完成设置再继续执行,这个例子中简单使用sleep函数同步是不严谨的…


#include <iostream>
#include <cstring>
#include <string>

using namespace std;
extern "C"
{
       #include <unistd.h>	
       #include <sys/types.h>
       #include <sys/wait.h>
       #include <stdio.h>

      /*
	POSIX.1-2001, POSIX.1-2008
	int pipe(int pipefd[2]);

        RETURN VALUE:
        On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.
      */
}

int main(int argc, char* argv[])
{	

	
	pid_t pid = fork();
	if(pid == -1)
	{
		cout << "Err: fork failed,exiting!" << endl;
		return -1;
		
	}
	
	if(pid == 0)
	{
		//left cmd process

		sleep(5);

		int pipes[2];
		int res = pipe(pipes);
		if(-1 == res)
		{
			cout << "pipe creation failed" << endl;
			return -1;
		}
		
		pid = fork();
		if(-1 == pid)
		{
			cout << "fork failed" << endl;
		}
		if(pid == 0)
		{
			// right cmd process as father and front group
			dup2(pipes[0],STDIN_FILENO);
			close(pipes[0]);
			close(pipes[1]);
			execlp("cat","cat",NULL);
			
			int status;
			wait(&status);
			
		}
		else
		{	// left cmd process as child
			dup2(pipes[1],STDOUT_FILENO);
			close(pipes[0]);
			close(pipes[1]);
			execlp("ls","ls",NULL);
			
		}

	}
	else
	{

		/*shell process*/

		/*set child as another group*/
		setpgid(pid,pid);

		/*set child as front gruop*/
		if(-1 == tcsetpgrp(STDIN_FILENO,pid))
		{
			cout << "set child as front failed" << endl;
		}

		int status = 0;
		/*回收子进程*/
		pid = wait(&status);
		if(-1 == pid)
		{
			cout << "wait for child exit failed" << endl;
			
		}
	}
	
	return 0;
}

3.3 popen pclose函数

#include <stdio.h>

FILE *popen(const char *command, const char *type);

int pclose(FILE *stream);

CONFORMING TO
       POSIX.1-2001, POSIX.1-2008.

popen 函数和pclose函数封装了数据流重定向的工作!简化编程工作

在这里插入图片描述

内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值