Tutorial on Operating Systems
Scheduling
-Virtually all OS's use a process to abstract a running
program.
-The OS scheduler is to fairly share the computer's resources
(primarily time slices running on 1 or more cpus, and memory).
-Process model: Many applications exhibit a two-state behavior
(on/off) :
- ON: Executes the program that is in memory until: the
process blocks for I/O, performs a system call, the process
encounters an error, the process ran for its allowed time
slice.
- OFF: The process is likely to be waiting on an
asynchronous event: I/O, a signal, OR waiting for its next
time slice
- Issue a 'ps aux | less' and look at the STAT column. The
possible states of a process in Ubuntu 16.04 :
- Here are the different values that the s, stat and state
output specifiers (header
"STAT" or "S") will display
to describe the state of a process:
D uninterruptible sleep (usually IO)
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to
complete)
T stopped by job control signal
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but
not reaped by its parent
-Long history
- Non-premptive: Allows a process burst to complete before
selecting the next process.
- Process selection:
- First come first serve
- Shortest job first
- Premptive: Interupts a process, selects the next process to
run, performs a context switch
- Processes run based on a quantum which determines the
timeslice
- Priority Scheduling: A run queue for each possible
priority.
- Algorithm serves the highest priority queue that has a
process ready to run.
- For the highest pri queue, select the first process on
the queue
- calculate its quantum and let it run
- when the time is up, place the process on the expired
list
-Linux Scheduling- Basic Algorithm
- 140 levels of priority requiring 140 separate queues (can be
configured however)
- Two sets of queues- active and expired
- Priorities 0-99 for 'soft real-time ' processes and priorities
100-139 for normal processes
- 'Nice' call sets the priority level of a process
- Calculating timeslices using Static Priority (SP) but biases
higher priority processes
- If SP<120, Quantum= (140-SP)x20
- If SP>=120, Quantum=(140-SP)x5
- Typical Quanta:
- Highest static Pri: (static Pri=100) Niceness: -20;
Quantum: 800ms
- High Static Pri: (static Pri 110) Niceness: -10; Quantum:
600ms
- Normal: (static Pri 120) Niceness: 0; Quantum: 100ms
- Low Static Pri: (static Pri 130) Niceness: +10; Quantum:
50ms
- Lowest Static Pri: (static Pri 139) Niceness:+20: ;
Quantum: 5ms
System Timers AND Kernel Timer Ticks
- Timer type:
- Real-time clock- battery backed on computer. Typically
read only at boot time....or in situations where an accurate
time is required (e.g., sudo hwclock -r )
- Time stamp counter: ticks once per CPU clock. Very
accurate interval timing!
- Programmable Internet Timer: generates periodic
interrupts.
- On Linux, the rate is call HZ and is might be 1000 HZ
(or 100 HZ on slower machines)
- A number of special but less commonly used timers
- Linux uses the best timer available to setup a timer tick
- The kernel executes a number of operations each tick
including
- Keeping track of time
- Invoking dynamic timer routines
- Calling scheduler_tick()
- Each timer tick, a variable called jiffies is incremented
- Tracks the number of HZ since the last system boot
- A 32 bit counter incremented at 1000 Hz wraps in about
50 days
- Ugly hack to use 64 bit (32 bit machines can't access
atomically)
- jiffies_64 with the lower 32 bits accessed by
jiffies (spin lock is used to sync access)
- The time of day is stored in the kernel structure
xtime which is a struct timespec
- Incremented once per tick
- Tick rate can be adjusted slightly using adjtimex()
- HiResolution Timer: If hardware and kernel support high
resolution timer, certain timing functions will be more
accurate. Further information is here: http://elinux.org/High_Resolution_Timers
- The tool cyclictest
(sudo apt-get install rt-tools) implements tests to
determine CPU cycle timing and system timing accuracy.
Schedulers have fairness issues when operating on multi-CPU
computers.
- New approach: Completely Fair Scheduler (CFS)
- Replaces time slice with virtual run time
- Based on the theoretic Generalized Processor Sharing (GPS)
model, but dynamically adjusts details of the quantum
- Introduces a target scheduling latency (TSL) to track
minimum task runtime.
- Introduces minimum granularity to stretch the TSL
depending on the number of running tasks- to ensure the
vruntimes might shrink towards 0 which decreases efficiency
(due to added overhead of more frequent context switches)
Scheduling at a higher level - jobs
- A job corresponds to running a program or a script. usually in
a batch-mode manner.
- The 'cron' commands are used to 'schedule jobs' to
run a specific times.
- To setup a cron job:
- To edit your crontab:
- crontab -e
- In vi, add a line : "45 11 * * * myScript.sh" will
run the script file at 11:45 am every day
- To list your crontab file:
- To remove your crontab
Peaking, probing and playing.....
- watch 'cat /proc/timer_list | grep jiffies'
- sudo hwclock -r
- After installing cyclictest,
- sudo cyclictest -t1 -p 80 -i 10000 -l 10000
Last update: 2/7/2018