Processes and Threads Flashcards
Motivation for Processes
Modern computer systems perform many tasks seemingly at the same time
- wait for user input
- network communication
- waiting for I/O requests to finish
- background computation
A process is an OS concept to seperate this complexity into units that are each executed sequentially
Program vs. Process
Programs are sets of instructions and data that describe how to execute a certain task
Processes are instances of running programs
- they have a current state associated with the “core image”
- state includes the processor registers and resources a process uses (e.g. opened file handles)
There may be multiple instances of the same program at the same time
-> multiple processes can execute the same program, but with different input data, at a different stage
The process model
- with only a single CPU core, only one process can be executed at a time
- multiple processes are executed in turn
- one local program counter for every process
- one global program counter (CPU)
- -> switched between local values when switching processes
Process Lifetime
Processes are created at/by:
- system initialization (bootup)
- in response to another process requsting its creation by the respective system call
- a user request to create the process
- batch job
Background processes are called services or daemons .
Processes are terminated by:
- normal exit
- error exit
- fatal error
- killed by another process
Process States
Running: using the CPU at this instance
Ready: runnable but temporarily stopped in favor of a running process
Blocked: waiting for external event (e.g. I/O interrupt)
(see Slide 9)
Processes = A Program in Execution (Definition)
A process holds all information required to run that program
- unique identifier for the process
- pointers to adress regions
- handles to additional resources
- current state of execution
- priority for scheduling
On UNIX systems:
- information about child processes
- child processes can be controlled by parents
- one root process for the whole OS to bootstrap startup of and control other processes
Context Switching
Switching from one process to another:
- setting the currently running process to blocked or ready
- saving its current state in process table
- selecting another ready process according to scheduling decision
- restoring the process context
- setting new process to running
Threads Motivation
Processes have their own address space. There may be multiple processes running the same program, but they do not share any internal state. In many cases, having multipe parallel tasks share one address space makes programming simpler: - reading data from keyboard - text formatting - saving data to disk - spelling checker - prepare printing
All running at the same time and not blocking each other.
Threads
Like processes, have their own execution state
- running, ready or blocked
- scheduling priority
- CPU registers
Similar motivation as processes:
- simpler programming model with sequential tasks
- higher CPU utilization
Classical thread model
Difference ro process is shared resources
- includes RAM, network sockets, file handles,…
- threads are sometimes called light-weight processes, because shared resources can lead to faster thread creation and thread switching
1 program has 0…n processes
1 process has shared resources + 1…m threads
Process is the owner of resorces and a kind of container for its threads. Threads use the resources of their process on a shared basis.
Process vs. Thread information
Per process items:
- Adress space
- global variables
- open files
- child processes
Per thread items:
- program counter
- register
- stack
- state
Threads execution stacks
Threads in a process share text and data areas
Each thread has its own stack
User Space Threads (FIBER)
- have thread tables in user space so the kernel does not need to switch contexts
- this makes fiber fast
- however the integration with system calls is difficult
IPC = Inter Process Communication Issues
3 basic issues for inter process communication
- passing information from one process to another
- ensuring that two or more processes do not conflict
- dependencies between processes (e.g. producer/consumer)
Same principles apply to threads:
- passing information is trivial because of shared memory
- conflict handling and sequencing apply equally to threads
Options for IPC medium
Files: in a shared file system (typical for processes to access the same file system )
Named Pipes: pseudo files without permanent storage but FIFO queue in kernel
Anonymous pipes: e.g. shell connecting stdout of one process to stdin of another
Client/server communication over sockets: server sockets listen for incoming connections, cklient sockets connect to them to form 1:1 link
shared memory regions