Need help with fork, forking multiple childs and shared memory

Thread Tools Search this Thread
Top Forums Programming Need help with fork, forking multiple childs and shared memory
# 1  
Old 07-28-2009
Computer Need help with fork, forking multiple childs and shared memory

Hi all,

I m writing an application, where i need to fork multiple childs and those child should handle particular task given to them.

More descriptive.

For example, suppose i have 4 Network, each network has multiple nodes. Now on the basis of network child should be forked and these child will again fork multiple childs on the basis of nodes.

It like
Main Prg
Main-Child1 Main-Child2 Main-Child3 Main-Child4
MC1-1 MC2-1 MC3-1 MC4-1
MC1-2 MC2-2 MC4-2
MC1-3 MC2-3 MC4-3
MC1-4 MC4-4

Now in above diagram there can be more main child depending on the network size and there can me more main-childs child depending on the node in that main-child network. and the lowest level child will be performing some task given to it,its similar task that are other lower child are doing but the difference is the type of data they are provided.

How do i use a shared memory or pipes or kind of IPC to communicate between main-child to its child and from main prg to main-childs to provide some task info to lower level childs.

I have exercised for this and i tried simple program that creates multiple childs. I just want that if any body address me for this scenario that how can i accomplish the above , or any test program any body can give for above scenario.

Thanks in advance.
# 2  
Old 07-29-2009
You can easily enough create some shared memory:

#include <sys/mman.h>

int sharedsize=getpagesize(); // Should map memory in multiples of this
void *mem=mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  fprintf(stderr, "Couldn't map shared memory\n");
  fprintf(stderr, "%d bytes of shared memory at %p\n", sharedsize, mem);

Some systems use MAP_ANON instead of MAP_ANONYMOUS.

As for communication, what sort of communication is needed? Should these processes wait for instructions, or will you need to reach out and poke them to tell them to check on something?

Last edited by Corona688; 07-29-2009 at 02:22 PM.. Reason: typo
# 3  
Old 07-30-2009

Yes communication means, every lower level child should tell its parent that i have finished and give another task that is pending. Means the lower level child will first see for the instruction to do the task then after performing that task it will notify the parent that i have done,now parent will wait for child notification and when it gets child's notification it will again tell child that this is your another task.

I have written a sample program that creates main child's and then these child's again creates another sub child's. Now my problem is the main child's exits after forking sub child, how do i make them wait for permanent basis.
# 4  
Old 07-30-2009
Few cents from me.

Is the number of children from main program and the number of childs (second level) from the first level children constant? Or is that something dynamic and subject to change based on the network configuration?

If that is not a constant
- it would be worth not spawning the children in advance and instead spawn them only on demand basis and terminate once the functionality of the child is done.
- by this, once the child is terminated, kernel will deliver SIGCHLD signal to the parent, and parent can get the execution status of the child from that.
- parent can fork and exec, on needed basis and by this child need not be awake all the time consuming resources even when not doing any work.
- in short, if there is work, parent will fork and exec, child will work and die, kernel will intimate the parent.
# 5  
Old 07-30-2009


No the no of first level children as well as second node level children are not constant all are dynamic.

What i m doing is as below
- I m looking for total networks.
- then i fork that many child's for network level this is my first level
- now on each first level child i m counting no of nodes
- and here i m forking second level children.
- now the second level child will be there till the network is there.

means the first level child will be running till the network is there, and the second level child will be running still there are nodes.
so both the first level and second level chidren are there for some much amount of time.

now in second level children i m doing some x type of task and calling
kill signal as kill(getppid(), userdefined signal) to the parent.

now the problem is i m not able to identify which children gave the interrupt to parent , i have signal handler but in that signal handler how do i identify which child have sent me the signal.

If i identify the child then i can again assign that child some pending task for waiting node in node list.

If you want can post my whole code. i m really stuck over here.

Any suggestions.
# 6  
Old 07-31-2009
use SIGCHLD - kernel intimates the parent that child is done ..
and wait on the handler, this will return the PID of the child to parent provided WNOHANG is not used.

Give it a try and let us know.
# 7  
Old 08-07-2009


Sorry for late reply.

Actually i was implementing the above program so i was bit busy, still i m facing one problem as i said earlier, how do i know which child sent me a signal, its not SIGCHLD , i m sending user defined signal to parent, and in sig handler i m not able to know which child sent me a signal, my child is running forever, means once the particular child sent me a singnal i wiil reasign some task to it, but i m not able to identify.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Shared library with acces to shared memory.

Hello. I am new to this forum and I would like to ask for advice about low level POSIX programming. I have to implement a POSIX compliant C shared library. A file will have some variables and the shared library will have some functions which need those variables. There is one special... (5 Replies)
Discussion started by: iamjag
5 Replies

2. Programming

forking for multiple clients

hello everyone, i am making a tcp chat server using c in linux. i have used socket programming to connect the server and the client. can anyone please let me know how i can use forking for multiple clients?? thank you (1 Reply)
Discussion started by: sweetbella
1 Replies

3. Programming

waiting for multiple childs - C - waitpid

Hi gurus, I would like to fork more children and then write their return values: so far I tried: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv=0, i; ... (5 Replies)
Discussion started by: wakatana
5 Replies

4. Programming

forking. sharing global data in childs

hi, i want to write a code for forking 3 4 child. n wants that every child process one of the account from global account list. i wrote a program for that, but problem is every child is processing every account in list. what can me done to avoid it. attaching code with it #include <stdio.h>... (2 Replies)
Discussion started by: anup13
2 Replies

5. Programming

Shared memory for shared library

I am writing a shared library in Linux (but compatible with other UNIXes) and I want to allow multiple instances to share a piece of memory -- 1 byte is enough. What's the "best" way to do this? I want to optimize for speed and portability. Obviously, I'll have to worry about mutual exclusion. (0 Replies)
Discussion started by: otheus
0 Replies

6. Programming

Shared memory in shared library

I need to create a shared library to access an in memory DB. The DB is not huge, but big enough to make it cumbersome to carry around in every single process using the shared library. Luckily, it is pretty static information, so I don't need to worry much about synchronizing the data between... (12 Replies)
Discussion started by: DreamWarrior
12 Replies

7. Programming

memory sharing - not shared memory -

hi, this is the problem: i want to swap a linked list between 4 processes (unrelated), is there any way i can do that just by sending a pointer to a structure? //example typedef struct node { int x; char c; struct node *next; } node; or i should send the items ( x,c ) by... (9 Replies)
Discussion started by: elzalem
9 Replies

8. Programming

Memory leak of fork()

Today, I wrote a test code for fork/execvp/waitpid. In the parent process, it fork 100 child processes which only execute "date" to print the current datetime. When any child process die, the parent process will receive a SIGCHLD signal. Then, the parent process will re-fork-execvp the child... (7 Replies)
Discussion started by: whererush
7 Replies

9. Programming

mmap vs shared memory - which is faster for reading data between multiple process

Between mmap and shared memory which is the best method of sharing data between multiple applications, interms of speed? (1 Reply)
Discussion started by: nmds
1 Replies

10. UNIX for Advanced & Expert Users

Shared memory shortage but lots of unused memory

I am running HP-UX B.11.11. I'm increasing a parameter for a database engine so that it uses more memory to buffer the disk drive (to speed up performance). I have over 5GB of memory not being used. But when I try to start the DB with the increased buffer parameter I get told. "Not... (1 Reply)
Discussion started by: cjcamaro
1 Replies
Login or Register to Ask a Question