source: trunk/FACT++/dim/src/examples/dim_fork2.cxx@ 16336

Last change on this file since 16336 was 10614, checked in by tbretz, 14 years ago
New release V19 r20
File size: 2.7 KB
Line 
1#include <cerrno>
2#include <cstdio>
3#include <unistd.h>
4extern "C" {
5#include "dis.hxx"
6}
7
8struct 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
23struct 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;
35 ::dis_update_service(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);
49 ::dis_stop_serving();
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);
61 ::dis_start_serving(nam);
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
81int 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
99g++ -o xxxx -I../../DIM/dim -ldim -lrt -pthread ../test/test_dim.cpp
100
101[frankm@plus04 cmt]$ ./xxxx
102PID:20407 Register Child to DNS....
103PID 20407 - Fri Dec 3 22:01:57 2010 - (FATAL) Child: Some Services already known to DNS
104PID:20404 RE-Register Parent to DNS....
105
106*/
Note: See TracBrowser for help on using the repository browser.