- Threads: abstract the CPU
- address space: memory
- files: disks
- sockets: network
- The Process supports the ability to run multiple programs simultaneously on a single CPU
- CPS only runs one program at a time: switches from process to process quickly
benefits:
- resource utilization (CPU, memory, I/O) can be improved
- allow processes to use the CPU when it would otherwise become idle
- enables the concurrent execution of a programs on a single processors
- higher job throughout
- lower response times when running multiple interactive programs
four main events that cause processes to be created:
- system initialization
- execution of a process creation system call by a running process
- a user request to create
- initiation of a batch job
- batch job is not interactive, java scheduler that runs without user order
- System call in UNIX: fork()
- create a clone of the calling process
- different address spaces: two process always have different spaces
- same memory image
- same values for the PC and general-purpose registers, same open file handlers
- recall that pointer-counter stores the address of the next instruction
int main()
{
fork();
printf("Hello world!\n");
return 0;
}this will result two "Hello world!", as the pointer-counter stores the same instruction, 'printf'
int main()
{
if(fork() == 0)
printf("child!\n");
else
printf("parent!\n");
return 0;
}the next instruction is actually compare return value with 0
fork() is usually followed by an invocation to a function in the exec() family
- execve() cause the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and data segments
typical conditions:
- Normal exit (voluntary)
- Error exit (voluntary)
- error with the program
- Fatal error (involuntary)
- the operating system kills the program (by sending a signal to the process), maybe caused by stackoverflow...
- Killed by another process (involuntary)
mey be in:
- Running: using CPU
- Ready: runnable, but temporarily stopped
- Blocked: unable to run until some external event happens
OS maintains information about a process in a data structure PCB
- process table: list of PCBs (one PCB for each process)
- Process ID, process state, parent process, ...
- provides an execution context to a process
- execution context: program counter, stack pointer, ...
- sequential execution of a set of instructions
- has a PC, SP, registers, stack
- to run, lod execution context to CPU's registers
- PC, SP, general purpose registers with intermediate values for computations
- when not running, the context of a thread is stored in memory, and the processor's
registers hold the context for some other thread
- PC: instruction of some other thread
- SP: stack of... ![[multi_thread_mamory.png]]
- threads execute the environment defined by a process
- a process has one or more threads
- all threads of a process share the same address space
- code, data and heap memory are shared between threads
- each thread has its own stack
-
- parallelism
- has potential to use multiple processors
- enable overlap of I/O with other actives
- avoid blocking progress while waiting for I/O operations
- Threads
- share an address space
- sharing data easy nad fast
- do not provide isolation, one thread bring the entire process down
- created faster
- incur in less overhead when switching between threads
- share an address space
- Processes
- enable applications to be paralleled
- provide isolated execution environments
- incur in interprocess communication overhead
a POSIX standard API for thread creation and synchronisation
