File: fork_test.cpp

package info (click to toggle)
libassa 3.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,084 kB
  • ctags: 2,323
  • sloc: cpp: 15,641; sh: 8,704; makefile: 372; perl: 51
file content (165 lines) | stat: -rw-r--r-- 4,440 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// -*- c++ -*-
//------------------------------------------------------------------------------
//                           fork_test.cpp
//------------------------------------------------------------------------------
// $Id: fork_test.cpp,v 1.5 2006/07/20 02:30:55 vlg Exp $
//------------------------------------------------------------------------------
//  Copyright (c) 2002,2005 by Vladislav Grinchenko
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version
//  2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------

/*------------------------------------------------------------------------------
This is what happens in the test:

              main
                |
              fork
               / \
            fork  A (WAIT) for (5 times) { sleep(1); print msg }
            / \    
           /   \     
          /     \    
         /       \
        /         B (KILL)
      fork
      / \
     /   C (WAIT) for (5 times) { sleep(2); print msg }
    /     
  done     
           

Child B is killed immediately after main() exits. SIGTERM is 
sent to child A and C and parent process waits for them to finish.
First finishes A, and then C.
Process D is not in the picuture because its state flag is LEAVE_ALONE.
init will become its parent and it shall be killed separately.
------------------------------------------------------------------------*/

#include <iostream>
#include <fstream>
using namespace std;

#include "assa/Logger.h"
#include "assa/Fork.h"
using namespace ASSA;

int
main (int argc, char** argv)
{
	std::cout << "= Running fork_test Test =" << std::endl;

	::unlink ("fork_A.log");
	::unlink ("fork_B.log");
	::unlink ("fork_C.log");
	::unlink ("fork_D.log");
	::unlink ("fork_test.log");

#if defined (WIN32)

	std::cout 
		<< "= Running fork_test Test =\n"
		<< "Spanning 3 child processes: A, B, and C\n"
		<< "First B shall be terminated, then wait for A and C to finish.\n"
		<< "Parent: forking A ...\n"
		<< "Parent: forking B ...\n"
		<< "Parent: forking C ...\n"
		<< "Parent: forking D ...\n";

#else

	// Log::open_log_file ("fork_test.log", APP);
	Log::open_log_stdout (APP);

	// Just test that it works at all

	DL((APP,"Spanning 3 child processes: A, B, and C\n"));
	DL((APP,"First B shall be terminated, then wait for A and "
		"C to finish.\n"));

	DL((APP,"Parent: forking A ...\n"));
	LOGGER->log_resync ();

	Fork a (Fork::WAIT_ON_EXIT, Fork::IGNORE_STATUS);

	if (a.isParent()) {
		DL((APP,"Parent: forking B ...\n"));
		LOGGER->log_resync ();

		Fork b (Fork::KILL_ON_EXIT, Fork::IGNORE_STATUS);

		if (b.isParent()) {
			DL((APP,"Parent: forking C ...\n"));
			LOGGER->log_resync ();

			Fork c (Fork::WAIT_ON_EXIT, Fork::IGNORE_STATUS);

			if (c.isParent()) {
				DL((APP,"Parent: forking D ...\n"));
				LOGGER->log_resync ();

				Fork d (Fork::LEAVE_ALONE, Fork::IGNORE_STATUS);

				if ( d.isParent() ) {
					//zombie = d.getChildPID ();
				}
				else { // Child D
					Log::log_close ();
					Log::open_log_file ("fork_D.log", APP);
					DL((APP,"In child D ... sleeping 20 seconds.\n"));
					sleep (20);
				}
				/* Exiting will trigger ForkList destructor
				 * that suppose to collect exiting status of children
				 */
			}
			else if (c.isChild()) {
				Log::log_close ();
				Log::open_log_file ("fork_C.log", APP);

				DL((APP,"In child C ..."
					" print 5 msgs with 2 sec interval.\n"));

				for (int i=0; i<5; i++) {
					sleep(2);
					DL((APP,"[%1d]  child < C > is here\n", i+1));
				}
				DL((APP,"child C exits!\n"));
				return 0;
			}
		}
		else if (b.isChild()) {
			Log::log_close ();
			Log::open_log_file ("fork_B.log", APP);

			DL((APP,"In child B - you should see it just once.\n"));
			sleep(12);
			/*
			 * you normally won't see this message becuase
			 * parent suppose to terminate this child with SIGTERM
			 */
			DL((APP,"child B exits!\n"));
			return 0;
		}
	}
	else if (a.isChild()) {
		Log::log_close ();
		Log::open_log_file ("fork_A.log", APP);
		DL((APP,"In child A ... print 5 msgs with 1 sec interval.\n"));

		for (int j=0; j<5; j++) {
			sleep(1);
			DL((APP,"[%d] child < A > is here\n", j+1));
		}
		DL((APP, "child A exits!\n"));
		return 0;
	}
#endif	// !ifdef WIN32

	return 0;
}