FACT++  1.0
dim_fork2.cxx
Go to the documentation of this file.
1 #include <cerrno>
2 #include <cstdio>
3 #include <unistd.h>
4 extern "C" {
5 #include "dis.hxx"
6 }
7 
8 struct Child {
9  int id, run, calls;
10  char name[132];
11  Child(const char* n, const char* svc) : id(0), run(0), calls(0) {
12  ::strncpy(name,n,sizeof(name));
13  id = ::dic_info_service((char*)svc,MONITORED,0,0,0,callback,(long)this,&run,sizeof(run));
14  }
15  virtual ~Child() {}
16  static void callback(void* tag, void* buffer, int* /* size */) {
17  Child* c = *(Child**)tag;
18  c->run = *(int*)buffer;
19  c->calls++;
20  }
21 };
22 
23 struct Parent {
24  int id, run, is_parent;
25  Child* children[50];
26  Parent() : id(0), run(0) {
27  is_parent = 0;
28  memset(children,0,sizeof(children));
29  id = ::dis_add_service((char*)"Parent/run",(char*)"I",&run,sizeof(run),0,0);
30  ::dis_start_serving((char*)"Parent");
31  }
32  virtual ~Parent() {}
33  void update() {
34  ++run;
36  }
37  void print() {
38  /*
39  size_t cnt = 0;
40  for(size_t i=0; i<sizeof(children)/sizeof(children[0]); ++i) {
41  if ( children[i]->calls > 2 ) cnt++;
42  }
43  ::printf("PID:%d Child service answers from %ld children. %ld still missing.\n",
44  ::getpid(), cnt, sizeof(children)/sizeof(children[0])-cnt);
45  */
46  }
47  void fork_em() {
48  //::dis_remove_service(id);
50  ::printf("Sleep a bit to see task disappear in did\n");
51  ::sleep(3);
52  for(size_t i=0; i<sizeof(children)/sizeof(children[0]); ++i) {
53  char nam[132], svc[132];
54  pid_t pid = ::fork();
55  sprintf(nam,"Child_%02ld",i);
56  sprintf(svc,"Child_%02ld/run",i);
57  if ( pid == 0 ) {
58  ::dim_init();
59  ::printf("PID:%d Register Child service Child_%02d/run to DNS....\n",::getpid(),i);
60  id = ::dis_add_service(svc,(char*)"I",&run,sizeof(run),0,0);
62  return;
63  }
64  else if ( pid > 0 ) {
65  //children[i] = new Child(nam, svc);
66  is_parent = true;
67  }
68  else {
69  ::printf("ERROR in fork!!!! %s\n",strerror(errno));
70  ::exit(0);
71  }
72  }
73  ::dim_init();
74  ::sleep(3);
75  ::printf("PID:%d RE-Register Parent to DNS....\n",::getpid());
76  id = ::dis_add_service((char*)"Parent/run",(char*)"I",&run,sizeof(run),0,0);
77  ::dis_start_serving((char*)"Parent");
78  }
79 };
80 
81 int main(int /* argc */, char** /* argv */) {
82  Parent p;
83  ::sleep(5);
84  p.fork_em();
85  while(1) {
86  p.update();
87  ::sleep(1);
88  if ( p.is_parent ) {
89  p.print();
90  }
91  }
92  ::printf("Process %d exiting\n",::getpid());
93  return 1;
94 }
95 
96 
97 /*
98 
99 g++ -o xxxx -I../../DIM/dim -ldim -lrt -pthread ../test/test_dim.cpp
100 
101 [frankm@plus04 cmt]$ ./xxxx
102 PID:20407 Register Child to DNS....
103 PID 20407 - Fri Dec 3 22:01:57 2010 - (FATAL) Child: Some Services already known to DNS
104 PID:20404 RE-Register Parent to DNS....
105 
106 */
int is_parent
Definition: dim_fork2.cxx:24
unsigned dic_info_service(char *serv_name, int req_type, int req_timeout, void *serv_address, int serv_size, void(*usr_routine)(), dim_long tag, void *fill_addr, int fill_size)
Definition: dic.c:601
int i
Definition: db_dim_client.c:21
void update()
Definition: dim_fork2.cxx:33
#define MONITORED
Definition: dim_common.h:9
int main(int, char **)
Definition: dim_fork2.cxx:81
int run
Definition: dim_fork2.cxx:9
int run
Definition: dim_fork2.cxx:24
Child(const char *n, const char *svc)
Definition: dim_fork2.cxx:11
virtual ~Child()
Definition: dim_fork2.cxx:15
int calls
Definition: dim_fork2.cxx:9
void dim_init()
Definition: dim_thr.c:111
#define dis_start_serving
Definition: dis.h:8
#define dis_update_service
Definition: dis.h:18
static void callback(void *tag, void *buffer, int *)
Definition: dim_fork2.cxx:16
#define dis_stop_serving
Definition: dis.h:9
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
int id
Definition: dim_fork2.cxx:9
virtual ~Parent()
Definition: dim_fork2.cxx:32
char name[132]
Definition: dim_fork2.cxx:10
#define dis_add_service
Definition: dis.h:12
Parent()
Definition: dim_fork2.cxx:26
void fork_em()
Definition: dim_fork2.cxx:47
void print()
Definition: dim_fork2.cxx:37
sprintf(name1,"NewService%d", i)