[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

parent and child in fork system call.

Posted on 2012-09-06
7
Medium Priority
?
667 Views
Last Modified: 2012-10-28
hi,
when child is created by the fork system call. what are the things that the child share with the parent process.
0
Comment
Question by:nagaharikola
7 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 900 total points
ID: 38375082
The child and parent share the code. The data is not shared directly unless a shared memory segment is used, or the data is communicated via pipes between them. The data variables (global or local) can be used in both parent and child, but they are (virtually) different instances. (Virtually, because if a variable is not accessed by the child, then some optimization may occur so that a copy of the variable is not made for the child.)
0
 
LVL 20

Expert Comment

by:simon3270
ID: 38375269
The parent and child also share any open file handles, which is how they can easily set up pipes between them.
0
 
LVL 22

Assisted Solution

by:Brian Utterback
Brian Utterback earned 300 total points
ID: 38377445
Technically speaking, the only thing that is really shared between the parent and child is the process group. Everything else (address space, data, file handles, etc.) are copies in the child of what was in the parent. Now, some things like the file handles are copies that point to the same kernel resources. Other things that are normally shared between process (shared memory, dynamic shared objects) will be shared between the parent and child but that is the result of their inherent shared nature, not as a result of the child and parent relationship.

What is the problem you are trying to solve?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 900 total points
ID: 38377971
>> Everything else (address space, data, file handles, etc.) are copies in the child of what was in the parent.
   The executable code is usually not copied (although it could be copied). Usually, the executable code (i.e., text) memory sections will be shared between the parent and child. But the global and stack variables will be copies if either the value changes either by the parent or child. Older OS's would unconditionally copy the data sections (but that was determined to be inefficient, especially when all the child did was to exec another program).
0
 
LVL 22

Expert Comment

by:Brian Utterback
ID: 38378024
The fact that the memory system may be used to trick the process into thinking that it has a copy when it does not is not really relevant, there is no way for the child to exploit that fact and it looks exactly the same from within the process either way.

Also, I am not so sure about your statement about "older operating systems". Copy on Write has been in existence for since about 1972, although not ubiquitous until the early '80s. But I bet nobody reading this is using an OS from that era. A much more important distinction is whether or not a particular system has virtual memory, which is required for COW.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 38378073
The COW point was a brief aside to clarify that not all data was copied. The question does ask about what is shared, and in most OS's today, the text (code) section is shared.
0
 
LVL 41

Assisted Solution

by:noci
noci earned 300 total points
ID: 38378926
Effectively the whole user context is copied, whether directly or more efficiently with COW.  using COW just means the copy is postponed until data is written to some memory area.  Any previously "shared" resource like shared memory stays shared.

In the kernel the process slot is copied and all reference counts of accessed resources files, sockets etc. are adjusted.  Resources are refered to in process slots by internal links.
The child has no locks inherited.
If the above failes the parent gets -1 from fork().
Otherwise the parent get the pid of the new slot returned, the child gets 0 returned.

See: http://www.kernel.org/doc/man-pages/online/pages/man2/fork.2.html
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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