1
Operating Systems – 2009/09 © Gerhard Fohler, 2008 1
Operating Systems processes and threads
Gerhard Fohler rts.eit.uni-kl.de/os
Operating Systems – 2009/09 © Gerhard Fohler, 2008 2
• terms and concepts • states of processes • control structures in OS • processes – threads – OS
2
Operating Systems – 2009/09 © Gerhard Fohler, 2008 3
Process
• executable program • used data (variables, etc.) • context
– current state of processor • PC, registers, …
– data for process handling • priority, …
Operating Systems – 2009/09 © Gerhard Fohler, 2008 4
Trace
• characterizes behavior of process • sequence of instructions which are executed for
process • overlapping of traces of different processes
characterizes process behavior
3
Operating Systems – 2009/09 © Gerhard Fohler, 2008 5
Traces
a+0 a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9
b+0 b+1 b+2
c+0 c+1 c+2 c+3 c+4 c+5 c+6
processes
A B C
dispatcher
d+0 d+1 d+2 d+3 (I/O)
T I/O
… Timeout … I/O Request combined trace
a+0 a+1 a+2 a+3 a+4
d+0 d+1 d+2 d+3 b+0 b+1 b+2
T
I/O
d+0 d+1 d+2 d+3 c+0 c+1 c+2 c+3 c+4
d+0 d+1 d+2
T
d+3 a+5 a+6 a+7 a+8 a+9
d+0 d+1 d+2 d+3 c+5 c+6
T
Operating Systems – 2009/09 © Gerhard Fohler, 2008 6
simple process modell
not running running
enter exit dispatch
pause
CPU dispatch
pause
enter exit queue
4
Operating Systems – 2009/09 © Gerhard Fohler, 2008 7
OS and Processes
• OS controls execution of processes execution patterns, resource allocation
• representation of processes has to support OS – state of process – memory used by process – maintenance of processes in (state) queues
Operating Systems – 2009/09 © Gerhard Fohler, 2008 8
Creation of Processes
OS builds data structures and allocates necessary memory
when is a process created? • login of interactive user • OS: performing a service • creation by user process
– process spawning, parent, child
5
Operating Systems – 2009/09 © Gerhard Fohler, 2008 9
Termination of processes
process indicates termination • Logout by user • service request to OS • ocurrence of fault during execution of process • halt-instruction
Operating Systems – 2009/09 © Gerhard Fohler, 2008 10
Reasons for Termination
• program done • exceeded time limit • insufficient memory • memory access
violation • unauthorized access • arithmetic fault • exceeded waiting time • I/O fauolt
• invalid instruction • priviledged instruction • operator or OS
intervention • termination of/by parent
process
6
Operating Systems – 2009/09 © Gerhard Fohler, 2008 11
Major Process States
• running – process has CPU and is executing
• ready – process ist ready to execute (but waits for access
to CPU) • blocked
– process waiting for event, e.g., completion of I/O, not ready to run
Operating Systems – 2009/09 © Gerhard Fohler, 2008 12
Major Process States
• New – OS has created process
• processnummer (process identifier) • tables and entries for process control
– but process not yet ready to run • avoid resource overload due to too many
processes
7
Operating Systems – 2009/09 © Gerhard Fohler, 2008 13
Major Process States
• Exit – termination – process no longer executed – process information (tables) maybe used by
auxiliary programes, e.g., accounting – process information and tables deleted if no longer
needed
Operating Systems – 2009/09 © Gerhard Fohler, 2008 14
5 state process model
running dispatch
ready new exit
blocked
timeout event
occurs event wait
admit release
8
Operating Systems – 2009/09 © Gerhard Fohler, 2008 15
queuing model
CPU dispatch
timeout
admit release ready queue
event 1 queue
event n queue
event 1 wait
event n wait
event 1 occurs
event n occurs
Operating Systems – 2009/09 © Gerhard Fohler, 2008 16
Process Switch
• switching of running process • possible when OS has CPU:
– Supervisor Call explicit call by program, e.g., I/O
– Trap error
– Interrupt cause outside process, control given to interrupt handler and OS
9
Operating Systems – 2009/09 © Gerhard Fohler, 2008 17
Swapping
• too many processes in main memory lead to decreased performance
• approach: Swapping = move processes to secondary storage (and move back again to main memory)
• in OS: two new process states and queues – ready, suspend – blocked, suspend
Operating Systems – 2009/09 © Gerhard Fohler, 2008 18
process states with suspend
running dispatch
ready exit
blocked
timeout
event occurs event
wait
release
new admit
ready, suspend
admit
blocked, suspend
event occurs
activate
suspend
activate
suspend
suspend
10
Operating Systems – 2009/09 © Gerhard Fohler, 2008 19
Causes of Suspend
• Swapping • BS moves process to secondary storage, e.g.,
background process, utility, process with problems • interactive request, e.g., debugging • timing: periodic process can be move out in between
activations
Operating Systems – 2009/09 © Gerhard Fohler, 2008 20
• terms and concepts • states of processes • control structures in OS • processes – threads – OS
11
Operating Systems – 2009/09 © Gerhard Fohler, 2008 21
Control Structures for Process Management in OS
Operating Systems – 2009/09 © Gerhard Fohler, 2008 22
Control structures in OS
OS manages the following tables for processes and resource
• memory tables • I/O tables for devices and channels • file tables • process tables
12
Operating Systems – 2009/09 © Gerhard Fohler, 2008 23
Process Image
• user program • user data
– modifiable part of user space (daten, user stack, modifiable programs)
• system stack – parameters und calling adr. of system calls
• process control block (PCB) – execution context – process identification, processor state information,
process control information
Operating Systems – 2009/09 © Gerhard Fohler, 2008 24
Process Image process id.
processor state Information
process control information
user stack
private user address space
shared address space
as if program was alone
• program as seen from OS
13
Operating Systems – 2009/09 © Gerhard Fohler, 2008 25
Process Image
Process Control Block (PCB)
• is virtual memory – not necessarily
continuous • Primary Process Table
pointer to process identifier (PI)
• OS needs parts of image in main memory for process managment
process id.
processor state Information
process control information
user stack
private user address space
shared address space
Operating Systems – 2009/09 © Gerhard Fohler, 2008 26
PCB: Process Identification
• unique process nummer (proc. identifier) = index in primary process table
• user identifier – user who owns process
• Parent Process Identifier – number of process who generated
this process
process id.
processor state Information
process control information
user stack
private user address space
shared address space
14
Operating Systems – 2009/09 © Gerhard Fohler, 2008 27
PCB: Processor State Information
• register contents • control- and statusregister
– program counter – program status word (PSW)
• information on control, modus, status-bits
• Stack Pointers
process id.
processor state Information
process control information
user stack
private user address space
shared address space
Operating Systems – 2009/09 © Gerhard Fohler, 2008 28
PCB: Process Control Information
• scheduling and state information – current state of process – priority and scheduling information – event, for which process waits
• pointers to other processes – process-queues – parent, child, …
process id.
processor state Information
process control information
user stack
private user address space
shared address space
15
Operating Systems – 2009/09 © Gerhard Fohler, 2008 29
PCB: Process Control Information
• Interprocesscommunikation (IPC) – flags, signale, pointers to messages
• privileges • memory management
– pointers to segment or page tables • ressources
– used: open files, I/O devices – already consumed: CPU time, I/O,
etc.
process id.
processor state Information
process control information
user stack
private user address space
shared address space
Operating Systems – 2009/09 © Gerhard Fohler, 2008 30
Execution Modes
• at least two execution modes in order to protect datastructures of OS (e.g., PCB) – privileged mode (= system mode, kernel mode,
supervisor mode, control mode) access to control registers, …
– User Mode • mode switch in routines of OS • execution modes supported by mode-bits of CPU
16
Operating Systems – 2009/09 © Gerhard Fohler, 2008 31
Mode Switch vs. Process Switch
• different concepts • mode switch basis for process switsch • not every mode switch results in process switch
user mode
mode switch
process switch
call, trap, interrupt
save/restore processor context
update PCB (state) PCB ⇒ queue select new process update PCB update MM data
priv. mode
?
Operating Systems – 2009/09 © Gerhard Fohler, 2008 32
• terms and concepts • states of processes • control structures in OS • processes – threads – OS
17
Operating Systems – 2009/09 © Gerhard Fohler, 2008 33
Processes in Operating System
Operating Systems – 2009/09 © Gerhard Fohler, 2008 34
OS and Processes
different approaches to implement OS 1. strict separation of OS kernel and processes 2. OS executes inside user processes 3. process based OS
18
Operating Systems – 2009/09 © Gerhard Fohler, 2008 35
Processes and Kernel decoupled
• concept of process only for user programs • OS operates separated from processes in
priviledged mode
Kernel
P1 P2 Pn ...
Operating Systems – 2009/09 © Gerhard Fohler, 2008 36
OS execution in processes
• OS is collection of routines • almost all OS routines executed in process context
after mode switch
Process Switching
P1 P2 Pn ... OS OS OS
19
Operating Systems – 2009/09 © Gerhard Fohler, 2008 37
OS execution in Processes
• OS code and Data in shared address space abgelegt
• separate kernel stack for kernel mode
• user program as well as OS routines running in process
user stack
private user address space
shared address space
PCB
kernel stack
Operating Systems – 2009/09 © Gerhard Fohler, 2008 38
Process based OS
• OS is collection of system processes • OS provides only basic services (process switching;
memory managment, IPC, interrupts und I/O) not separate process
• Process Switching? ⇒ seeThreads
Process Switching
U1 U2 Un ... OS1 OSn ...
20
Operating Systems – 2009/09 © Gerhard Fohler, 2008 39
Summary Processes
• process is central concept of OS • OS creates, manages, and terminates processes • process going through number of states
(ready, running, blocked, suspend, ...) • data structures for process management
– process table – process image: used memory space,
PCB: state, resources, priority, etc.
Operating Systems – 2009/09 © Gerhard Fohler, 2008 40
Summary Processes
• mode switch between user/kernel Mode – interrupt, trap or supervisor Call
• approaches to implement OS and processes – strict separation – OS functions in user processes – kernel processes for execution of OS functions
21
Operating Systems – 2009/09 © Gerhard Fohler, 2008 41
Threads
Operating Systems – 2009/09 © Gerhard Fohler, 2008 42
Motivation Threads
• processes are units for both 1. resource management 2. dispatching (starting, preempting, terminating
tasks, see later) -> processes are big, overheads • decouple (1) and (2):
– Process (Task): unit for resource management – Thread (Lightweight Process): unit for
dispatching, small overhead • Multithreading: n > 1 Threads per process
22
Operating Systems – 2009/09 © Gerhard Fohler, 2008 43
Processes and Threads
• process – virtual addressspace with process image – memory protection, files, I/O, resources
• Thread – state (running, ready, …) – context – Stack – thread-locale variables
Operating Systems – 2009/09 © Gerhard Fohler, 2008 44
Single-threaded Process Model
Process Control Block
User Address Space
User Stack
Kernel Stack
Process
23
Operating Systems – 2009/09 © Gerhard Fohler, 2008 45
Multi-threaded Process Model
Process Control Block
User Address Space
Thread Control Block
User Stack
Kernel Stack
Thread Thread Control Block
User Stack
Kernel Stack
Thread Thread Control Block
User Stack
Kernel Stack
Thread
Operating Systems – 2009/09 © Gerhard Fohler, 2008 46
Thread vs. process
• thread creation takes less time • switching between threads less overhead, time than
process switch • terminating threads takes less time • communication between threads of a process can be
done without bothering kernel (careful with synchronization)
24
Operating Systems – 2009/09 © Gerhard Fohler, 2008 47
Use of von Threads
(small) applications working together/on larger application
• e.g., file server in LAN – high number of requests in short time – process data/resources the same – one thread per request
• e.g., Spreadsheet – one thread to display menus and read input – one thread for calculations – one thread for updates…
Operating Systems – 2009/09 © Gerhard Fohler, 2008 48
States of Threads
• basic states: running, ready, blocked • Suspend not for single threads – all threads of a
process have access to same adress space • terminating a process terminates all its threads • who is blocking?
– single thread? – whole process?
25
Operating Systems – 2009/09 © Gerhard Fohler, 2008 49
User-Level Threads (ULT)
• threads not visible to kernel • thread anagement via thread
library • thread switching in user mode
(mode switch not needed) • application specific scheduling
Threads
P
user Space
kernel space
Lib.
threads
Operating Systems – 2009/09 © Gerhard Fohler, 2008 50
Threads Library
• contains code for – creation and termination of threads – data and message exchange among threads – thread scheduling – saving and restoring thread context
• blocking system call blocks all ULTs of a process • no distribution to several processors
26
Operating Systems – 2009/09 © Gerhard Fohler, 2008 51
Kernel-Level Threads (KLT)
• thread management by Kernel
• kernel thread API, no Library
• thread switching done by Kernel
• scheduling of threads
P
user space
kernel space
threads
Operating Systems – 2009/09 © Gerhard Fohler, 2008 52
Kernel-Level Threads
• thread wise blocking • kernel - routines multi-threaded • can schedule several threads of a process (multi
processor, multi core) • thread switching within process via needs 2 Mode
Switches ⇒ slower than ULTs
⇒ e.g. combined ULT/KLT
27
Operating Systems – 2009/09 © Gerhard Fohler, 2008 53
Summary Threads
• thread: unit for dispatching • several threads per process possible • faster creationg and switching than with processes • implementations
– user-level threads – kernel-level threads
Operating Systems – 2009/09 © Gerhard Fohler, 2008 54
Ausblick
• processes, threads • par, Synchronisation (Mutual Exclusion) • Deadlock • Speicherverwaltung (Virtual Memory) • Scheduling • Ein- und Ausgabe • Dateiverwaltung
Top Related