\hypertarget{ecore_exe_example_8c-example}{\section{ecore\+\_\+exe\+\_\+example.\+c}
}
This is a process that will send messages to a child and it will stop when it receives \char`\"{}quit\char`\"{}.Check the \hyperlink{Ecore_exe_simple_example_c}{Full tutorial}


\begin{DoxyCodeInclude}

\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <string.h>}
\textcolor{preprocessor}{#include <Ecore.h>}

\textcolor{preprocessor}{#define BUFFER\_SIZE 1024}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_msg\_from\_child\_handler(\textcolor{keywordtype}{void} *data \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keywordtype}{int} type EINA\_UNUSED, \textcolor{keywordtype}{void} *event)
\{
   \hyperlink{struct__Ecore__Exe__Event__Data}{Ecore\_Exe\_Event\_Data} *dataFromProcess = (
      \hyperlink{struct__Ecore__Exe__Event__Data}{Ecore\_Exe\_Event\_Data} *)event;
   \textcolor{keywordtype}{char} msg[BUFFER\_SIZE];

   \textcolor{keywordflow}{if} (dataFromProcess->\hyperlink{struct__Ecore__Exe__Event__Data_a7d28f61d1dc37d2582ed1028c692c46f}{size} >= (BUFFER\_SIZE - 1))
     \{
        fprintf(stdout, \textcolor{stringliteral}{"Data too big for bugger. error\(\backslash\)n"});
        \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga780091d7d49a4bb6cb753f12e1a3b19d}{ECORE\_CALLBACK\_DONE};
     \}

   strncpy(msg, dataFromProcess->\hyperlink{struct__Ecore__Exe__Event__Data_a81848de0f19ad311c27b9e5d480d645f}{data}, dataFromProcess->\hyperlink{struct__Ecore__Exe__Event__Data_a7d28f61d1dc37d2582ed1028c692c46f}{size});
   msg[dataFromProcess->\hyperlink{struct__Ecore__Exe__Event__Data_a7d28f61d1dc37d2582ed1028c692c46f}{size}] = 0;

   \textcolor{keywordflow}{if} (strcmp(msg, \textcolor{stringliteral}{"quit"}) == 0)
     \{
        fprintf(stdout, \textcolor{stringliteral}{"My child said to me, QUIT!\(\backslash\)n"});
        \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}();
     \}
   \textcolor{keywordflow}{else}
     fprintf(stdout, \textcolor{stringliteral}{"I received a message from my child: %s\(\backslash\)n"}, msg);

   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga780091d7d49a4bb6cb753f12e1a3b19d}{ECORE\_CALLBACK\_DONE};
\}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_sendMessage(\textcolor{keywordtype}{void} *data)
\{
   \textcolor{keyword}{static} \textcolor{keywordtype}{int} numberOfMessages = 0;
   \hyperlink{group__Ecore__Exe__Group_ga4f1476f2054418e6436880883e04a141}{Ecore\_Exe} *childHandle = (\hyperlink{group__Ecore__Exe__Group_ga4f1476f2054418e6436880883e04a141}{Ecore\_Exe} *)data;
   \textcolor{keywordtype}{char} msg[BUFFER\_SIZE];

   sprintf(msg, \textcolor{stringliteral}{" Message: %d\(\backslash\)n"}, numberOfMessages);
   numberOfMessages++;

   \textcolor{keywordflow}{if} (\hyperlink{group__Ecore__Exe__Group_gac5a7d73f64baf6b752154836ff4123b9}{ecore\_exe\_send}(childHandle, msg, strlen(msg)) != \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE})
     fprintf(stderr, \textcolor{stringliteral}{"Could not send my name to the child\(\backslash\)n"});
   \textcolor{keywordflow}{else}
     fprintf(stdout,
             \textcolor{stringliteral}{"I'm the father and I sent this message to the child: %s\(\backslash\)n"}, msg);

   \textcolor{keywordflow}{return} \hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW};
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{void})
\{
   pid\_t childPid;
   \hyperlink{group__Ecore__Exe__Group_ga4f1476f2054418e6436880883e04a141}{Ecore\_Exe} *childHandle;

   \textcolor{keywordflow}{if} (!\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}())
     \textcolor{keywordflow}{goto} exit;

   childHandle = \hyperlink{group__Ecore__Exe__Group_gacd208a964c62424424d4c4c92a651db1}{ecore\_exe\_pipe\_run}(\textcolor{stringliteral}{"./ecore\_exe\_example\_child"},
                                    \hyperlink{group__Ecore__Exe__Group_gga6fb12d2a7c21ff1340c0f69447f91599a83dcb150caa9f5f4ce548eaa4a92c6af}{ECORE\_EXE\_PIPE\_WRITE} |
                                    \hyperlink{group__Ecore__Exe__Group_gga6fb12d2a7c21ff1340c0f69447f91599ae97566413d58b792e92369966ffeece5}{ECORE\_EXE\_PIPE\_READ\_LINE\_BUFFERED} |
                                    \hyperlink{group__Ecore__Exe__Group_gga6fb12d2a7c21ff1340c0f69447f91599a76bc9e447a456727dc2d686b4682b512}{ECORE\_EXE\_PIPE\_READ}, NULL);

   \textcolor{keywordflow}{if} (childHandle == NULL)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"Could not create a child process!\(\backslash\)n"});
        \textcolor{keywordflow}{goto} \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown};
     \}

   childPid = \hyperlink{group__Ecore__Exe__Group_ga31fa4d88303d0dc45b3dfdb55fb53883}{ecore\_exe\_pid\_get}(childHandle);

   \textcolor{keywordflow}{if} (childPid == -1)
     fprintf(stderr, \textcolor{stringliteral}{"Could not retrive the PID!\(\backslash\)n"});
   \textcolor{keywordflow}{else}
     fprintf(stdout, \textcolor{stringliteral}{"The child process has PID:%u\(\backslash\)n"}, (\textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int})childPid);

   \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\hyperlink{group__Ecore__Exe__Group_ga3a5071bbc714abfbcede55c39c6ecf8a}{ECORE\_EXE\_EVENT\_DATA}, 
      \_msg\_from\_child\_handler, NULL);
   ecore\_timer\_add(1, \_sendMessage, childHandle);

   \hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}();

   \hyperlink{group__Ecore__Exe__Group_gab0948b947e85cf65c7a4e048705dcab8}{ecore\_exe\_free}(childHandle); \textcolor{comment}{//This will not affect the child process}

   \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();

   \textcolor{keywordflow}{return} EXIT\_SUCCESS;

\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}:
   \hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}();

exit:
   \textcolor{keywordflow}{return} EXIT\_FAILURE;
\}

\end{DoxyCodeInclude}
 