source: branches/FACT++_part_filenames/dim/src/examples/dim_fork.cxx

Last change on this file was 11071, checked in by tbretz, 14 years ago
Replaced v19r21 by a version extracted with 'unzip -a' to get proper unix text format.
File size: 2.0 KB
Line 
1#include <cerrno>
2#include <cstdio>
3#include <unistd.h>
4#include <sys/wait.h>
5extern "C" {
6#include "dis.hxx"
7}
8
9static int id, id_cmd, run = 0;
10static int doForkNow = 0;
11
12void handle_cmd(void *tag, void *data, int *size)
13{
14 doForkNow = 1;
15}
16
17static pid_t fork_em() {
18 // ::dis_remove_service(id);
19 ::dis_stop_serving();
20 ::printf("Sleep a bit to see task disappear in did\n");
21 //::sleep(3);
22 pid_t pid = ::fork();
23 if ( pid == 0 ) {
24 ::dim_init();
25 ::printf("PID:%d Register Child service Child/run to DNS....\n",::getpid());
26 id = ::dis_add_service((char*)"Child/run",(char*)"I",&run,sizeof(run),0,0);
27 ::dis_start_serving((char*)"Child");
28 }
29 else if ( pid > 0 ) {
30 ::dim_init();
31 //::sleep(3);
32 ::printf("PID:%d RE-Register Parent to DNS....\n",::getpid());
33 id = ::dis_add_service((char*)"Parent/run",(char*)"I",&run,sizeof(run),0,0);
34 // id_cmd = :: dis_add_cmnd((char *)"Parent/cmd",(char*)"C",handle_cmd,0);
35 ::dis_start_serving((char*)"Parent");
36 }
37 else {
38 ::printf("ERROR in fork!!!! %s\n",strerror(errno));
39 ::exit(0);
40 }
41 return pid;
42}
43
44int main(int /* argc */, char** /* argv */) {
45 id = ::dis_add_service((char*)"Parent/run",(char*)"I",&run,sizeof(run),0,0);
46 // id_cmd = :: dis_add_cmnd((char *)"Parent/cmd",(char*)"C",handle_cmd,0);
47 ::dis_start_serving((char*)"Parent");
48 ::sleep(5);
49 pid_t child = 0;
50
51 Again:
52 child = fork_em();
53 if ( child > 0 ) {
54 int status;
55 sleep(5);
56 kill(child,SIGTERM);
57 wait(&status);
58 sleep(1);
59 goto Again;
60 }
61
62 while(1)
63 {
64 /*
65 if(doForkNow)
66 {
67 int status;
68 doForkNow = 0;
69 if(child > 0)
70 {
71 kill(child,SIGTERM);
72 wait(&status);
73 sleep(1);
74 }
75 child = fork_em();
76 }
77 */
78 ::sleep(1);
79 }
80 return 1;
81}
82
83
84/*
85
86g++ -o dim_fork -I../../DIM/dim -ldim -lrt -pthread ../test/dim_fork.cpp
87
88[frankm@plus04 cmt]$ ./dim_fork
89PID:20407 Register Child to DNS....
90PID 20407 - Fri Dec 3 22:01:57 2010 - (FATAL) Child: Some Services already known to DNS
91PID:20404 RE-Register Parent to DNS....
92
93*/
Note: See TracBrowser for help on using the repository browser.