parent and child in fork system call.

when child is created by the fork system call. what are the things that the child share with the parent process.
Who is Participating?
phoffricConnect With a Mentor Commented:
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.)
The parent and child also share any open file handles, which is how they can easily set up pipes between them.
Brian UtterbackConnect With a Mentor Principle Software EngineerCommented:
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?
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

phoffricConnect With a Mentor Commented:
>> 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).
Brian UtterbackPrinciple Software EngineerCommented:
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.
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.
nociConnect With a Mentor Software EngineerCommented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.