Operations On Processes, Creation and Exit
Objectives
We would like to :
- Understand basics of process creation and destruction
Notes
- In linux each process has a unique process id.
- Fork will create a new process
- There are other mechanisms as well, but they are not important here.
- vfork, clone
- The
fork
command creates an identical copy of the process
- copy of the stack, heap, text, data, ... memory segments
- Copy of the registers (ie both will start executing at the same point)
- But they will not:
- Have the same process id
- have the same return value from fork
- The old (parent) process will get the new (child's) process id
- The child will get a 0.
- The child will have parent process id of the parent, not the parent's parent.
- Some IPC mechanisms will not be inherited.
- Utilization stats will be set to 0 in the child.
- See man 2 fork.
- The parent can choose:
- Continue to execute: a parallel program for example
- Wait for the child: the shell without job control.
- Linux/unix implements a copy on write for memory
- Many calls to fork are immediately followed by a exec
- So memory duplication is not needed.
- Calls to
exit
will terminate the process
- This is the same as a call to return.
- There are other mechanisms as well, but they are not important here.
- Most resources are freed.
- In linux a small amount of data is retained until the parent acknowledges the exit. (zombie)
- In *nux the parent is sent a signal that a child process has exited (SIGCHILD)
- The parent can receive this signal with
wait
- And get the exit status of the child.
- If the parent is gone, systemd gets the message.
- Or some other suitable process
- A process without a parent is called an orphan.