

This example shows some simple usage of ecore\+\_\+pipe. We are going to create a pipe, fork our process, and then the child is going to communicate to the parent the result of its processing through the pipe.

As always we start with our includes, nothing special\+: 
\begin{DoxyCodeInclude}
\textcolor{preprocessor}{#include <unistd.h>}
\textcolor{preprocessor}{#include <Ecore.h>}

\end{DoxyCodeInclude}


The first thing we are going to define in our example is the function we are going to run on the child process, which, as mentioned, will do some processing and then will write the result to the pipe\+: 
\begin{DoxyCodeInclude}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
do\_lengthy\_task(\mbox{\hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe}} *\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}})
\{
   \textcolor{keywordtype}{int} i, j;
   \textcolor{keywordtype}{char} *buffer;
   \textcolor{keywordflow}{for} (i = 0; i < 20; i++)
     \{
        sleep(1);
        buffer = malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{char}) * i);
        \textcolor{keywordflow}{for} (j = 0; j < i; j++)
          buffer[j] = \textcolor{charliteral}{'a'} + j;
        \mbox{\hyperlink{group__Ecore__Pipe__Group_ga5336bc71e829c391926c26750ae0f407}{ecore\_pipe\_write}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}}, buffer, i);
        free(buffer);
     \}
   \mbox{\hyperlink{group__Ecore__Pipe__Group_ga5336bc71e829c391926c26750ae0f407}{ecore\_pipe\_write}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}}, \textcolor{stringliteral}{"close"}, 5);
\}

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
The sleep was added so the parent process would think the child process was doing something interesting...
\end{DoxyNote}
Next up is our function for handling data arriving in the pipe. It copies the data to another buffer, adds a terminating N\+U\+LL and prints it. Also if it receives a certain string it stops the main loop(effectively ending the program)\+: 
\begin{DoxyCodeInclude}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
handler(\textcolor{keywordtype}{void} *data \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}}, \textcolor{keywordtype}{void} *buf, \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} len)
\{
   \textcolor{keywordtype}{char} *str = malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{char}) * len + 1);
   memcpy(str, buf, len);
   str[len] = \textcolor{charliteral}{'\(\backslash\)0'};
   printf(\textcolor{stringliteral}{"received %u bytes\(\backslash\)n"}, len);
   printf(\textcolor{stringliteral}{"content: %s\(\backslash\)n"}, (\textcolor{keyword}{const} \textcolor{keywordtype}{char} *)str);
   free(str);
   \textcolor{keywordflow}{if} (len && !strncmp(buf, \textcolor{stringliteral}{"close"}, len < 5 ? len : 5))
     \{
        printf(\textcolor{stringliteral}{"close requested\(\backslash\)n"});
        \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();
     \}
\}

\end{DoxyCodeInclude}


And now on to our main function, we start by declaring some variables and initializing ecore\+: 
\begin{DoxyCodeInclude}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{void})
\{
   \mbox{\hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe}} *\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}};
   pid\_t child\_pid;

   \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();

\end{DoxyCodeInclude}


And since we are talking about pipes let\textquotesingle{}s create one\+: 
\begin{DoxyCodeInclude}

   \mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}} = \mbox{\hyperlink{group__Ecore__Pipe__Group_ga3d584a8f962644f748dfd84d39d3421a}{ecore\_pipe\_add}}(handler, NULL);

\end{DoxyCodeInclude}


Now we are going to fork\+: 
\begin{DoxyCodeInclude}

   child\_pid = fork();

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
duh...
\end{DoxyNote}
The child process is going to do the our fancy processing\+: 
\begin{DoxyCodeInclude}
   \textcolor{keywordflow}{if} (!child\_pid)
     \{
        \mbox{\hyperlink{group__Ecore__Pipe__Group_gab9df469d955d67dbcd95fd6f2dabdc79}{ecore\_pipe\_read\_close}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}});
        do\_lengthy\_task(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}});
     \}

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
It\textquotesingle{}s very important to call \mbox{\hyperlink{group__Ecore__Pipe__Group_gab9df469d955d67dbcd95fd6f2dabdc79}{ecore\+\_\+pipe\+\_\+read\+\_\+close()}} here so that the child process won\textquotesingle{}t read what it is writing to the pipe itself.
\end{DoxyNote}
And the parent is going to run ecore\textquotesingle{}s main loop waiting for some data\+: 
\begin{DoxyCodeInclude}
   \textcolor{keywordflow}{else}
     \{
        \mbox{\hyperlink{group__Ecore__Pipe__Group_ga60099202e0880dd3de617ca14fa0c58b}{ecore\_pipe\_write\_close}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}});
        \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();
     \}

\end{DoxyCodeInclude}
 \begin{DoxyNote}{Note}
Calling \mbox{\hyperlink{group__Ecore__Pipe__Group_ga60099202e0880dd3de617ca14fa0c58b}{ecore\+\_\+pipe\+\_\+write\+\_\+close()}} here isn\textquotesingle{}t important but since we aren\textquotesingle{}t going to write in the pipe it is good practice.
\end{DoxyNote}
And finally when done processing(the child) or done receiving(the parent) we delete the pipe and shutdown ecore\+: 
\begin{DoxyCodeInclude}

   \mbox{\hyperlink{group__Ecore__Pipe__Group_ga7ed5669e83a3119d2023c64288f9af60}{ecore\_pipe\_del}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}});
   \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();

   \textcolor{keywordflow}{return} 0;
\}

\end{DoxyCodeInclude}
 