Rev 8155 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
17 | |||
18 | /* fork management */ |
||
19 | |||
20 | #define MAX_FORK 1024 |
||
21 | #define MAX_DELAY 1500 /* At most these seconds of execution */ |
||
22 | |||
23 | struct { |
||
24 | pid_t pid; |
||
25 | time_t t; |
||
26 | int type; |
||
27 | int pad; |
||
28 | } forklist[MAX_FORK]; |
||
29 | int forkcnt; |
||
30 | |||
31 | void addfork(pid_t pid, int type) |
||
32 | { |
||
33 | if(forkcnt>=MAX_FORK) { |
||
34 | kill(pid,SIGKILL); return; |
||
35 | } |
||
36 | forklist[forkcnt].pid=pid; |
||
37 | forklist[forkcnt].t=time(NULL); |
||
38 | forklist[forkcnt].type=type; |
||
39 | forkcnt++; |
||
40 | } |
||
41 | |||
42 | /* forklist management */ |
||
43 | void forkman(int kz) |
||
44 | { |
||
45 | int delay, i, t, st; |
||
46 | time_t now; |
||
47 | delay=MAX_DELAY; |
||
48 | if(forkcnt>=MAX_FORK/2) delay=delay/5; |
||
49 | if(forkcnt*4>=MAX_FORK*3) delay=delay/4; |
||
50 | now=time(NULL); |
||
51 | for(i=forkcnt-1; i>=0; i--) { |
||
52 | if(now-forklist[i].t>delay) kill(forklist[i].pid,SIGKILL); |
||
53 | t=waitpid(forklist[i].pid,&st,WNOHANG); |
||
54 | if(t==0 || !WIFEXITED(st)) continue; |
||
55 | memmove(forklist+i,forklist+i+1,forkcnt-1-i); |
||
56 | forkcnt--; |
||
57 | } |
||
58 | if(kz) waitpid(-1,NULL,WNOHANG); /* kill zombies */ |
||
59 | } |
||
60 | |||
61 | void wait_children(void) |
||
62 | { |
||
63 | time_t now; |
||
64 | int i; |
||
65 | do { |
||
66 | now=time(NULL); |
||
67 | for(i=0; i<forkcnt; i++) { |
||
68 | if(forklist[i].type && forklist[i].t < now-MAX_DELAY) break; |
||
69 | } |
||
70 | if(i>=forkcnt) return; |
||
71 | sleep(1); |
||
72 | } |
||
73 | while(1); |
||
74 | } |
||
75 |