ContainerTracer  0.1
sync.h
Go to the documentation of this file.
1 
28 #ifndef SYNC_H
29 #define SYNC_H
30 
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <errno.h>
34 
35 /* 0: read 1: write*/
36 static int pfd1[2], pfd2[2];
37 
43 static inline int TELL_WAIT(void)
44 {
45  if (pipe(pfd1) < 0 || pipe(pfd2) < 0) {
46  fprintf(stderr, "[%s] pipe error...\n", __func__);
47  return -EPIPE;
48  }
49  return 0;
50 }
51 
58 static inline int TELL_PARENT(void)
59 {
60  if (write(pfd2[1], "c", 1) != 1) {
61  fprintf(stderr, "[%s] write error...\n", __func__);
62  return -EIO;
63  }
64  return 0;
65 }
66 
72 static inline int WAIT_PARENT(void)
73 {
74  char recv;
75 
76  if (read(pfd1[0], &recv, 1) != 1) {
77  fprintf(stderr, "[%s] read error...\n", __func__);
78  return -EIO;
79  }
80 
81  if (recv != 'p') {
82  fprintf(stderr, "[%s] incorrect data received(%c)\n", __func__,
83  recv);
84  return -EIO;
85  }
86  return 0;
87 }
88 
95 static inline int TELL_CHILD(void)
96 {
97  if (write(pfd1[1], "p", 1) != 1) {
98  fprintf(stderr, "[%s] write error...\n", __func__);
99  return -EIO;
100  }
101  return 0;
102 }
103 
109 static inline int WAIT_CHILD(void)
110 {
111  char recv;
112 
113  if (read(pfd2[0], &recv, 1) != 1) {
114  fprintf(stderr, "[%s] read error...\n", __func__);
115  return -EIO;
116  }
117 
118  if (recv != 'c') {
119  fprintf(stderr, "[%s] incorrect data received(%c)\n", __func__,
120  recv);
121  return -EIO;
122  }
123  return 0;
124 }
125 
126 #endif
static int TELL_WAIT(void)
Pipe initialize sequence. You must call before use this source.
Definition: sync.h:43
static int TELL_PARENT(void)
The child calls parents.
Definition: sync.h:58
static int pfd1[2]
Definition: sync.h:36
static int WAIT_PARENT(void)
Receive from parent&#39;s call.
Definition: sync.h:72
static int pfd2[2]
Definition: sync.h:36
static int WAIT_CHILD(void)
Receive from child&#39;s call.
Definition: sync.h:109
static int TELL_CHILD(void)
The parent calls children.
Definition: sync.h:95