In the concept of operating system we always say that in the fork() execution the child and parent have their own copy of heap and stack but they share memory segment.My question is when we say shared memory segment does that mean the text or code that they are going to execute?
2 Answers
Right (at least that happens on Linux). According to Linux manpage for fork (you can watch it by typing man 2 fork in your console or entering here if you don't have any Linux):
fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points:
- Process ID is unique for parent & child
- Parent Process ID of child is the same as the parent's Parent Process ID
- Memory locks, semaphores, signals, etc. aren't inherited
- ...
2 Comments
In addition to the answer provided above.
The fork() system call creates a process that becomes an replica of the parent process that it is forking. Same executable.
The example provided here clearly explains the concept.here
However we may want to execute the new process with different existence .For that we require the exec() system call.
Exec() system call creates a new process from existing executable . So the problem of the new process having duplicate executable can be solved using exec().
Various types of Exec() calls are explained here.Exec()