\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 \mbox{\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} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_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)
\{
   \mbox{\hyperlink{struct__Ecore__Exe__Event__Data}{Ecore\_Exe\_Event\_Data}} *dataFromProcess = (
      \mbox{\hyperlink{struct__Ecore__Exe__Event__Data}{Ecore\_Exe\_Event\_Data}} *)event;
   \textcolor{keywordtype}{char} msg[BUFFER\_SIZE];

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

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

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

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

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

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

   \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}})
     fprintf(stderr, \textcolor{stringliteral}{"Could not send my name to the child\(\backslash\)n"});
   \textcolor{keywordflow}{else}
     printf(
             \textcolor{stringliteral}{"I'm the father and I sent this message to the child: %s\(\backslash\)n"}, msg);

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

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

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

   childHandle = \mbox{\hyperlink{group__Ecore__Exe__Group_gacd208a964c62424424d4c4c92a651db1}{ecore\_exe\_pipe\_run}}(\textcolor{stringliteral}{"./ecore\_exe\_example\_child"},
                                    \mbox{\hyperlink{group__Ecore_gga26f57bfa7ecfa8eee5fc053e342880c5a83dcb150caa9f5f4ce548eaa4a92c6af}{ECORE\_EXE\_PIPE\_WRITE}} |
                                    \mbox{\hyperlink{group__Ecore_gga26f57bfa7ecfa8eee5fc053e342880c5ae97566413d58b792e92369966ffeece5}{ECORE\_EXE\_PIPE\_READ\_LINE\_BUFFERED}} |
                                    \mbox{\hyperlink{group__Ecore_gga26f57bfa7ecfa8eee5fc053e342880c5a76bc9e447a456727dc2d686b4682b512}{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} \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}};
     \}

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

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

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

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

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

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

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

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

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

\end{DoxyCodeInclude}
 