Re: Sharing one class object from one process to another process
Ami wrote:
Hello Tommy,
Once again thanks for all your help. My master program is supposed
to take the inputs from user regarding source/target server
information, user credentials etc and pass it to slave program so that
it can act upon that. if I can create a class object which contains
all master data and can pass it to slave program that solves the
purpose. On the other side, the slave program should also be able to
send the status and progress information back to master process.
As far as I can guess the resources created i.e. logfiles in one
process cannot be accessed in another process but would like to know
if it is possible. If yes, i would like to write in master program
create log file from other processes so that one logfile has all
information at one place.
I hope it gives you some idea about my intentions.
Thanks
Well, overall, it sounds like a traditional hosting application.
Usually a multi-threaded approached, or spawned jobs, like a WEB host
spawning PHP applets.
I don't think model is different. But it seems your main incentive for
using spawns, is the idea that if one fails, it won't fail the entire
process.
By doing so, you now need advanced Inter as oppose to Intra process
communications as methods.
The logging is the simplist. You should be able to use simple log
function as so:
void writelog(const char *sz)
{
FILE *fv = fopen("master.log","at");
if (fv) {
fprintf(fv,"%s\n",sz);
fclose(fv);
}
}
You can add a system level file lock to make it more robust at the
expense of speed, but the most likely you will want loggin order (all
the log lines for slave1 grouped together, slave6 grouped together).
But I think overall, what you want is a DLL maybe that holds the
master data, exposing functions that access the data. The Master.exe
is the owner so it controls the reading and writing via the functions.
Note: I'm doing this above off the top of my head with no compile
syntax testing.
Create two files, master.h, master.cpp
//--------------------------------
// master.h
#pragma once
// Global Data structure
typedef struct _tagTMasterData {
int data1;
int data2;
... so on ..
} TMASTERDATA;
void WINAPI SetData1(int data1);
void WINAPI SetData2(int data2);
int WINAPI GetData2()
int WINAPI GetData1()
void WINAPI MasterLog(const char *sz);
//--------------------------------
// master.cpp
#include "master.h"
TMASTEDATE master = {0};
TREADERWRITER MasterMutex; <<-- FIND ONE! <g>
TCRITICALSECTION LogMutex; <<-- FIND ONE! <g>
void WINAPI SetData1(int data1)
{
TWriterGrabber grab(MasterMutex);
master.data1 = data1;
}
int WINAPI GetData1()
{
TReaderGrabber grab(MasterMutex);
return master.data1;
}
void WINAPI SetData2(int data2)
{
TWriterGrabber grab(MasterMutex);
master.data1 = data2;
}
int WINAPI GetData2()
{
TReaderGrabber grab(MasterMutex);
return master.data2;
}
void WINAPI MasterLog(const char *sz)
{
TCriticalSectionGrabber grab(LogMutex); // PROBABLY NOT REQUIRED
FILE *fv = fopen("master.log","at");
if (fv) {
fprintf(fv,"%s\n",sz);
fclose(fv);
}
}
Compile this into a DLL called Master.DLL .
The above is a variable basic way for a master to have global data
under its control and it exposes functions for threads to use or
processes to import. You can also use this for creating your shared
memory as well, if you prepared to do that. But you will need some
wrapper functions to protect the shared resource. So consider using
the "Get/Set" functions method.
You can search the net for some Reader/Writer, Critical Section class
or just use raw critical section or semaphore method for everything to
get your proof of concept going, and then optimize it later.
Hope this provides some ideas.
--