Solved

Parent & child accessing global variable

Posted on 1998-10-19
8
1,015 Views
Last Modified: 2013-12-26
Okay, I've asked this same question in the usenet comp.unix.programmer newsgroup, but will go into more detail here.

I have a server which waits until it receives requests from clients. When this happens, a fork() occurs and the child process is told to deal with the request. This way, the server can wait for more incoming requests without delay.
I have a global variable which is a linked list of a struct which I have declared, the child process makes alterations to the variable, but these changes are not saved/seen once the child exists - I understand that a child process created with the fork keyword cannot access it's parents data space.
Is there anyway I can somehow overcome this hurdle?
I've been told to send the data from the child to the parent via a pipe, but this would mean that the parent process would have to wait or detect when the child process has finished ...

Any ideas?

Thanks in advance.

Chris.

0
Comment
Question by:dufus1
  • 4
  • 3
8 Comments
 
LVL 2

Accepted Solution

by:
kellyjj earned 20 total points
ID: 1293727
the pipe is proboly the best methond of this.  

How ever you don't neccarily need to wait in the parent for the child.

while (!done)
{
   check_if_child_needed
      spawn_child

   check_if_child_is_done
       read_data_from_pipe
       kill_child
}

In the child you will want to wait to die until the parent has gotten the data.

Hope this gets you going.
0
 

Author Comment

by:dufus1
ID: 1293728
How could I determine whether the child is done though ...?
How would the parent know that the child has finished processing and is ready to send the linked list accross?
Would I have to use some for of IPC?

Thanks for your help

Chris.


0
 

Author Comment

by:dufus1
ID: 1293729
How could I determine whether the child is done though ...?
How would the parent know that the child has finished processing and is ready to send the linked list accross?
Would I have to use some for of IPC?

Thanks for your help

Chris.


0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Expert Comment

by:kellyjj
ID: 1293730
You could have the child send a signal to indicate it was through
or you could use IPC.  Also you could have some kind of record or byte string to send through the pipe to indicate it.
0
 

Author Comment

by:dufus1
ID: 1293731
Hi, thanks for your advice.

I'm not 100% sure this is going to work though.
My code looks something like this in pseudo-code

for(;;){
    wait_for_network_connection() ;
    // via accept keyword
    fork() and child processes_received_client_request() ;
} ;

Most of the time, the program will be waiting for a new request from a client and will only be in a position to check whatever's in the pipe or the status of the child when a new connection has been accepted .. this isn't really how I'd like things to be done. Ideally, the linked list would be updated as soon as possible...

Do you understand what I rambling on about?!?!
Perhaps I'm not using the correct approach?

Thanks again for your time.

Chris.


0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1293732
I think I understand what you are trying to say.

Instead of 'waiting ' for a connecetion you can check for it. this way you are not stuck waiting for it and can do other things   This should not be a prob as long as you don't get stuck waiting for anything to get done.
0
 

Author Comment

by:dufus1
ID: 1293733
Hi,

Can you tell me how I can check for a connection?
All this hassle ... just because a child process can't access it's parents global variables ...
Looks like Linuxs' clone() would be ideal ...except I'm not using Linux ;-(


0
 

Expert Comment

by:aditya070797
ID: 1293734
Hi Dufus1,
        You asked if your approach could be changed, here are a couple of suggestions below. I think the approach you are taking using pipes is not really practical. You are going to have two-way pipes to each and every child you fork which will make your program extremely complicated/messy (doing selects incrementally on socket and pipe file descriptors) and invariably impossible to maitain assuming that you are forking children to handle connections because you could have a number of concurrent connections (and associated child processes).
        Why don't you use a blob of shared memory instead ? You need to take a guess at how big you want this blob to be; You could use this blob as an array of structures as opposed to a linked list. The advantage of using shared memory is that you can have any process attach to this blob and use it as if it were local to it. You should now worry about locking/unlocking on this blob since concurrent processes would be updating it (which would be the case anyway).
      This is a typical problem of synchronization in a shared environment since the very reason you need a seperate process to handle each connection is that there could be multiple concurrent connections and any number of children could want to modify the list at any given time. Before you take on a job of this sort, you DO need to have an understanding of UNIX IPC.
         A very much better alternative would be to use p-threads instead of concurrent processes since they share the same data-space and sophisticated locking mechanisms are already provided by p-threads. If you are looking for reader-writer locking functions (if they are not provided in your machine's implementation of p-threads), you can find my implementation of them in the unix programming archives at this site or from Richard W. Steven's UNIX Network Programming Vol II 2nd Edition.
Let me know if this is of any help
--Aditya.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 221
pre4 challenge 19 104
EvenOdd challenge 10 127
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 104
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question