Processes and Threads
-
Definition of a Process: A Program in execution
-
Includes Program Counter, Registers, open file descriptors, memory, stack,
etc.
-
There can be more than one process all running the same program. No problem.
-
Normally, the state for each process is stored in a process control block.
-
Scheduling
-
Switching from one process to another is called a context switch.
-
Normally takes from 100 ... 1000 instructions. Normally happens 100 ...
1000 times /second
-
Includes
-
Switch to kernel state and memory mapping
-
save applicatiosn regsters
-
put app on appropriate que (ready, device queue, timer queue, etc)
-
recompute apps priority
-
figure out what to run next
-
switch to that apps conext
-
load that apps registers
-
jump back into that app.
-
It's just overhead
-
Normally takes hundred or thousands of instructions
-
Overhead is SwitchInstr * SwitchRate / MIPS
-
60 * 1000 / 1000000 = 6%
-
60 * 2000 / 100,000,000 = 0.12%
-
Overhead dependant on hardware support
-
Decsystem-20 has muliple registers and can change the active register set
with one instruction
-
Sparc has overlapping registers windows and might need to dump/restore
200 registers.
Process Creation and Destruction
Process Creation
-
fork()/exec() model
-
Primative fork() clones current process in all respects
-
One process is PARENT, other is CHILD (can tell difference only on fork
return code).
-
Code/bss/file-descriptors all shared
-
Stack/Data marked Copy-On-Write
-
Exec() replaces old program with new program
-
KEEPS SAME PROCESS
-
Keeps same file descriptors, resource limits, etc.
-
New code, data, stack
-
Common paradigm is ..
-
fork()
-
if (child)
-
change file descriptors
-
exec other program
-
else // parent
-
wait for child to finish
-
System() model.
-
Name program file you with run
-
Sometimes can name file descriptors or limits, sometimes not.
-
Plan 9 model
-
Plan 9 is from ATT (the Unix people)
-
Blend of the previous two.
-
Does fork() like thing, but each element can be copied or not.
-
Could copy data (for example), but don't have too.
-
Linux has this (called clone()).
Process Termination
-
Normal termination by exit() call.
-
Unix/DOS allow one-integer EXIT CODE that normally indicates success/failure
-
MacOS has no exit code.
-
Can terminate by
-
Resource limit violation
-
Parent exits and system does not allow a child without a parent (VMS).
-
interrupt
-
Termination should free code/data/stack/file-descriptors/other-kernel-data
Threads
-
Definition: a CPU context sharing within in address space and protection
domain.
-
Threads include program counter, register set, stack space
-
Threads share code and data sections, open file descriptors, signals, address
mappings.
-
Program considerations:
-
Auto variables are NOT shared
-
malloc'ed new'ed variables ARE shared.
-
File descriptors ARE shared.
-
Library might screw you (consider errno)
-