Parent & child accessing global variable

Posted on 1998-10-19
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.


Question by:dufus1
  • 4
  • 3

Accepted Solution

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)


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

Hope this gets you going.

Author Comment

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



Author Comment

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



Expert Comment

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.
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.


Author Comment

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

    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.



Expert Comment

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.

Author Comment

ID: 1293733

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 ;-(


Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
if loop error 4 77
mixString challenge 36 98
sumDigits challenge 9 98
string initialization in java 11 103
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now