Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what parent process stuff gets shared in newly created child process in Linux

Tags:

linux

When a process creates a child process using fork() then what are things of parent process that get shared with child process. like address space, memory, signal etc.

Note:-I have gone through the fork manual.still i need some more information about it.I have Google it also.but i need not get complete idea about it.Please somebody explain me deeply how the fork works.

like image 490
Rajdhar Avatar asked Dec 19 '22 19:12

Rajdhar


2 Answers

From Advanced Programming in the UNIX by W. Richard Stevens

The child is a copy of the parent. For example, the child gets a copy of the parent's data space, heap, and stack. Note that this is a copy for the child; the parent and the child do not share these portions of memory

one characteristic of fork is that all file descriptors that are open in the parent are duplicated in the child.

There are numerous other properties of the parent that are inherited by the child:

1. real user ID, real group ID, effective user ID, effective group ID
2. supplementary group IDs
3. process group ID
4. session ID
5. controlling terminal
6. set-user-ID flag and set-group-ID flag
7. current working directory
8. root directory
9. file mode creation mask
10. signal mask and dispositions
11. the close-on-exec flag for any open file descriptors
12. environment
13. attached shared memory segments
14. resource limits
15. Memory mappings

The differences between the parent and child are

1. the return value from fork
2. the process IDs are different
3. the two processes have different parent process IDs—the parent process ID of the     child is the parent; the parent process ID of the parent doesn't change
4. the child's values for tms_utime, tms_stime, tms_cutime, and tms_ustime are set to 0
5. file locks set by the parent are not inherited by the child
6. pending alarms are cleared for the child
7. the set of pending signals for the child is set to the empty set
like image 182
sujin Avatar answered Apr 29 '23 22:04

sujin


Any reference to Stevens is normally a good reference in my book. However, one thing Stevens doesn't do is give Linux specific answers, and the clone system call did not exist when Stevens wrote his book.

Given you've tagged this Linux, I am supposing you want the Linux specific answer, which you'll find with man clone. This gives you the entire list of things that might or might not be shared with fork(), as fork() is implemented using clone(). From memory, fork() uses clone() passing no flags, (i.e. a 0). The manpage for clone() will thus tell you exactly what it does and does not copy.

There is a good explanation here: https://unix.stackexchange.com/questions/87551/which-file-in-kernel-specifies-fork-vfork-to-use-sys-clone-system-call

like image 38
abligh Avatar answered Apr 29 '23 22:04

abligh