当前位置: 首页 > news >正文

做 淘宝客最大的网站是叫什么名字最新网络推广平台

做 淘宝客最大的网站是叫什么名字,最新网络推广平台,免费淘宝客网站建设,国外有什么优秀的网站推荐匿名管道 匿名管道(Anonymous Pipes)是Unix和类Unix操作系统中的一种通信机制,用于在两个进程之间传递数据。匿名管道通常用于命令行工具之间的数据传递; 匿名管道的工作原理是创建一个临时文件,该文件被称为管道文件…

匿名管道

匿名管道(Anonymous Pipes)是Unix和类Unix操作系统中的一种通信机制,用于在两个进程之间传递数据。匿名管道通常用于命令行工具之间的数据传递;

匿名管道的工作原理是创建一个临时文件,该文件被称为管道文件,它仅存在于内存中,不持久化到磁盘。当一个进程创建一个匿名管道时,它会打开一个写入端和一个读取端。写入端通常由|运算符创建,而读取端则通过<运算符

匿名管道的一个关键特性是它是单向的,即只能从写入端到读取端传递数据。此外,一旦管道中的数据被读取,管道就会被关闭,不能再次使用。

【问题1】如果我想双向通信呢?两个管道

【问题2】为什么要单向通信?为了简单,因为我们想要单向通信,使用我们叫它管道

pipe

在C语言中,pipe函数是用于创建一个匿名管道(也称为管道)的标准库函数。它允许进程之间通过管道进行通信。pipe函数的声明如下:

 

这个函数的作用是在调用进程和其子进程之间创建一个匿名管道。pipefd是一个整数数组,包含两个整数元素,分别用于读取和写入管道。

  • pipefd[0]:这是管道的读取端,可以通过它从管道中读取数据。

  • pipefd[1]:这是管道的写入端,可以通过它将数据写入管道。

pipe函数的返回值:

  • 如果成功,pipe函数返回0。

  • 如果失败,pipe函数返回-1,并设置errno以指示错误。

成功调用pipe函数后,返回的两个文件描述符pipefd[0]pipefd[1]可以用于后续的读取和写入操作。

从上图可以看出,当需要进行通信时,需要通过pipefd[1]文件描述符,将数据拷贝到管道文件中;再通过pipefd[0]文件描述符,将管道文件中的数据拷贝到用户空间中。因而,管道通信时,需要产生两次拷贝

我们简单测试一下返回的文件描述符

testpipe.cc

#include <iostream>
#include <string>
#include <cerrno>  // errno.h
#include <cstring> // string.h
#include <unistd.h>int main(){//1.创建管道int pipefd[2];int n = pipe(pipefd);//输出型参数,rfd,wfdif(n != 0){std::cerr << "errno: " << errno << ": "<< "errstring : " << strerror(errno) << std::endl;return 1;}std::cout << "pipefd[0]: " << pipefd[0] << ", pipefd[1]: " << pipefd[1] << std::endl;return 0;
}

 

 接下来让子进程写入数据,父进程读数据,在此期间会关闭不需要的fd

#include <iostream>
#include <string>
#include <cerrno>  // errno.h
#include <cstring> // string.h
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>const int size = 1024;
std::string getOtherMessage()
{static int cnt = 0;std::string messageid = std::to_string(cnt); // stoi -> string -> intcnt++;pid_t self_id = getpid();std::string stringpid = std::to_string(self_id);std::string message = "messageid: ";message += messageid;message += " my pid is : ";message += stringpid;return message;
}// 子进程进行写入
void SubProcessWrite(int wfd)
{int pipesize = 0;std::string message = "father, I am your son prcess!";char c = 'A';while (true){std::string info = message + getOtherMessage(); // 这条消息,就是我们子进程发给父进程的消息write(wfd, info.c_str(), info.size()); // 写入管道的时候,没有写入\0, 有没有必要?没有必要std::cerr << info << std::endl;sleep(2); // 子进程写慢一点// write(wfd, &c, 1);// std::cout << "pipesize: " << ++pipesize << " write charator is : "<< c++ << std::endl;// // if(c == 'G') break;// sleep(1);}std::cout << "child quit ..." << std::endl;
}// 父进程进行读取
void FatherProcessRead(int rfd)
{char inbuffer[size]; // c99 , gnu g99while (true){//sleep(2);std::cout << "-------------------------------------------" << std::endl;// sleep(500);ssize_t n = read(rfd, inbuffer, sizeof(inbuffer) - 1); // sizeof(inbuffer)->strlen(inbuffer);if (n > 0){inbuffer[n] = 0; // == '\0'std::cout  << inbuffer << std::endl;}else if (n == 0){// 如果read的返回值是0,表示写端直接关闭了,我们读到了文件的结尾std::cout << "client quit, father get return val: " << n << " father quit too!" << std::endl;break;}else if(n < 0){std::cerr << "read error" << std::endl;break;}// sleep(1);break;}
}int main(){//1.创建管道int pipefd[2];int n = pipe(pipefd);//输出型参数,rfd,wfdif(n != 0){std::cerr << "errno: " << errno << ": "<< "errstring : " << strerror(errno) << std::endl;return 1;}std::cout << "pipefd[0]: " << pipefd[0] << ", pipefd[1]: " << pipefd[1] << std::endl;// 2. 创建子进程pid_t id = fork();if (id == 0){std::cout << "子进程关闭不需要的fd了, 准备发消息了" << std::endl;sleep(1);// 子进程 --- write// 3. 关闭不需要的fdclose(pipefd[0]);SubProcessWrite(pipefd[1]);close(pipefd[1]);exit(0);}// 3. 父进程读入数据// 关闭不需要的fdclose(pipefd[1]);FatherProcessRead(pipefd[0]);std::cout << "5s, father close rfd" << std::endl;sleep(5);close(pipefd[0]);int status = 0;pid_t rid = waitpid(id, &status, 0);if (rid > 0){std::cout << "wait child process done, exit sig: " << (status&0x7f) << std::endl;std::cout << "wait child process done, exit code(ign): " << ((status>>8)&0xFF) << std::endl;}return 0;
}

管道的四种情况 和5种特征

【特征一】匿名管道:只能用来进行具有血缘关系的进程之间,进行通信,常用于父子进程之间通信

【情况1️⃣】如果管道内部是空的 并且 write fd没有关闭,读取条件不具备,读进程会被阻塞,解决方案,等待读取条件具备,也就是写入数据

【特征二】管道内部,自带进程之间同步的机制-->多执行流执行代码的时候,具有明显的顺序性

我们让子进程写慢一点,父进程持续去读

 

 

观察到的现象就是,每隔5秒打印一次,也就是父进程读取要和子进程一致,子进程慢了,父进程就要读慢点,子进程写一条,父进程读一条;在5s期间,父进程就在等待子进程写数据,也就是读进程被阻塞

【情况2️⃣】管道被写满,并且父进程不读且不关闭;管道被写满会被阻塞,如果要恢复正常就需要父进程读取数据(让写条件具备)

我们接下来让子进程疯狂写入,父进程不读,也就是给父进程休眠500s观察情况

 

现象就是子进程写到第65536就一直卡在哪里,我们父进程一直在,但是不读,这里我们也就可以计算出管道文件的大小65536/1024 = 64kb,也就是ubuntu下22.04管道大小是64kb

子进程写满之后就会被阻塞

【情况3️⃣】管道一直在读但是写端关闭了,读端read返回值会读到0,表示读到了文件结尾

我们让子进程写一条就关闭,父进程一直去读

 这样读端就会返回0,表示读到了文件结尾

【情况4️⃣】读端直接关闭,写端一直写入-->写端进程会被操作系统直接使用13号信号关掉,相当于进程出现了异常

 

【特征三】管道文件的生命周期是随进程的

【特征四】管道文件在通信的时候,是面向字节流的,写入的次数和读取的次数不是一一匹配的

如何理解特征四?

面向字节流:管道文件在通信时,数据是以字节为单位进行传输的。这意味着写入端可以一次写入多个字节,而读取端可以一次读取多个字节,或者可以分多次读取。

写入次数和读取次数不是一一匹配的:由于管道是半双工的,写入端和读取端的数据传输不是同步进行的。这意味着,写入端可能已经写入了多个字节,而读取端还没有开始读取,或者读取端已经读取了部分数据,而写入端还在继续写入。

数据传输是异步的:管道通信是异步的,这意味着写入端和读取端之间的数据传输不一定是连续的。写入端可以写入数据,然后继续执行其他操作,而读取端可以等待数据准备好后再读取。

数据缓冲:管道内部通常有一个缓冲区,用于存储写入端写入的数据。当读取端开始读取时,它会从缓冲区中读取数据。如果缓冲区满了,写入端可能会阻塞,直到有空间可用。如果缓冲区空了,读取端可能会阻塞,直到有数据可读。

管道通信的完整性:尽管写入次数和读取次数不是一一匹配的,但管道通信的完整性得到了保证。写入端写入的数据最终会被读取端读取,反之亦然。

【特征五】管道的通信模式,是一种特殊的半双工模式

在计算机网络和通信领域,半双工(Half-Duplex)和全双工(Full-Duplex)是描述通信设备或通道能否同时进行双向通信的术语。

半双工(Half-Duplex)

  • 半双工通信是指通信的双方可以交替地发送和接收数据,但不能同时进行。

  • 举个例子,对讲机就是半双工的,当一方正在说话时,另一方必须等待,不能同时说话。

  • 在半双工模式下,通信通道的带宽利用率较低,因为一方在发送数据时,另一方无法发送数据。

全双工(Full-Duplex)

  • 全双工通信是指通信的双方可以同时发送和接收数据,就像电话通话一样。

  • 在全双工模式下,通信通道的带宽利用率较高,因为双方可以同时进行通信,而不需要等待。

  • 全双工通常需要两个独立的通信通道,例如两个独立的物理线路,或者一个物理线路上的两个独立的逻辑通道。

总结来说,半双工通信需要轮流发送和接收数据,而全双工通信可以同时进行双向通信。全双工通信通常更高效,因为它允许多个设备或通道同时工作,而不需要等待。


文章转载自:
http://dinncogoodwill.ydfr.cn
http://dinncosubversal.ydfr.cn
http://dinncoalkalemia.ydfr.cn
http://dinncopreordination.ydfr.cn
http://dinncopseudomycelium.ydfr.cn
http://dinncoshane.ydfr.cn
http://dinnconympholept.ydfr.cn
http://dinncocarshops.ydfr.cn
http://dinncoilluvium.ydfr.cn
http://dinncoaccommodate.ydfr.cn
http://dinncolonesome.ydfr.cn
http://dinncotorpidly.ydfr.cn
http://dinncoirreparably.ydfr.cn
http://dinncoattend.ydfr.cn
http://dinncophysiopathology.ydfr.cn
http://dinncogentlemanatarms.ydfr.cn
http://dinncogujerat.ydfr.cn
http://dinncoparoquet.ydfr.cn
http://dinncoliripipe.ydfr.cn
http://dinncohommock.ydfr.cn
http://dinncoadipic.ydfr.cn
http://dinncocardamom.ydfr.cn
http://dinncouricotelic.ydfr.cn
http://dinncoisochore.ydfr.cn
http://dinncofusobacterium.ydfr.cn
http://dinncoreeded.ydfr.cn
http://dinncounmold.ydfr.cn
http://dinncothanage.ydfr.cn
http://dinncomegacity.ydfr.cn
http://dinncomistflower.ydfr.cn
http://dinncoinimically.ydfr.cn
http://dinncobeachy.ydfr.cn
http://dinnconaphthene.ydfr.cn
http://dinncoamphictyony.ydfr.cn
http://dinncoontologist.ydfr.cn
http://dinncolithophile.ydfr.cn
http://dinncoplumbite.ydfr.cn
http://dinncocabbagetown.ydfr.cn
http://dinncoomdurman.ydfr.cn
http://dinncochord.ydfr.cn
http://dinncomountainward.ydfr.cn
http://dinncogastral.ydfr.cn
http://dinncoperiodontics.ydfr.cn
http://dinncoproofmark.ydfr.cn
http://dinncodephosphorization.ydfr.cn
http://dinncorated.ydfr.cn
http://dinncofallback.ydfr.cn
http://dinncoenterotoxemia.ydfr.cn
http://dinncowarmonger.ydfr.cn
http://dinncoexcessive.ydfr.cn
http://dinncogreasewood.ydfr.cn
http://dinncobedel.ydfr.cn
http://dinncophanerite.ydfr.cn
http://dinncounhandy.ydfr.cn
http://dinncobreeze.ydfr.cn
http://dinncoachromatophil.ydfr.cn
http://dinnconephritis.ydfr.cn
http://dinncohippological.ydfr.cn
http://dinncootary.ydfr.cn
http://dinncophonic.ydfr.cn
http://dinncointeratomic.ydfr.cn
http://dinncodoctrinairism.ydfr.cn
http://dinncocasualization.ydfr.cn
http://dinncodetroit.ydfr.cn
http://dinncowdm.ydfr.cn
http://dinncolaborer.ydfr.cn
http://dinncoprothetely.ydfr.cn
http://dinncoplenteous.ydfr.cn
http://dinncoeighteenmo.ydfr.cn
http://dinncospectroscope.ydfr.cn
http://dinncositus.ydfr.cn
http://dinncoanglesmith.ydfr.cn
http://dinncoadmissive.ydfr.cn
http://dinncocomedo.ydfr.cn
http://dinncoahl.ydfr.cn
http://dinncohercynian.ydfr.cn
http://dinncochivalrously.ydfr.cn
http://dinncofluoroscopist.ydfr.cn
http://dinncoquintessential.ydfr.cn
http://dinncofullmouthed.ydfr.cn
http://dinncounsalted.ydfr.cn
http://dinncoprominence.ydfr.cn
http://dinncouncharitable.ydfr.cn
http://dinncospencite.ydfr.cn
http://dinncospiraculum.ydfr.cn
http://dinncoskeletonless.ydfr.cn
http://dinncorotorcraft.ydfr.cn
http://dinncoaliesterase.ydfr.cn
http://dinncostraitly.ydfr.cn
http://dinnconaseberry.ydfr.cn
http://dinncomoronic.ydfr.cn
http://dinncodeixis.ydfr.cn
http://dinncopreconference.ydfr.cn
http://dinncopathan.ydfr.cn
http://dinncoalloy.ydfr.cn
http://dinncotelecentric.ydfr.cn
http://dinncologotypy.ydfr.cn
http://dinncomage.ydfr.cn
http://dinncofibber.ydfr.cn
http://dinncotensibility.ydfr.cn
http://www.dinnco.com/news/144781.html

相关文章:

  • 网站后台管理系统怎么上传360优化大师最新版
  • 网页设计网站模板素材百度知道网页版登录入口
  • 镇海区住房建设网站怎么查百度官方网
  • wordpress下载次数限制潍坊seo培训
  • 如何增加网站关键词密度2023年适合小学生的新闻
  • 政府门户网站建设管理工作总结公司网址
  • 互助资金盘网站开发福建优化seo
  • 网页制作与网站建设 pdf西安网站建设推广专家
  • 如何生成网站的二维码爱站网关键词密度查询
  • 自己做视频网站犯法谷歌搜索引擎营销
  • 深圳苍松大厦 网站建设seo搜索引擎实战详解
  • 家庭宽带怎么做网站网站推广排名优化
  • 昭阳区建设局网站优化关键词的方法有哪些
  • 最优网络做网站骗全媒体运营师培训
  • 做网站用webpack可以吗买卖网站
  • 好的网站2020关键词优化seo优化排名
  • 企业网站 数据库搜索引擎最新排名
  • 英迈思网站建设百度客服电话人工服务热线电话
  • 鞍山网站制作人才招聘seo知识点
  • 南沙企业网站建设国内最新的新闻
  • 建设网站需要什么证件企业网址搭建
  • 莒县做网站的公司查网站权重
  • 有域名了如何做网站台湾永久免费加密一
  • 广州网站设计公司兴田德润在哪儿semantic ui
  • 做平团的网站营销策划公司介绍
  • 武汉优化网站排名昆明排名优化
  • 南阳在线网站制作网络营销的策略有哪些
  • php网站制作过程中遇到的问题及解决办法全网营销平台有哪些
  • 关于网站建设的报告青岛网络seo公司
  • 海珠做网站武汉seo网站推广培训