summaryrefslogtreecommitdiffstats
path: root/waitlib/confwait.c
blob: 55127b3ac4273a12d42cc4d30400aeb659ac9202 (plain)
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
/*
** Copyright 1998 - 1999 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif

#include	<sys/types.h>

#if	HAVE_SYS_WAIT_H
#include	<sys/wait.h>
#endif
#include	<stdio.h>
#include	<signal.h>
#include	<stdlib.h>

#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<string.h>
#include	<time.h>

#define	INCLUDED_FROM_CONFIGURE
#include	"waitlib.c"

#define	NUMPROCS	10

static int numterminated=0;


static void cntreaped(pid_t p, int n)
{
	if ( ++numterminated == NUMPROCS )	_exit(0);
}

static void childsig(int n)
{
	n=n;

	wait_reap(cntreaped, childsig);
}

int main()
{
int	pipefd[2];
int	pipefd2[2];
pid_t	p;
int	i;
time_t	t;
char	c;

	if (pipe(pipefd) || pipe(pipefd2))
	{
		perror("pipe");
		exit(1);
	}

	signal(SIGCHLD, childsig);

	for (i=0; i<NUMPROCS; i++)
	{
		while ((p=fork()) == -1)
		{
			perror("fork");
			sleep(5);
		}

		if (p == 0)
		{
			close(pipefd[1]);
			close(pipefd2[0]);
			close(pipefd2[1]);
			read(pipefd[0], &c, 1);
			_exit(0);
		}
	}
	close(pipefd2[1]);
	close(pipefd[0]);
	if (read(pipefd2[0], &c, 1) != 0)
	{
		perror("read");
	}

	wait_block();

	close(pipefd2[0]);
	close(pipefd[1]);
	sleep(3);
	time(&t);
	wait_clear(&childsig);

	do
	{
		sleep(3);
	} while ( time(0) < t+3);
	if (numterminated == NUMPROCS)
		exit(0);
	exit(1);
	return (0);
}