Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

fork in C++

0 views
Skip to first unread message

Ramesh

unread,
May 31, 2009, 3:32:42 PM5/31/09
to
Hi All!

I have written a progarm which will create 10 child processes ( by
fork) to execure a method simuleneously; But the the child will do
database updates. In the parent I need database success/failure update
counts done by each child. For that I declared global two arrays to
assign in the child processes; But once the child process exists then
global variables coming to original values, the child updated values
are not reflecting once child existed; I tried even with static
member variables also;

two static member variable are declared:

static int fList[10] ;
static int sList[10] ;

Static def:

int C_SMSSvcRetroExpireProcess::sList[] = { 0 };
int C_SMSSvcRetroExpireProcess::fList[] = { 0 };

Fork:
for ( index = 0; index < MAX_PROCESSES && (pid = fork()) !
= 0 ; index++ )
{
// Spawned a number of child processes to be executed
// simultaneously.
........
}
// child process
if ( pid == 0 )
{
// this is the method to be called, which will update
database inturn
smsRetroExprEligibleSvcSuccessCount[index] =
processRetroRecs ( smsSvcDeactvVCltn,
startRecCount,
endRecCount,
fileSeqNbr++,
retroActiveAutoExprDays,
_smsRetroProcessDir,
recsEligibleForUpdate,
index,
childRetCode);

TRACE_BEGIN( APPLICATION ) << "After sList[" << index
<< "]="<< sList[index] << TRACE_END;
TRACE_BEGIN( APPLICATION ) << "After fList[" << index
<< "]="<< fList[index] << TRACE_END;

// exit the child
_exit(childRetCode); // Up to here I am able to print
the above two statement correctly
}

In the child process code I am able print the sList, fList correctly
but once _exit(childRetCode); is executed I lost the child update
values; I tried to declare the sList, fList as global and also
static;But still I am not able track the child update values;

How to retain the fList, sList in parent above even if the childs
exited? Please give me clue... Thank a lot


Andy Champ

unread,
May 31, 2009, 4:20:41 PM5/31/09
to
Ramesh wrote:
> Hi All!
>
> I have written a progarm which will create 10 child processes ( by
> fork) to execure a method simuleneously; But the the child will do
> database updates. In the parent I need database success/failure update
> counts done by each child. For that I declared global two arrays to
> assign in the child processes; But once the child process exists then
> global variables coming to original values, the child updated values
> are not reflecting once child existed; I tried even with static
> member variables also;
>
<snip>

Global data is within one process only (and possibly even smaller areas
than that!). For interprocess communication you will have to use
operating-specific methods.


As you said "Fork" I'd assume you have some kind of Unix; if you post
up which flavour someone will probably be able to suggest a suitable
newsgroup. Your query is off-topic for this one.

Andy

0 new messages