Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Process.h

Go to the documentation of this file.
00001 /*
00002  * Process.h
00003  *
00004  * Smalltalk like class library for C++
00005  * Process wrapper. Header.
00006  *
00007  * Copyright (c) 2004 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #ifndef __PROCESS_H
00025 #define __PROCESS_H
00026 
00027 #if !defined(_REENTRANT)
00028 #  define _REENTRANT
00029 #endif
00030 #include <unistd.h>
00031 #include <signal.h>
00032 
00033 #include <stlib/Object.h>
00034 #include <stlib/Array.h>
00035 #include <stlib/Set.h>
00036 
00037 #include <stlib/Callback0.h>
00038 #include <stlib/Callback1.h>
00039 
00040 namespace OS {
00041 
00049 class Process : public Core::Object
00050 {
00051   private:
00052     static bool has_handler;
00053 
00054   protected:
00055     Callback<void> *_receiver;   // what to do in the process
00056     Array *_arguments;
00057 
00058     int process_id;
00059     int _status;
00060     bool runningProcess;          // running process flag
00061 
00062     static Set processes;
00063 
00064   protected:
00065     Process(void);
00066 
00067   public:
00068     Process(Callback<void> *receiver, Array *args = nil);
00073     virtual ~Process(void);
00074 
00075     /* Instance creation protocol */
00076     static Process *fork(Callback<void> *receiver);
00077     static Process *fork(Callback<void> *receiver, Array *args);
00078 
00079     template <class TReceiver>
00080     static Process *fork(TReceiver *receiver, void (TReceiver::*execFunc)(void))
00081     {
00082         Callback0<TReceiver,void> *callback;
00083         callback = new Callback0<TReceiver,void>(receiver, execFunc);
00084         return fork(callback);
00085     }
00086 
00087     template <class TReceiver>
00088     static Process *fork(TReceiver *receiver, void (TReceiver::*execFunc)(Array *),
00089                          Array *args)
00090     {
00091         Callback1<TReceiver,void,Array*> *callback;
00092         callback = new Callback1<TReceiver,void,Array*>(receiver, execFunc);
00093         return fork(callback, args);
00094     }
00095 
00104     static Process *forkJob(String *progName, SequenceableCollection *args = nil,
00105                             SequenceableCollection *fds = nil);
00106 
00107     static void finalizeProcesses(void);
00108 
00109     /* Accessing protocol */
00110     virtual void arguments(Array *args);
00111     virtual void receiver(Callback<void> *obj);
00112     virtual int status(void);
00113 
00114     /* Controlling protocol */
00115     virtual void resume(void);
00116     virtual void run(void);
00117     virtual void finalize(void);
00118     virtual void suspend(void);
00119     virtual void terminate(void);
00120     virtual void yield(void);
00121 
00122     /* Testing protocol */
00123     virtual bool isRunning(void);
00124 
00125     /* Private protocol */
00126     virtual void childSignaled(void);
00127 
00128   private:
00129     void privateExecuteJob(Array *args);
00130     static void sigchildHandler(int signum);
00131 };
00132 
00133 };
00134 
00135 #endif /* __PROCESS_H */

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2