{"id":6536,"date":"2023-04-02T15:13:20","date_gmt":"2023-04-02T15:13:20","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/pipes-and-filters\/"},"modified":"2023-08-23T17:06:15","modified_gmt":"2023-08-23T17:06:15","slug":"pipes-and-filters","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/pipes-and-filters\/","title":{"rendered":"Pipes-and-Filters"},"content":{"rendered":"<p>The Pipes-and-Filters architecture pattern describes the structure of systems that process data streams.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6531\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilter.png\" alt=\"PipesAndFilter\" width=\"650\" height=\"324\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilter.png 1229w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilter-300x150.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilter-1024x511.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilter-768x383.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>The Pipes-and-Filters pattern is similar to the Layers Pattern. The idea of the Layers Pattern is to structure the system in layers so that higher layers are based on the services of lower layers. The Pipes-and-Filters naturally extend the Layers Pattern, using the layers as filters and the data flow as pipes.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Pipes-and-Filters\"><\/span>Pipes-and-Filters<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Purpose\"><\/span>Purpose<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>A system that processes data in several steps<\/li>\n<li>Each step processes its data independently from the other<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Solution\"><\/span>Solution<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Divide the task into several processing steps<\/li>\n<li>Each processing step is the input for the next processing step<\/li>\n<li>The processing step is called a filter; the data channel between the filters is called a pipe<\/li>\n<li>The data comes from the data source and ends up in the data sink<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Structure\"><\/span>Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6532\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilterFlow.png\" alt=\"PipesAndFilterFlow\" width=\"650\" height=\"189\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilterFlow.png 791w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilterFlow-300x87.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFilterFlow-768x223.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6533\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFiltersStructure.png\" alt=\"PipesAndFiltersStructure\" width=\"650\" height=\"373\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFiltersStructure.png 1267w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFiltersStructure-300x172.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFiltersStructure-1024x588.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/PipesAndFiltersStructure-768x441.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<pre><code><b>Filter<\/b><\/code><\/pre>\n<ul>\n<li>Gets input data<\/li>\n<li>Performs its operation on the input data<\/li>\n<li>Produces output data<\/li>\n<\/ul>\n<pre><code><b>Pipe<\/b><\/code><\/pre>\n<ul>\n<li>Transmits data<\/li>\n<li>Buffers data in a queue<\/li>\n<li>Synchronizes neighbors<\/li>\n<\/ul>\n<pre><code><b>Data Source<\/b><\/code><\/pre>\n<ul>\n<li>Produces input to the processing pipeline<\/li>\n<\/ul>\n<pre><code><b>Data Sink<\/b><\/code><\/pre>\n<ul>\n<li>Consumes data<\/li>\n<\/ul>\n<p>The most interesting part of the Pipes-and-Filter is the data flow.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Data_Flow\"><\/span>Data Flow<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>There are several ways to control the data flow.<\/p>\n<h4>Push Principle<\/h4>\n<ul>\n<li>The filter is started by passing the data of the previous filter<\/li>\n<li>The (n-1)-th filter sends (write operation) data to the n-th filter<\/li>\n<li>The data source starts the data flow<\/li>\n<\/ul>\n<h4>Pull Principle<\/h4>\n<ul>\n<li>The filter is started by requesting data from the previous filter<\/li>\n<li>The n-th filter requests data from the (n-1)-th filter<\/li>\n<li>The data sink starts the data flow<\/li>\n<\/ul>\n<h4>Mixed Push\/Pull Principle<\/h4>\n<ul>\n<li>The n-th filter requests data from the (n-1)-th filter and explicitly passes it to the (n+1)-th filter<\/li>\n<li>The n-th filter is the only active filter in the processing chain&nbsp;&nbsp;&nbsp;&nbsp;<\/li>\n<li>The n-th filter starts the data flow<\/li>\n<\/ul>\n<h4>Active Filters as Independent Processes<\/h4>\n<ul>\n<li>Each filter is an independent process that reads data from the previous queue or writes data to the following queue<\/li>\n<li>The n-th filter can read data only after the (n-1)-th filter has written data to the connecting queue<\/li>\n<li>The n-th filter can write its data only after the (n+1)-th filter has read the connecting queue<\/li>\n<li>This structure is known as the Producer\/Consumer<\/li>\n<li>Each filter can start the data flow<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The most prominent example of the Pipes-and-Filters Pattern is the UNIX Command Shell.<\/p>\n<h4>Unix Command Shell<\/h4>\n<ul>\n<li>\n<p>Find the five python files in my python3.6 installation that have the most lines:<\/p>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6534\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/UnixPipe.png\" alt=\"UnixPipe\" width=\"650\" height=\"162\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/UnixPipe.png 997w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/UnixPipe-300x75.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/UnixPipe-768x191.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Here are the steps of the pipeline:<\/p>\n<ul>\n<li>Find all files ending with py:<code> find -name \"*.py\"<\/code><\/li>\n<li>Get from each file its number of lines: <code>xargs wc -l<\/code><\/li>\n<li>Sort numerical: <code>sort -g<\/code><\/li>\n<li>Remove the last two lines having irrelevant statistical information: <code>head -n -2<\/code><\/li>\n<li>Get the five last lines:<code> tail -5<\/code><\/li>\n<\/ul>\n<p>Finally, here is the classic of command line processing using pipes from <a href=\"https:\/\/de.wikipedia.org\/wiki\/Douglas_McIlroy\" moz-do-not-send=\"true\">Douglas Mcllroy<\/a>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">tr -cs A-Za-z <span style=\"color: #cc3300;\">'\\n'<\/span> |\r\ntr A-Z a-z |\r\nsort |\r\nuniq -c |\r\nsort -rn |\r\nsed <span style=\"color: #006699; font-weight: bold;\">${<\/span><span style=\"color: #003333;\">1<\/span><span style=\"color: #006699; font-weight: bold;\">}<\/span>q\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>If you want to know what this pipeline does, read the full story behind it in the article &#8220;<a href=\"http:\/\/www.leancrew.com\/all-this\/2011\/12\/more-shell-less-egg\/\" moz-do-not-send=\"true\">More shell, less egg<\/a>&#8220;.<\/p>\n<p>Thanks to the ranges library in C++20, the Pipes-and-Filters Pattern is directly supported in C++.<\/p>\n<h4>Ranges<\/h4>\n<p>The following program<code> firstTenPrimes.cpp<\/code> displays the first ten primes starting with 1000.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ firstTenPrimes.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;ranges&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #cc00ff;\">isPrime<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> i) {\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> j <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>; j <span style=\"color: #555555;\">*<\/span> j <span style=\"color: #555555;\">&lt;=<\/span> i; <span style=\"color: #555555;\">++<\/span>j){\r\n        <span style=\"color: #006699; font-weight: bold;\">if<\/span> (i <span style=\"color: #555555;\">%<\/span> j <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">0<\/span>) <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">false<\/span>;\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #336666;\">true<\/span>;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> odd <span style=\"color: #555555;\">=<\/span> [](<span style=\"color: #007788; font-weight: bold;\">int<\/span> i){ <span style=\"color: #006699; font-weight: bold;\">return<\/span> i <span style=\"color: #555555;\">%<\/span> <span style=\"color: #ff6600;\">2<\/span> <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">1<\/span>; };\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> vec <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>iota(<span style=\"color: #ff6600;\">1<\/span><span style=\"color: #aa0000; background-color: #ffaaaa;\">'<\/span><span style=\"color: #ff6600;\">000<\/span>) <span style=\"color: #555555;\">|<\/span> std<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>filter(odd)           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n                                       <span style=\"color: #555555;\">|<\/span> std<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>filter(isPrime)       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n                                       <span style=\"color: #555555;\">|<\/span> std<span style=\"color: #555555;\">::<\/span>views<span style=\"color: #555555;\">::<\/span>take(<span style=\"color: #ff6600;\">10<\/span>)              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n                                       <span style=\"color: #555555;\">|<\/span> std<span style=\"color: #555555;\">::<\/span>ranges<span style=\"color: #555555;\">::<\/span>to<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&gt;<\/span>();   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> v<span style=\"color: #555555;\">:<\/span> vec) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> v <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The data source (<code>std::views::iota(1'000)<\/code>)&nbsp; creates the natural number, starting with 1000. First, the odd numbers are filtered out (line 1), and then the prime numbers (line 2). This pipeline stops after ten values (line 3) and pushes the elements onto the<code> std::vector<\/code> (line 4). The convenient function<code> std::ranges::to<\/code> creates a new range (line 4). This function is new with C++23. Therefore, I can only execute the code with the newest windows compiler on the <a href=\"https:\/\/godbolt.org\/z\/dWaM6EvK4\">compiler explorer<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6535\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/firstTenPrimes.png\" alt=\"firstTenPrimes\" width=\"400\" height=\"53\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/firstTenPrimes.png 529w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/04\/firstTenPrimes-300x40.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Pros_and_Cons\"><\/span>Pros and Cons<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>I use in my following comparison the term universal interface. This means all filters speak the same language, such as xml or jason.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Pros\"><\/span>Pros<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>When one filter pushes or pulls the data directly from its neighbor, no intermediate buffering of data is necessary<\/li>\n<li>An n-th filter implements the Layers Pattern and can, therefore, easily be replaced<\/li>\n<li>Filters, implementing the universal interface, can be reordered<\/li>\n<li>Each filter can work independently of the other and has not had to wait until the neighbored filter is done. This enables the optimal distribution of work between the filters.<\/li>\n<li>Filters can run in a distributed architecture. The pipes connect the remote entities. The pipes can also split or synchronize the data flow. Pipes-and-Filters are heavily used in distributed or concurrent architectures and provide excellent performance and scalability opportunities.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Cons\"><\/span>Cons<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>The parallel processing of data may be inefficient due to communication, serialization, and synchronization overhead<\/li>\n<li>A filter such as a sort needs the entire data<\/li>\n<li>If the processing power of the filters is not homogenous, you need big queues between them<\/li>\n<li>To support the universal interface, that data must be formatted between the filters<\/li>\n<li>The most complicated part of this pattern is error handling. When the Pipes-and-Filters architecture crashes during the data processing, you have data that is not partially and fully processed. Now, you have a few options:\n<ul>\n<li>Start the process once more if you have the original data.<\/li>\n<li>Use only the fully processed data.<\/li>\n<li>Introduce markers in your input data. You start the process based on the markers when your system crashes.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Whats_Next\"><\/span>What&#8217;s Next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Broker structures distributed software systems that interact with remote service invocations. It is responsible for coordinating the communication, its results, and exceptions. In my next post, I will dive deeper into the architectural pattern Broker.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Pipes-and-Filters architecture pattern describes the structure of systems that process data streams.<\/p>\n","protected":false},"author":21,"featured_media":6531,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[413],"class_list":["post-6536","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-ranges"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6536"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6536\/revisions"}],"predecessor-version":[{"id":8138,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6536\/revisions\/8138"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6531"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}