#include <Process.h>
Inheritance diagram for OS::Process:
Public Member Functions | |
Process (Callback< void > *receiver, Array *args=nil) | |
virtual | ~Process (void) |
Destructor. | |
virtual void | arguments (Array *args) |
virtual void | receiver (Callback< void > *obj) |
virtual int | status (void) |
virtual void | resume (void) |
virtual void | run (void) |
virtual void | finalize (void) |
virtual void | suspend (void) |
virtual void | terminate (void) |
virtual void | yield (void) |
virtual bool | isRunning (void) |
virtual void | childSignaled (void) |
Static Public Member Functions | |
static Process * | fork (Callback< void > *receiver) |
static Process * | fork (Callback< void > *receiver, Array *args) |
template<class TReceiver> | |
static Process * | fork (TReceiver *receiver, void(TReceiver::*execFunc)(void)) |
template<class TReceiver> | |
static Process * | fork (TReceiver *receiver, void(TReceiver::*execFunc)(Array *), Array *args) |
static Process * | forkJob (String *progName, SequenceableCollection *args=nil, SequenceableCollection *fds=nil) |
Starts program "progName" in new process. | |
static void | finalizeProcesses (void) |
Protected Member Functions | |
Process (void) | |
Protected Attributes | |
Callback< void > * | _receiver |
Array * | _arguments |
int | process_id |
int | _status |
bool | runningProcess |
Static Protected Attributes | |
static Set | processes |
Private Member Functions | |
void | privateExecuteJob (Array *args) |
Static Private Member Functions | |
static void | sigchildHandler (int signum) |
Static Private Attributes | |
static bool | has_handler = false |
It can be used for the same thing as Thread but the main task is to allow external program execution.
WARNING! Do not registrate signal handler for SIGCHLD. Never. You will break all your processes.
Definition at line 49 of file Process.h.
|
Definition at line 43 of file Process.cc. References _arguments, _receiver, _status, finalizeProcesses(), nil, process_id, and runningProcess. Referenced by fork(). 00044 { 00045 finalizeProcesses(); 00046 _receiver = nil; 00047 _arguments = nil; 00048 // Nastavime priznak beziciho vlakna 00049 runningProcess = false; 00050 process_id = 0; 00051 _status = 0; 00052 }
|
|
Definition at line 54 of file Process.cc. References _arguments, _receiver, _status, finalizeProcesses(), process_id, and runningProcess. 00055 { 00056 finalizeProcesses(); 00057 // Nastavime vykonnou funkci 00058 _receiver = receiver; 00059 _arguments = args; 00060 // Nastavime priznak beziciho vlakna 00061 runningProcess = false; 00062 process_id = 0; 00063 _status = 0; 00064 }
|
|
Destructor. It DOES NOT terminate the child process 'cause sometimes it could be usefull. The child process must be terminated explicitly. Definition at line 66 of file Process.cc. 00067 {
00068 // terminate();
00069 }
|
|
Definition at line 100 of file Process.cc. References _arguments. 00101 { 00102 _arguments = args; 00103 }
|
|
Definition at line 204 of file Process.cc. References _status, finalize(), process_id, and runningProcess. Referenced by sigchildHandler(). 00205 { 00206 int pid; 00207 pid = waitpid(process_id, &_status, WNOHANG); 00208 if (pid > 0) { 00209 runningProcess = false; 00210 finalize(); 00211 } 00212 }
|
|
Definition at line 176 of file Process.cc. References processes, and Core::Set::remove(). Referenced by childSignaled(), finalizeProcesses(), and terminate().
|
|
Definition at line 116 of file Process.cc. References Core::Set::copy(), finalize(), isRunning(), Core::Collection::iterator(), and processes. Referenced by Process(). 00117 { 00118 Set *temp = (Set *) processes.copy(); 00119 for (Iterator* i = temp->iterator(); !i->finished(); i->next()) { 00120 Process *p = (Process *) i->value(); 00121 if (!p->isRunning()) 00122 p->finalize(); 00123 } 00124 delete temp; 00125 }
|
|
Definition at line 88 of file Process.h. References fork(). 00090 { 00091 Callback1<TReceiver,void,Array*> *callback; 00092 callback = new Callback1<TReceiver,void,Array*>(receiver, execFunc); 00093 return fork(callback, args); 00094 }
|
|
Definition at line 80 of file Process.h. References fork(). 00081 { 00082 Callback0<TReceiver,void> *callback; 00083 callback = new Callback0<TReceiver,void>(receiver, execFunc); 00084 return fork(callback); 00085 }
|
|
Definition at line 78 of file Process.cc. References Process(), and run(). 00079 { 00080 Process *process = new Process(receiver, args); 00081 process->run(); 00082 return process; 00083 }
|
|
Definition at line 71 of file Process.cc. References Process(), and run(). Referenced by fork(), and run(). 00072 { 00073 Process *process = new Process(receiver); 00074 process->run(); 00075 return process; 00076 }
|
|
Starts program "progName" in new process. Parameters: progName - name of program to start args - (Array of String) arguments to be passed to program fds - (Array of IOAccesor) file descriptors to which stdin, stdout and stderr will be redirected. Definition at line 85 of file Process.cc. References privateExecuteJob(). 00087 { 00088 Callback1<Process,void,Array*> *callback; 00089 Array *funcArgs = Array::with(progName, args, fds); 00090 Process *process = new Process; 00091 00092 callback = new Callback1<Process,void,Array*>(process, &Process::privateExecuteJob); 00093 process->receiver(callback); 00094 process->arguments(funcArgs); 00095 process->run(); 00096 return process; 00097 }
|
|
Definition at line 198 of file Process.cc. References runningProcess. Referenced by finalizeProcesses(). 00199 { 00200 return runningProcess; 00201 }
|
|
Definition at line 214 of file Process.cc. References Core::String::asCString(), Core::SequenceableCollection::at(), Core::Array::at(), Core::FileAccessor::changeDescriptorTo(), nil, and Core::SequenceableCollection::size(). Referenced by forkJob(). 00215 { 00216 String *progName = (String *) args->at(0); 00217 SequenceableCollection *progArgs = (SequenceableCollection *) args->at(1); 00218 SequenceableCollection *fds = (SequenceableCollection *) args->at(2); 00219 00220 char *file = progName->asCString(); 00221 char *fileArgs[progArgs->size() + 2]; 00222 fileArgs[0] = file; 00223 for (int i = 0; i < progArgs->size(); i++) 00224 fileArgs[i+1] = dynamic_cast<String *>(progArgs->at(i))->asCString(); 00225 fileArgs[progArgs->size() + 1] = NULL; 00226 /* file descriptors assignment */ 00227 if (fds != nil) { 00228 FileAccessor *fd = dynamic_cast<FileAccessor *>(fds->at(0)); 00229 if (fd != nil) { 00230 close(0); // stdin 00231 fd->changeDescriptorTo(0); 00232 } 00233 fd = dynamic_cast<FileAccessor *>(fds->at(1)); 00234 if (fd != nil) { 00235 close(1); // stdout 00236 fd->changeDescriptorTo(1); 00237 } 00238 fd = dynamic_cast<FileAccessor *>(fds->at(2)); 00239 if (fd != nil) { 00240 close(2); // stderr 00241 fd->changeDescriptorTo(2); 00242 } 00243 } 00244 execvp(file, fileArgs); 00245 }
|
|
Definition at line 105 of file Process.cc. References _receiver. 00106 { 00107 _receiver = obj; 00108 }
|
|
Definition at line 128 of file Process.cc. References process_id, and runningProcess. 00129 { 00130 if (runningProcess == false) { // vlakno dosud nebylo vytvoreno 00131 // zadna akce 00132 return; 00133 } 00134 kill(process_id, SIGCONT); 00135 }
|
|
Definition at line 147 of file Process.cc. References _arguments, _receiver, Core::Set::add(), Core::Callback< void >::executeWith(), fork(), has_handler, nil, process_id, processes, runningProcess, and sigchildHandler(). Referenced by fork(). 00148 { 00149 int pid; 00150 if (_receiver == nil) return; // Not fully initialized 00151 00152 if ((pid = ::fork()) == 0) { 00153 /* I'm child */ 00154 runningProcess = true; 00155 _receiver->executeWith(_arguments); 00156 exit(0); 00157 } else { 00158 /* I'm parent */ 00159 if (pid == -1) { // fork failed 00160 // Better to raise an Exception here 00161 return; 00162 } 00163 runningProcess = true; 00164 process_id = pid; 00165 processes.add(this); 00166 if (!has_handler) { 00167 struct sigaction handler; 00168 handler.sa_handler = Process::sigchildHandler; 00169 handler.sa_flags = SA_NOCLDSTOP; 00170 sigaction(SIGCHLD, &handler, NULL); 00171 has_handler = true; 00172 } 00173 } 00174 }
|
|
Definition at line 247 of file Process.cc. References childSignaled(), Core::Set::copy(), Core::Collection::iterator(), and processes. Referenced by run(). 00248 { 00249 Set *temp = (Set *) processes.copy(); 00250 for (Iterator* i = temp->iterator(); !i->finished(); i->next()) { 00251 Process *p = (Process *) i->value(); 00252 p->childSignaled(); 00253 } 00254 delete temp; 00255 }
|
|
Definition at line 110 of file Process.cc. References _status. 00111 { 00112 return _status; 00113 }
|
|
Definition at line 138 of file Process.cc. References process_id, and runningProcess. 00139 { 00140 if (runningProcess == false) { // vlakno dosud nebylo vytvoreno 00141 // zadna akce 00142 return; 00143 } 00144 kill(process_id, SIGSTOP); 00145 }
|
|
Definition at line 181 of file Process.cc. References finalize(), process_id, and runningProcess. 00182 { 00183 // zruseni vlakna 00184 if (runningProcess != false) { 00185 kill(process_id, SIGTERM); 00186 // Pockame az vlakno dobehne a odchytime navratovou hodnotu 00187 finalize(); 00188 } 00189 }
|
|
Definition at line 191 of file Process.cc. 00192 { 00193 /* nothing 00194 - there is no way to force another process to leave the processor */ 00195 }
|
|
Definition at line 56 of file Process.h. Referenced by arguments(), Process(), and run(). |
|
Definition at line 55 of file Process.h. Referenced by Process(), receiver(), and run(). |
|
Definition at line 59 of file Process.h. Referenced by childSignaled(), Process(), and status(). |
|
Definition at line 40 of file Process.cc. Referenced by run(). |
|
Definition at line 58 of file Process.h. Referenced by childSignaled(), Process(), resume(), run(), suspend(), and terminate(). |
|
Referenced by finalize(), finalizeProcesses(), run(), and sigchildHandler(). |
|
Definition at line 60 of file Process.h. Referenced by childSignaled(), isRunning(), Process(), resume(), run(), suspend(), and terminate(). |