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:

#include <unistd.h>
#include <Ecore.h>

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:

static void
do_lengthy_task(Ecore_Pipe *pipe)
{
int i, j;
char *buffer;
for (i = 0; i < 20; i++)
{
sleep(1);
buffer = malloc(sizeof(char) * i);
for (j = 0; j < i; j++)
buffer[j] = 'a' + j;
ecore_pipe_write(pipe, buffer, i);
free(buffer);
}
ecore_pipe_write(pipe, "close", 5);
}
Note
The sleep was added so the parent process would think the child process was doing something interesting...

Next up is our function for handling data arriving in the pipe. It copies the data to another buffer, adds a terminating NULL and prints it. Also if it receives a certain string it stops the main loop(effectively ending the program):

static void
handler(void *data EINA_UNUSED, void *buf, unsigned int len)
{
char *str = malloc(sizeof(char) * len + 1);
memcpy(str, buf, len);
str[len] = '\0';
printf("received %u bytes\n", len);
printf("content: %s\n", (const char *)str);
free(str);
if (len && !strncmp(buf, "close", len < 5 ? len : 5))
{
printf("close requested\n");
}
}

And now on to our main function, we start by declaring some variables and initializing ecore:

int
main(void)
{
Ecore_Pipe *pipe;
pid_t child_pid;

And since we are talking about pipes let's create one:

pipe = ecore_pipe_add(handler, NULL);

Now we are going to fork:

child_pid = fork();
Note
duh...

The child process is going to do the our fancy processing:

if (!child_pid)
{
do_lengthy_task(pipe);
}
Note
It's very important to call ecore_pipe_read_close() here so that the child process won't read what it is writing to the pipe itself.

And the parent is going to run ecore's main loop waiting for some data:

Note
Calling ecore_pipe_write_close() here isn't important but since we aren't going to write in the pipe it is good practice.

And finally when done processing(the child) or done receiving(the parent) we delete the pipe and shutdown ecore:

return 0;
}