Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Will the child process by fork() automatically copy the shared memory 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm really confused about unix fork() even after reading many articles. Really appreciate your reply if you know answers.

It's said the child process duplicates everything of parent process including the code. Does the child execute the parent program code from the beginning once it's created? For example:
/*some statements before fork()*/
int pid = fork();
if( pid < 0 ){ perror(&quot;...&quot;); exit(EXIT_FAILURE);}
else if( pid ) //parent process
{
/*some code here*/
}
else // child process
{
/*child code here*/
}

Because I just want the child process execute the child code within braces. And I tried several times, some results are weird, and often many many child processes are spawned. So I'm wondering the child process executes the parent code from the beginning? Am I right? I hope I'm wrong.But I really found fork() is hard to use.

Another puzzle is if parent creates a share memory segment before fork(), does the child also duplicates this share memory space? or only the share memory id?

One question about share memory again. if use semaphore for mutual exclusion, shmget(key_t key,..,..), do I have to the value of semaphore id to key? as I read one sentence like this: &quot;the key is associated with the semaphore ID&quot;.

Thnaks for reply!

 
Both the original code and the forked process resume at the statement after the fork call, they don't start at the start of main() as you asked (obvious really as if they did they would end up at the fork command again and each would fork off another process - ad nauseam....)

As far as I acn remember the two processes will not share the shared memory area.

I did not understand your third question - I suspect one or more words were left out.

Personally I don't like coding the parent and child logic in the one program, I prefer to have the forked child exec() the child logic from a separate module, it makes coding a lot easier - especially when there are to be many many child processes. If you take this approach you can then use vfork() which is a lot faster as it doesn't do much of the duplication work normally associated with a traditional fork() on the basis that the forked thread won't actually do much other than run an exec().
 
Thanks very much!
Could you say more about exec(), I use &quot;man exec&quot;, it's said it's a shell built-in functions to execute
other commands.So if I want to let the child execute child.c program, how to do it? and how can the child accesses data, variables duplicated from parent since they are now in two different .c programs.
I totally agree that the parent and child processes code should not be logically in one program. I am a begineer of the UNIX C, and I'm wondering why there is no some system function like create_process in Windows to specify c program to be executed by the child directly.

For my third question,I want to know what value I should assign to the parameter of key in shmget(key_t key,..) if I have defined a semaphore for the shared memory access synchronization.

Thanks again!
 
Glad to be of assistance.

Regarding the exec call, there are two exec functions available - the first is the shell exec which you have found, the second is part of the C standard library. You can find the man page for this is section 3 (do man execv to find more info).

In the man page you will find the various permutations of the exec API. The difference between fork and exec is as follows.

1) fork, as you have already discovered spawns another copy of the original process (note these processes can share a shared memory area as described below - my origianl post was slightly wrong but read on for reasons why I don't use this)

2) exec and its variations work differently in that the exec'ed process REPLACES the caller completely. The calling process disappears once the exec function starts.

The combination of fork and exec where the forked child immediately execs the required child logix code replicates the spawn() and create_process() functions you have come across in windows. It's also the reason why the vfork() function was created. It takes a certain amount of work to fork a complete child process, if it is known that the forked child will only exec another process then there is no point in doing much of the fork work and vfork omits this logic, thereby saving wasted time.

Hope this helps you.

Re the shmget key, The key can be any positive integer you choose to use. If you use the constant IPC_PRIVATE then the shared memory are will be reserved exclusively for the process (and its forked children) but I don't use that due to my use of the fork/exec logic.

Cheers - Gavin
 
Blueruby,

Just a couple of points of clarification.

1) Shared memory will be available to forked child processes. If you are sharing with only child processes, create and attach before the fork and everything will be automatic. In this case you can use the IPC_PRIVATE key to create the segment and avoid the problem of key collision. Just make sure you clean up the segments or they will build up after multiple runs of your program.

2) Semaphores are typically maintained across a fork (depending on the type) as well, but you must make sure that you set the process-shared attribute, if threre is one, to indicate that the semaphore will operate acorss process boundaries rather than thread boundaries.

3) If at all possible use threads to fulfill your multi-processing needs rather than processes. It is much easier.

Mark.
 
Minor notes on fork/threads

1) If you are forking with shared memory, make sure only one process clears out the shared memory. If shared memory is cleared out by the first process to die, the program you will start wondering why the parent dies when the child dies. It took me a long time to work this one out.

2) STL type things and Rogue Wave type things like vectors and strings do not fork very well. The child doesn't always see the initialized vectors/strings.

3) If you are using threads on Solaris 2.8, semaphores can be used on any thread but must be initialized on the main thread.
 
wow , this is beautiful !
i searched in google without a cule for &quot; c++ command fork &quot;
and ur discussion totaly solves my problem at all :D
thx and hooray \o/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top