efl

Форк
0
/
ecore_exe_example.c 
99 строк · 2.4 Кб
1
/**
2
   Compile with gcc -o ecore_exe_example ecore_exe_example.c `pkg-config --cflags --libs ecore`
3
 */
4

5
#include <stdio.h>
6
#include <string.h>
7
#include <Ecore.h>
8

9
#define BUFFER_SIZE 1024
10

11
static Eina_Bool
12
_msg_from_child_handler(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
13
{
14
   Ecore_Exe_Event_Data *dataFromProcess = (Ecore_Exe_Event_Data *)event;
15
   char msg[BUFFER_SIZE];
16

17
   if (dataFromProcess->size >= (BUFFER_SIZE - 1))
18
     {
19
        printf("Data too big for bugger. error\n");
20
        return ECORE_CALLBACK_DONE;
21
     }
22

23
   strncpy(msg, dataFromProcess->data, dataFromProcess->size);
24
   msg[dataFromProcess->size] = 0;
25

26
   if (strcmp(msg, "quit") == 0)
27
     {
28
        printf("My child said to me, QUIT!\n");
29
        ecore_main_loop_quit();
30
     }
31
   else
32
     printf("I received a message from my child: %s\n", msg);
33

34
   return ECORE_CALLBACK_DONE;
35
}
36

37
static Eina_Bool
38
_sendMessage(void *data)
39
{
40
   static int numberOfMessages = 0;
41
   Ecore_Exe *childHandle = (Ecore_Exe *)data;
42
   char msg[BUFFER_SIZE];
43

44
   sprintf(msg, " Message: %d\n", numberOfMessages);
45
   numberOfMessages++;
46

47
   if (ecore_exe_send(childHandle, msg, strlen(msg)) != EINA_TRUE)
48
     fprintf(stderr, "Could not send my name to the child\n");
49
   else
50
     printf(
51
             "I'm the father and I sent this message to the child: %s\n", msg);
52

53
   return ECORE_CALLBACK_RENEW;
54
}
55

56
int
57
main(void)
58
{
59
   pid_t childPid;
60
   Ecore_Exe *childHandle;
61

62
   if (!ecore_init())
63
     goto exit;
64

65
   childHandle = ecore_exe_pipe_run("./ecore_exe_example_child",
66
                                    ECORE_EXE_PIPE_WRITE |
67
                                    ECORE_EXE_PIPE_READ_LINE_BUFFERED |
68
                                    ECORE_EXE_PIPE_READ, NULL);
69

70
   if (childHandle == NULL)
71
     {
72
        fprintf(stderr, "Could not create a child process!\n");
73
        goto ecore_shutdown;
74
     }
75

76
   childPid = ecore_exe_pid_get(childHandle);
77

78
   if (childPid == -1)
79
     fprintf(stderr, "Could not retrieve the PID!\n");
80
   else
81
     printf("The child process has PID:%u\n", (unsigned int)childPid);
82

83
   ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_handler, NULL);
84
   ecore_timer_add(1, _sendMessage, childHandle);
85

86
   ecore_main_loop_begin();
87

88
   ecore_exe_free(childHandle); //This will not affect the child process
89

90
   ecore_shutdown();
91

92
   return EXIT_SUCCESS;
93

94
ecore_shutdown:
95
   ecore_shutdown();
96

97
exit:
98
   return EXIT_FAILURE;
99
}
100

101

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.