current version
[opsoft_test.git] / gclib2 / modules / Core / djobs.cxx
blob5c3fcd03eb3a38aae478cfc2490cf8d7d8cfc96c
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include "gclib2.h"
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <errno.h>
12 DJobs::DJobs ()
17 DJobs::~DJobs ()
22 /// Добавить процесс (на контроль).
23 __djob_t * DJobs::add (int pid, char * user_data)
25 __djob_t * j;
26 j = alloc_job ();
27 j->pid = pid;
28 j->user_data = user_data;
29 add_tail (LPCHAR(j));
30 return j;
33 __djob_t * DJobs::attach (__djob_t * j)
35 if (! j)
36 return NULL;
38 return (__djob_t *) add_tail (LPCHAR(j));
41 __djob_t * DJobs::zombie ()
43 pid_t pid;
44 int status;
46 if (! first ())
47 return NULL;
49 pid = waitpid (-1, &status, WNOHANG);
50 if (pid <= 0)
51 return NULL;
53 return __do_zombie (pid, status);
56 __djob_t * DJobs::__do_zombie (pid_t pid, int exit_status)
58 __djob_t * j = by_pid (pid);
59 if (j) {
60 rm ();
61 j->exit_status = exit_status;
63 return j;
66 __djob_t * DJobs::wait_all ()
68 __djob_t * j;
69 int status;
70 pid_t pid;
72 while (first ()) {
73 pid = waitpid (-1, &status, 0);
74 if (pid <= 0)
75 break;
76 j = __do_zombie (pid, status);
77 return j;
80 return NULL;
83 __djob_t * DJobs::alloc_job ()
85 __djob_t * j;
86 j = CNEW (__djob_t, 1);
87 Dexec_init (j);
88 return j;
91 __djob_t * DJobs::by_pid (pid_t pid)
93 __djob_t * j;
94 first ();
95 while ((j = (__djob_t *) get ()) && j) {
96 if (j->pid == pid)
97 return j;
98 next ();
100 return NULL;
103 __djob_t * DJobs::fork ()
105 pid_t pid;
106 __djob_t *j;
108 pid = ::fork ();
109 if (pid < 0)
110 return NULL;
112 j = alloc_job ();
113 Dexec_init (j);
114 if (pid > 0)
115 j->pid = pid;
116 else {
117 j->pid = getpid ();
118 j->child = 1;
121 attach (j);
122 return j;