Creating a processes and IPC (Inter process communication)

In this example we will show how to create a new process and communicate with it in a portable way using the Ecore\+\_\+exe module.

In this example we will have two process and both will communicate with each other using messages. A father process will start a child process and it will keep sending messages to the child until it receives a message to quit. To see the full source use the links\+: \begin{DoxyItemize}
\item \mbox{\hyperlink{ecore_exe_example_8c-example}{Father}} \item \mbox{\hyperlink{ecore_exe_example_child_8c-example}{Child}}\end{DoxyItemize}
Let\textquotesingle{}s start the tutorial. The implementation of the child it\textquotesingle{}s pretty simple. We just read strings from stdin and write a message in the stdout. But you should be asking yourself right know. \char`\"{}\+If I\textquotesingle{}m receiving data from an other process why I\textquotesingle{}m reading and writing in stdin/stdout?\char`\"{}. That\textquotesingle{}s because, when you spawn a process using the \mbox{\hyperlink{structEcore__Exe}{Ecore\+\_\+\+Exe}} module it will create a pipe between the father and the child process and the stdin/stdout of the child process will be redirected to the pipe. So when the child wants to receive or send data to the father, just use the stdin/stdout. However the steps to send data from the father to the child is quite different, but we will get there.

The child will register a fd handler to monitor the stdin. So we start registering the ecore FD handler\+:  
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_gaf8483e27f0c9fd4a2a7c97fa5539a55f}{ecore\_main\_fd\_handler\_add}}(STDIN\_FILENO,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a1699f804eccf88e7e169a4a20f1d44e7}{ECORE\_FD\_READ}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_fd\_handler\_cb,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL,\ NULL,\ NULL);}

\end{DoxyCodeInclude}
 If you don\textquotesingle{}t remember the parameters of \mbox{\hyperlink{group__Ecore__FD__Handler__Group_gaf8483e27f0c9fd4a2a7c97fa5539a55f}{ecore\+\_\+main\+\_\+fd\+\_\+handler\+\_\+add}}, please check its documentation.

Now that we have our handler registered we will start the ecore\textquotesingle{}s main loop\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}

\end{DoxyCodeInclude}
 Now let\textquotesingle{}s take a look in the callback function. Its a simple function that will read from stdin 3 times and at the third time will say to the father\+: \char`\"{}quit\char`\"{}.  
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_fd\_handler\_cb(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ HANDLER\_TYPE\ *fd\_handler\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ numberOfMessages\ =\ 0;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ message[BUFFER\_SIZE];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!fgets(message,\ BUFFER\_SIZE,\ stdin))}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ numberOfMessages++;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (numberOfMessages\ <\ 3)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}My\ father\ sent\ this\ message\ to\ me:\%s\(\backslash\)n"{}},\ message);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ fflush(stdout);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}quit\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ fflush(stdout);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga780091d7d49a4bb6cb753f12e1a3b19d}{ECORE\_CALLBACK\_DONE}};}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}())}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordflow}{goto}\ error;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef\ \_WIN32}}
\DoxyCodeLine{\ \ \ \textcolor{comment}{/*\ note\ that\ stdin\ fd's\ on\ windows\ don't\ work\ the\ same}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ *\ as\ on\ unixes.\ this\ uses\ stdin\ just\ as\ a\ quick}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ *\ example\ that's\ simple\ instead\ of\ a\ more\ complex}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ *\ one,\ so\ this\ won't\ actually\ work\ on\ windows\ unless}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ *\ you\ use\ a\ fd\ that\ comes\ from\ somewhere\ that\ is}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ *\ select()able.\ */}}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga2d9e673038e129917a49df1e0ea63e72}{ecore\_main\_win32\_handler\_add}}(GetStdHandle(STD\_INPUT\_HANDLE),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_fd\_handler\_cb,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL);}
\DoxyCodeLine{\textcolor{preprocessor}{\#else}}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_gaf8483e27f0c9fd4a2a7c97fa5539a55f}{ecore\_main\_fd\_handler\_add}}(STDIN\_FILENO,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a1699f804eccf88e7e169a4a20f1d44e7}{ECORE\_FD\_READ}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_fd\_handler\_cb,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL,\ NULL,\ NULL);}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ EXIT\_SUCCESS;}
\DoxyCodeLine{}
\DoxyCodeLine{error:}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ EXIT\_FAILURE;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 You may notice that we are sending the messages to stdout, and our father will receive it. Also our string must have a \char`\"{}\textbackslash{}n\char`\"{} because the string will be buffered in the pipe until it finds EOF or a \char`\"{}newline\char`\"{} in our case we won\textquotesingle{}t have a EOF unless we close the pipe, so we use the \char`\"{}\textbackslash{}n\char`\"{} char.

One more thing, we use fflush(stdout) because probably our message won\textquotesingle{}t fill our entire buffer and the father would never receive the message. So we use this function to flush the buffer and the father can receive as fast as possible.

Now that we have our child ready, let\textquotesingle{}s start our work in the father\textquotesingle{}s source code.

We start creating the child process like this\+:  
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ childHandle\ =\ \mbox{\hyperlink{group__Ecore__Exe__Group_ga3446e8232234ab6a72b5b486304fa527}{ecore\_exe\_pipe\_run}}(\textcolor{stringliteral}{"{}./ecore\_exe\_example\_child"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore_gga26f57bfa7ecfa8eee5fc053e342880c5a83dcb150caa9f5f4ce548eaa4a92c6af}{ECORE\_EXE\_PIPE\_WRITE}}\ |}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore_gga26f57bfa7ecfa8eee5fc053e342880c5ae97566413d58b792e92369966ffeece5}{ECORE\_EXE\_PIPE\_READ\_LINE\_BUFFERED}}\ |}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore_gga26f57bfa7ecfa8eee5fc053e342880c5a76bc9e447a456727dc2d686b4682b512}{ECORE\_EXE\_PIPE\_READ}},\ NULL);}

\end{DoxyCodeInclude}
 With the command above we are creating our child process, the first parameter is the command to be executed, the second are the pipe flags and in our case we will write and read in the pipe so we must say what we are doing in the pipe. You may notice the flag ECORE\+\_\+\+EXE\+\_\+\+PIPE\+\_\+\+READ\+\_\+\+LINE\+\_\+\+BUFFERED, this means that reads are buffered until I find a newline. And the third parameter is data that we would like to send to the process in its creating. This case we are sending nothing, so just use NULL.

Then we check if the process was created\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (childHandle\ ==\ NULL)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}Could\ not\ create\ a\ child\ process!\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}};}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 After this we get the PID of the child process and just print it in the screen. The PID stands for Process identification. This is just an internal identifier of your process\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ childPid\ =\ \mbox{\hyperlink{group__Ecore__Exe__Group_ga31fa4d88303d0dc45b3dfdb55fb53883}{ecore\_exe\_pid\_get}}(childHandle);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (childPid\ ==\ -\/1)}
\DoxyCodeLine{\ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}Could\ not\ retrieve\ the\ PID!\(\backslash\)n"{}});}

\end{DoxyCodeInclude}
 The way that Ecore\+\_\+exe works is\+: when we want to read data sent from our child we must use an ecore event. So let\textquotesingle{}s start register our event listener\+: 
\begin{DoxyCodeInclude}{0}

\end{DoxyCodeInclude}
 Now to send messages to our child we will use a timer, so every 1 second we will send a message to the child. 
\begin{DoxyCodeInclude}{0}

\end{DoxyCodeInclude}
 After all this we start the main loop. Now let\textquotesingle{}s pass to the callback functions.

Now we will see how we actually send the data and receive it. Let\textquotesingle{}s start with \+\_\+send\+Message\+:  
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_sendMessage(\textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}\ numberOfMessages\ =\ 0;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{structEcore__Exe}{Ecore\_Exe}}\ *childHandle\ =\ (\mbox{\hyperlink{structEcore__Exe}{Ecore\_Exe}}\ *)data;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ msg[BUFFER\_SIZE];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ sprintf(msg,\ \textcolor{stringliteral}{"{}\ Message:\ \%d\(\backslash\)n"{}},\ numberOfMessages);}
\DoxyCodeLine{\ \ \ numberOfMessages++;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (\mbox{\hyperlink{group__Ecore__Exe__Group_gac5a7d73f64baf6b752154836ff4123b9}{ecore\_exe\_send}}(childHandle,\ msg,\ strlen(msg))\ !=\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}})}
\DoxyCodeLine{\ \ \ \ \ fprintf(stderr,\ \textcolor{stringliteral}{"{}Could\ not\ send\ my\ name\ to\ the\ child\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{\ \ \ \ \ printf(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}I'm\ the\ father\ and\ I\ sent\ this\ message\ to\ the\ child:\ \%s\(\backslash\)n"{}},\ msg);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 We use ecore\+\_\+exe\+\_\+send to send data to the child process, it\textquotesingle{}s pretty simple. To know what the parameters stands for, check the docs.

\begin{DoxyNote}{Note}
The function {\bfseries{ecore\+\_\+exe\+\_\+send}} will never block your program, also there is no partial send of the data. This means either the function will send all the data or it will fail.
\end{DoxyNote}
Now let\textquotesingle{}s take a look in our event callback and see how we retrieve the messages.  
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_msg\_from\_child\_handler(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{int}\ type\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Ecore__Exe__Event__Data}{Ecore\_Exe\_Event\_Data}}\ *dataFromProcess\ =\ (\mbox{\hyperlink{struct__Ecore__Exe__Event__Data}{Ecore\_Exe\_Event\_Data}}\ *)event;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ msg[BUFFER\_SIZE];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (dataFromProcess-\/>\mbox{\hyperlink{struct__Ecore__Exe__Event__Data_a7d28f61d1dc37d2582ed1028c692c46f}{size}}\ >=\ (BUFFER\_SIZE\ -\/\ 1))}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Data\ too\ big\ for\ bugger.\ error\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga780091d7d49a4bb6cb753f12e1a3b19d}{ECORE\_CALLBACK\_DONE}};}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ strncpy(msg,\ dataFromProcess-\/>\mbox{\hyperlink{struct__Ecore__Exe__Event__Data_a5af41c740afab1ba763ba2dc14807802}{data}},\ dataFromProcess-\/>\mbox{\hyperlink{struct__Ecore__Exe__Event__Data_a7d28f61d1dc37d2582ed1028c692c46f}{size}});}
\DoxyCodeLine{\ \ \ msg[dataFromProcess-\/>\mbox{\hyperlink{struct__Ecore__Exe__Event__Data_a7d28f61d1dc37d2582ed1028c692c46f}{size}}]\ =\ 0;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (strcmp(msg,\ \textcolor{stringliteral}{"{}quit"{}})\ ==\ 0)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}My\ child\ said\ to\ me,\ QUIT!\(\backslash\)n"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 It\textquotesingle{}s just like an normal event, we get a reference to Ecore\+\_\+\+Exe\+\_\+\+Event\+\_\+\+Data, extract the data and then show it in the screen.

And that\textquotesingle{}s it, after all it\textquotesingle{}s not complicated to create a process and communicate with it. 