Chapter 24 Process Creation
Objectives
We would like to :
- Understand the system call associated with process creation.
- Understand process ids
- Understand the basics of the fork call.
Notes
- This is chapter 24
- System calls to deal with processes
- fork creates a new process
- exit destroys a process
- wait allows an ancestor process to be notified when a descendant process exits.
- exec* replaces the memory (including text segment) in a process with different memory.
- But these are not all
- We have seen the picture on 515 multiple times, but it is worth another look.
-
- He notes in the book that the exec is not required
- This is the foundation of one form of parallel programming.
- If we just had some form of ipc.
- The wait is not required,
- but if it is not called a zombie will be formed.
- The parent is notified by means of a signal.
- This is a software interrupt.
- We may look at these later.
- Just to be sure
- From chapter 6.2 and other sources
- Each process has a process id
-
pid_t getpid(void);
-
pid_t getppid(void);
- pids will eventually wrap
- But the first 300 will not be reused
- pid 1 is special.
- pid 2 is probably special as well
- /proc/sys/kernel/pid_max, to see or change the maximum process id
- This was once limited to 32,767
- /proc/sys/kernel/thread-max contains the maximum number of threads+processes
-
ulimit -u
displays the maximum number of threads and processes for a user.
-
pid_t fork()
- Return
- -1 error
- 0 to the child process
- child_pid to the parent process.
- The two processes have the same copies of
- stack, heap, data, ... registers, ...
- Well actually not all, but copy on write.
- Many things, like file descriptors are shared.
- The man page has a long list of technical details that are not shared.
- But they will not impact us for a long time.
- Race conditions
- We do not know which process will run first after a fork.
- Or if they might both run at the same time.
- But both will share the terminal for output!
- The system is non-deterministic!
- So debugging can become a problem.
- Be careful with fork inside of a loop
- It can easily escape and become a fork bomb.
- Copy on write
- Many calls to fork are immediately followed by a call to exec.
- Originally fork would make a copy of all memory.
- Now it will only copy memory when one of the processes tries to write to it.
-
vfork()
will not copy memory.
- It is designed to be followed immediately by exec.
- Some programs
- basic.cpp is a basic example.
- race.cpp has two processes writing to a file.
- readrace.cpp is less exciting, but two processes read from a file.
- multifork.cpp is the basis of a parallel program.