Title: Processes and Scheduling
1Processes and Scheduling
- A Process model
- Program counter Registers Variables
- Processes must be timing independent
- Different views of processes
A
4 Program Counters
D
B
C
C
B
A
B
D
C
D
A
Time
One Program Counter
2Process States
- Running - actually using the cpu
- Ready - temporarily stopped to let another
process run - Blocked - unable to run until some external event
happens - A process can block itself, but not run itself
3Process State Transitions
- 1. Process blocks for input
- 2. Scheduler switches to another process
- 3. Scheduler switches back to this process
- 4. Input becomes available
Running
1
2
3
Blocked
Ready
4
4Five-State Process Model
Dispatch
Release
Admit
New
Ready
Running
Exit
Time-out
Event Wait
Event Occurs
Blocked
5Single Blocked Queue
Ready Queue
Release
Dispatch
Admit
Processor
Time-out
Event Wait
Event Occurs
Blocked Queue
6Multiple Blocked Queues
Ready Queue
Release
Dispatch
Admit
Processor
Time-out
7Queueing and Scheduling Processes
8Suspended Processes
- Processor is faster than I/O so all processes
could be waiting for I/O - Swap these processes to disk to free up more
memory - Blocked state becomes suspend state when swapped
to disk - Two new states
- Blocked, suspended
- Ready, suspended
9Process State Transition Diagram with Two Suspend
States
New
Admit
Suspend
Admit
Dispatch
Activate
Ready, suspend
Ready
Running
Exit
Time out
Suspend
Event Wait
Event Occurs
Event Occurs
Activate
Blocked, suspend
Blocked
10Operating System - Main Goals
- Interleave the execution of the number of
processes to maximize processor utilization while
providing reasonable response time - Allocate resources to processes
- Support inter-process communication and user
creation of processes
11How to achieve Goals ?
- Schedule and dispatch processes for execution by
the processor - Implement a safe and fair policy for resource
Allocation to processes - Respond to requests by user programs
- Construct and maintain Tables for each entity
managed by the operating system
12System Tables
13Memory Tables
- Allocation of main memory to processes
- Allocation of secondary memory to processes
- Protection attributes for access to shared memory
regions - Information needed to manage virtual memory
14I/O Tables
- Status of I/O device - is it available or is it
assigned ? - If assigned, then to which process is it assigned
- Status of I/O operation
- Location in main memory being used as the source
or destination of the I/O transfer
15File Tables
- Existence of files, the structure of the file
tree, links, etc. - Location on secondary memory (possibly on
different machines..) - Current Status (opened, shared, etc..)
- Attributes
- Sometimes this information is maintained by a
file-management system
16Process Table
- Process image consists of program, data, stack,
and attributes - Control Attributes form the
- Process Control Block - PCB
- Unique ID (may be an index into the PT)
- User ID Parent process ID
- process control information
- Processor state information
17Process Control Information
- Additional information needed by the operating
system to control and coordinate the various
active processes - scheduling and state information - state
priority event scheduling info. - data structuring - linked queues children
- inter-process communication - signals mesgs.
- process privileges - memory granted mode
- memory management - ptrs to segments
- resource ownership and utilization - open files
18Processor State Information
- Contents of processor registers
- User-visible registers
- Control and status registers (PSW)
- program counter
- condition codes
- status register - interrupts disabled/enabled
- Stack pointers - user and system stacks
19Process Control Block
20Managing Processes - the Table
- Process table - arrays of structures
21Process Management - Operations
- Process creation and termination
- Process scheduling and dispatching
- Process switching
- Process synchronization and support for
inter-process communication - Management of process control blocks
22 and in the 1st Assignment ..
- Thread creation, termination and switching
- Thread scheduling and switching
- Inter-thread communication via mail messages
- Management of the threads table (TT)
- Thread synchronization by the use of
- lock
- unlock
23Process Creation
- Assign a unique process identifier
- Allocate space for the process
- Initialize process control block
- Set up appropriate linkages
- to scheduling queue...
- Processes are created by parent processes
- Parent may terminate children processes
- Parent and offspring can cooperate by sharing
resources/information
24The Unix Process
- Each process is a single thread of control
- fork system call
- memory address space is copied
- parent receives pid of child (value of fork())
- child gets 0 (value of fork())
- pid fork() / upon success of fork() pid gt
0 in parent / - if (pid lt 0) / fork failed - memory
full ... table full / - else if (pid gt 0) / Parent code
goes here ... / - else / Child code goes here ...
/ -
- to find own pid - getpid()
25Processing in Unix
- init process forks a tree of processes
- login process for each terminal
- daemons for handling mail queue printers paging
management cron ...
26A Hierarchy of Processes
- Parent process creates children processes,
forming a tree of processes - Parent and child process share resources
- Parent and children execute concurrently
- Children have duplicate address spaces of their
parents - In Unix
- the fork() system call creates a new process
- execve system call (used after fork() ) replaces
the process core image with that of another
executable program
27Managing Processes (Unix)
- pid fork()
- waitpid(pid, status, opts)
- wait for termination of child, get exit status,
block or terminate... - execve(name, argv, envp)
- replace core by name, with arguments argv
- exit(status)
- sigaction(sig, act, oact)
- perform handler act, on signal sig...
- kill(pid, sig)
- alarm(seconds)
- pause()
28a trivial Shell
- while(1)
- type_prompt()
- read_command(command, params) / input from
terminal / - pid fork()
- if(pid lt 0)
- printf(Unable to fork)
- continue
-
- if (pid ! 0)
- waitpid(-1, status, 0) / parent code...
/ -
- else
- execve(command, params, 0) / child code ...
/ -
-
29Signals among Unix processes
- Processes can specify actions to be taken upon
receipt of a signal - Signals are generated by users (terminals)
program errors or other processes - signal(signum, function 1 0 )
- Default (0) is to exit
- Two other options is ignore or execute function
- Processes can only send signals to their process
group - ancestors siblings and descendants - Signals behave like software interrupts
- send signals by kill(pid, signum) - to specific
process, or to the group, or to uid processes
30Signals among Unix processes (II)
- kernel sets bits of signals in the PTE upon
receiving of signals - Some Examples
- sigabrt - abort process (core dump)
- sigalrm - alarm clock
- sigsegv - segmentation violation (invalid
address) - sighup - phone line hangup
- sigill - illegal instruction
- a child process sends signal to parent upon
termination and if parent executes wait(), it
gets the exit code too
31Processes privileges
- uid and gid determine privileges
- Effective uid (gid) can be set by a special bit
of the executable file that sets the effective
uid to be that of the owner of the file - Enables a controlled privileged access to system
protected resources - df mail login ... - terminated processes are zombies - just PTEs
with no resources - zombie entries can be erased by the kernel when
executing a wait() system call of an ancestor
32When to Switch a Process
- Interrupts
- Clock
- process has executed a full time-slice
- I/O
- Memory fault
- memory address is in virtual memory
- Trap
- error occurred
- ... file open...
33Switching Processes
34Change of Process State
- Save context of processor including program
counter and other registers - Update the process control block with the new
state and any accounting information - Move process control block to appropriate queue
- ready, blocked - Select another process for execution
35Change of Process State - II
- Update the process control block of the process
selected - Update memory-management data structures
- Restore context of the selected process
36A view of scheduling processes
- The Scheduler - lowest operating system layer
- Hides interrupt handling and details of
starting/stopping processes - The rest of the OS can be structured in process
form
Processes
. . . . .
0
1
n-2
n-1
Scheduler
37Kernel vs. User Processes
38 and in the 1st Assignment ..
- The Scheduler is implemented as the main()
program (not a thread) - The scheduler manages the TT - scheduling and
switching - Interrupt handling is simulated by calls to
functions like suspend() release(), from the
thread library - The Mailer is in thread form, in charge of all
operations on the queue of messages
39Process running and a Disk Interrupt occurs...
- Hardware
- Stacks program counter status word. Register/s
- Loads new program counter from Interrupt Vector
- Assembly/Machine language procedure
- Saves all registers of current process
- Sets up new stack for interrupt information
- C procedure
- switches the requesting procedure to ready
- Scheduler selects process to run (policy)
- Assembly/Machine language procedure starts
current process
40Synchronous vs. Asynchronous I/o
41Processes Threads
- Resource ownership - process is allocated a
virtual address space to hold the process image - Scheduling - process is an execution path
through one or more programs - execution may be interleaved with other processes
- These two characteristics are treated
independently by the operating system
42Processes Threads
- Dispatching is referred to as a thread
- Ownership of resources is referred to as a
process or task - Multithreading
- Operating systems support multiple threads of
execution within a single process - (classical) UNIX supports multiple user
processes but only one thread per process - Windows NT supports multiple threads
43Processes Threads
- The basic unit of cpu utilization - threads
- program counter register set stack space
- Peer threads share resources like code section
and data section - a process is a task with one thread
- multi-threaded tasks (processes) can have one
thread running while another is blocked - good for applications that require sharing a
common buffer by server threads
44Threads and Processes
one process one thread
one process multiple threads
multiple processes one thread per process
multiple processes multiple threads per process
45Threads and Processes
- Processes
- Have a virtual address space which holds the
process image - Protected access to files, I/O resources,
processors, and other processes - Threads
- Have an execution state (running, ready...)
- Have an execution stack
- Thread context is saved, when not running
46More on Threads
- Per-thread static storage for local variables
- Access to memory and resources of process
- all threads of a process share these
- Suspending a process suspends all threads of the
process since all threads share the same PTE - Termination of a process, terminates all threads
within the process
47Single Threaded and Multithreaded Process Models
Multithreaded Process Model
Single-Threaded Process Model
Thread
Thread
Thread
Thread Control Block
Thread Control Block
Thread Control Block
User Stack
Process Control Block
User Stack
Process Control Block
User Stack
User Stack
Kernel Stack
User Address Space
Kernel Stack
User Address Space
Kernel Stack
Kernel Stack
48Benefits of Threads
- Takes less time to create a new thread than a
process - Less time to terminate a thread than a process
- Less time to switch between two threads within
the same process - Threads within the same process share memory and
files --gt they can communicate without invoking
the kernel
49User-Level Threads
- All thread management is done by the application
- The kernel is not aware of the existence of
threads - Thread switching does not require kernel mode
privileges - Scheduling is application specific
- System calls by threads block the process
50Kernel-level Threads (Windows NT)
- Kernel maintains context information for the
process and the threads - Kernel can schedule different threads of the
same process to different processors - Switching between threads requires the kernel
- Kernel threads can simplify context switch of
system functions
51Combined Approaches for Threads (Solaris)
- Thread creation, scheduling and synchronization
done in user space - Multiple user-level threads are mapped onto some
(smaller or equal) number of kernel-level threads - Programmer may adjust the number of KLTs (LWP)
for a particular application - Multiple threads may run on multiple processors
- including kernel threads...
52Threads in Solaris
53Relationship Between Threads and Processes
ThreadsProcess
Description
Example Systems
Most UNIX implementations
11
Each thread of execution is a unique process with
its own address space and resources.
M1
A process defines an address space and dynamic
resource ownership. Multiple threads may be
created and executed within that process.
Windows NT, Solaris, OS/2, OS/390, MACH
54Threads in the 1st Assignment ..
- Run mostly in user space (obviously), but, the
running thread is a LWP (x2) - Blocking and suspending threads is controlled by
main() via the linked module - When a thread of execution is blocked (by the
Unix OS), the whole process is blocked - Thread control functions are performed by calls
to functions from the Solaris thread library
55Threads LWP Structure
Threads
Threads library
LWPs
Kernel - OS Scheduler
56LWPs are managed by the OS scheduler
- Threads library operations
- Manages the size of the LWP pool
- thr_setconcurrency(poolSize).
- thr_getconcurrency().
- Constructs new threads.
- thr_create(stack, stackSize, func, arg,
attributes, tid). - Attaches threads and LWPs by the following
rules - A thread runs until it is suspended, or a thread
with higher priority is created, then it frees
the LWP. - Once a LWP becomes free a thread with highest
priority is attached to it (Or main())
57Time Quantum for Threads
- Problem the threads library does not include
timed threads. - Solution a counter thread
- When the user wants to run a thread, a new
thread is created with attributes - THR_BOUND
(bound to a LWP until it exits). - The counter is busy-waiting for the time the
original thread is supposed to be running, and
then suspends it.
58context switch
context switch
context switch
LWP1
Scheduler
Scheduler
Thread A
Thread B
Time
LWP2
Free
Timer1
Free
Timer2
Context switch is not done at the same time for
the two LWPs - the Scheduler first initializes
the new timer and only then continues the
thread, the timer first suspends the thread and
only then exits, but the Scheduler waits for
the timer to exit before it continues. Size of
LWP pool is always 2. The counter checks if the
thread is blocked all the time, and if so -
returns the time left for the thread to run and
exits. This timing method does not insures that
the thread will run the appropriate time - it
relies on the fairness of the OS Scheduler to run
both LWPs fairly.
59UNIX Process State Transition Diagram
fork
Created
Preempted
return to user
not enough memory (swapping system only)
enough memory
User Running
preempt
swap out
return
reschedule process
Ready to Run in Memory
Ready to Run Swapped
swap in
system call, interrupt
Kernel Running
sleep
wakeup
wakeup
interrupt, interrupt return
exit
swap out
Sleep, Swapped
Asleep in Memory
Zombie
60Interprocess Communication
- Message system - no need for shared variables
- Two operations send(msg) receive(msg)
- Processes who wish to communicate need to
- Establish a communication link
- Exchange messages via send() receive()
- Direct Communication
- Links connect exactly one pair of processes
- Links may be unidirectional or bidirectional
- Each pair of processes can have only one link
- explicit names - send(P,msg) receive(Q,msg)
61Interprocess Communication (II)
- Indirect Communication
- Messages are directed and received from
mailboxes (ports) - Each mailbox has a unique id
- Many processes can be connected by one shared
mailbox - Pairs of processes may share several links
- Operations
- create a mailbox - msgqid msgget(key, flag)
- send and receive messages through mailbox -
- msgsnd(msgqid, msg, cnt, ) count msgrcv(id,
max, , ) - destroy a mailbox
62Process Scheduling
- Quality criteria for scheduling (algorithm)
- Fairness each process gets a fair share of
the cpu - Efficiency keep the cpu busy
- Response time minimize, for interactive users
- Turnaround for batch users (total time of
batch) - Throughput maximal number of processed jobs per
unit time - Waiting time minimize the average over processes
63When are Scheduling Decisions made ?
- 1. Process switches from Running to Waiting
(I/o) - 2. Switch from Running to Ready (clock.. )
- 3. Process switches from Waiting to Ready
(I/o..) - 4. Process terminates
- Types of Scheduling
- Preemptive scheduling processes can be
suspended by scheduler - non-preemptive only 1. And 4.
64Operating System - Main Goals
- Interleave the execution of the number of
processes to maximize processor utilization while
providing reasonable response time - Overhead
- Context switch
- Switch to user mode
- Restart user program at right location
- Update resource use and assignment
- Observation processes tend to run in bursts of
cpu-I/o use...
65Utilization vs. Turnaround time
- 5 interactive jobs i1i5 and one batch job b
- Interactive jobs 10 cpu 20 disk I/o 70
terminal I/o total time for each job 10 sec. - Batch job 90 cpu 10 disk I/o total time 50
sec. - Cannot run all in parallel !!
- i1..i5 in parallel - disk I/o is 100 utilized
- b and on i in parallel - cpu is 100 utilized
66Utilization vs. Turnaround time (II)
- Two possible schedules
- 1. First i1i5, then b
- UT (10x50 50x90)/60 83
- TA (10x5 60x1)/6 18sec.
- 2. b and each of the is in parallel
- UT (50x100)/50 100
- TA (1020304050 50)/6 36sec.
67(No Transcript)
68(No Transcript)
69Shortest Job First
- If run times are known in advance...
5
2
4
1
571112
B
C
A
D
8.75
4
1
2
4
5
13712
A
5.75
D
B
C
4
and if they are not known they can be
approximated...
70(No Transcript)
71(No Transcript)
72Determining length of next CPU burst
- Can be done by using the length of previous cpu
bursts - tn actual length of nth cpu burst
- Tn1 predicted value for the next cpu burst
- for 0 ? ? ? 1 define
- Tn1 ? tn (1 - ? ) Tn
- Since both ? and (1 - ? ) are less than or equal
1, each successive term has less weight than its
predecessor
73Round Robin - the oldest method
- Each process gets a small unit of cpu time
(time-quantum), usually 10-100 milliseconds - For n ready processes and time-quantum q, no
process waits more than (n - 1)q - In the limit of very large q - FIFO
- Quantum of time gt switching time
- relatively large waste of cpu time (i.e.
switching) - Quantum of time gtgt switching time
- long response (waiting) time...
74(No Transcript)
75An Example
Service Time
Arrival Time
Process
1
0
3
2
2
6
3
4
4
4
6
5
5
8
2
76First-Come-First-Served (FCFS)
- Each process joins the Ready queue
- When the current process ceases to execute, the
oldest process in the Ready queue is selected
1
2
3
4
5
77Shortest Remaining Time - SRTF
- Preemptive version of shortest process next
policy - Must estimate processing time
78Highest Response Ratio Next (HRRN)
- Choose next process with the lowest ratio
1
2
3
4
5
time spent waiting expected service
time expected service time
79Feedback Scheduling
- Penalize jobs that have been running longer
- Dont know remaining time process needs to execute
80 Comment Guaranteed Scheduling
- To achieve guaranteed 1/n of cpu time (for n
processes/users logged on) - Monitor the total amount of cpu time per process
and the total logged on time - Calculate the ratio of allocated cpu time to the
amount of cpu time each process is entitled to - Run the process with the lowest ratio
- Switch to another process when the ratio of the
running process has passed its goal ratio - also called Fair-share Scheduling
81Priority (Feedback) Scheduling
- Select runnable process with highest priority
- Prevent high priority processes from running
indefinitely, change priorities dynamically - decrease priorities at each clock tick
- increase priorities of i/o bound processes
- priority 1/f ( f is the fraction of
time-quantum used) - group priorities each priority group uses round
robin scheduling - main drawback - Starvation
82Dynamic Priority Groups - CTSS
- Assign different time quanta to the different
priority classes - Highest priority class 1 quantum, 2nd class 2
quanta, 3rd class 4 quanta, etc. - Move processes that used their time to lower
classes (i.e. classify running processes) - Net result - longer runs for cpu-bound
processes higher priority for I/o-bound
processes - for 100 time quanta - 7 switches
- Unix (low-level) scheduler ...
83Two-Level Scheduling
- Assume processes can be either in memory or
swapped out (to disk) - Schedule memory-resident processes by a low level
scheduler as before - Swap in/out by a high level scheduler using
- Elapsed time since process swapped in/out
- Allocated cpu time to process
- Process size (takes less space in memory)
- Process priority
84Scheduling in Unix
- Two-level scheduling
- Low level scheduler uses multiple queues to
select the next process, out of the processes in
memory, to get a time quantum. - High level scheduler moves processes from memory
to disk and back, to enable all processes their
share of cpu time - Low-level scheduler keeps queues for each
priority - User mode processes have positive priorities
- Kernel mode processes have negative priorities
(which are higher)
85Low-level Scheduling Algorithm
- Pick process from highest (non-empty) priority
queue - Run for 1 quantum (usually 1s), or until it
blocks - Increment cpu usage count every clock tick
- Every second, recalculate priorities
- Divide cpu usage by 2
- New priority base cpu usage
- (base is usually 0, but, can be niced to less...)
- Use round robin for each queue (separately)
86Unix priority queues
87Low-level Scheduling Algorithm - i/o
- Blocked processes removed from queue, but, when
blocking event occurs, placed in high priority
queue - The negative priorities are meant for (returning)
blocked processes - Negative priorities are hardwired in the system,
for example, -5 for Disk i/o is meant to enable a
reading/writing process to go back to the disk
without waiting for other processes - Interactive processes get good service, cpu bound
processes get whatever service is left...