Title: Slides created by:
1Operating Systems
- Allow the processor to perform several tasks at
virtually the same timeEx. Web Controlled Car
with a camera - Car is controlled via the internet
- Car has its own webserver (http//mycar/)
- Web interface allows user to control car and see
camera images - Car also has auto brake feature to avoid
collisions
Web interface view
2Multiple Tasks
- Assume that one microcontroller is being used
- At least four different tasks must be performed
- Send video data - This is continuous while a user
is connected - Service motion buttons - Whenever button is
pressed, may last seconds - Detect obstacles - This is continuous at all
times - Auto brake - Whenever obstacle is detected, may
last seconds - Detect and Auto brake cannot occur together
- 3 tasks may need to occur concurrently
3Prioritized Task Scheduling
- Sending Video Data and Detecting Obstacles must
happen concurrently - Both tasks never complete
- Servicing Motion Buttons must be concurrent with
Sending Video Data - Video should not stop when car moves
- CPU must switch between tasks quickly
- Some tasks must take priority
- Auto Brake must have highest priority
4Sharing Global Resources
- Global resources may be required by mulitple
tasks - ADC, comparators, timers, I/O pins
- Shared access must be controlled to avoid
interference - Ex. Task 1 and Task 2 need to use the ADC
- They cannot use the ADC at the same time
- One task must wait for the other
- Operating system guarantees that resource
conflicts are resolved
5Layered OS Architecture
- OS provides an abstraction to hide details of
hardware - Ex. delay(int) library function might setup a
timer-based interrupt - Using Library functions incurrs overhead
6Processes vs. Threads
- Context of a task is its register values, program
counter, and stack - All tasks have their own context
- Context switch is when on task stops and the next
starts - - Must save the old context and load the new
- - This is time consuming
- OS typically gives tasks access to memory (i.e
malloc) - Processes each have their own private memory
- - Requires memory protection
- Threads share memory
- RTOS usually implement tasks as threads
7Memory Management
- Programs can request memory dynamically with
malloc()
int valarr10
int valarr valarr (int ) malloc(10
sizeof(int))
- Dynamically allocated memory must be explicitly
released - - statically allocated memory is released on
function return
free(valarr)
- Dynamic memory allocation is flexible but harder
to deal with - - Must free the memory manually
- - Cannot access freed memory
8OS Memory Management
- A program cannot know the dynamic memory
allocation - - Which memory locations are used and which are
available? - Operating system keeps tables describing which
memory locations are available - The program must request memory from the OS
- - OS may deny request if there is no memory
available - OS also protects memory
- - Enforce memory access permissions
9Scheduler
- OS manages the execution state of each task
- 3 main states
- 1. Running The task is currently running
- 2. Ready The task is not running but it is
ready to run - 3. Blocked The task is not ready because it is
waiting for an event - Only one task can be running at a time
- A task can only run if it is first ready (not
blocked) - Scheduler must keep track of the state of each
task - Scheduler must decide which ready task should run
10Preemption
- A non-preemptive scheduler allows a task to run
until it gives up control of the CPU - - Task may call a library function (sleep) to
quit - - Needs to be awakened by an event, like an
interrupt - - Not much flexibility for OS to meet deadlines
- A preemptive scheduler allows the OS to stop a
running task and start another task - - OS has the power to influence the completion
of tasks - - OS must be awakened periodically to make
scheduling decisions - - May implement the OS kernel as a high priority
timer-based interrupt
11Scheduling Algorithms
- Round-Robin
- Scheduler keeps an ordered list of ready tasks
- First task is assigned a fixed-size time slice to
execute - After time slice is done, task is placed at the
end of the list and next task executes for its
time slice - Very simple, no priorities
Task 1
Task 2
12Prioritized Scheduling
- Fixed Priority Preemptive
- Scheduler keeps an ordered list of ready tasks,
ordered by priority - First task is assigned a fixed-size time slice to
execute - After time slice is done, scheduler chooses
highest priority ready task for next time slice - Next task might be the same as the previous task,
if it is high priority
13Atomic Updates
- Tasks may need to share global data and resources
- For some data, updates must be performed together
to make sense - Ex. Our system samples the level of water in a
tank - tank_level is level of water
- time_updated is last update time
- tank_level // Result of computation
- time_updated // Current time
- These updates must occur together for the data to
be consistent - Interrupt could see new tank_level with old
time_updated
14Mutual Exclusion
- While one task updates the shared variables,
another task cannot read them
Task 1
Task 2
tank_level ? time_updated ?
printf (i i, tank_level, time_updated)
- Two code segments should be mutually exclusive
- If Task 2 is an interrupt, it must be disabled
15Semaphores
- A semaphore is a flag which indicates that
execution is safe - May be implemented as a binary variable, 1
continue, 0 wait
TakeSemaphore() If semaphore is available (1)
then take it (set to 0) and continue If semaphore
is note available (0) then block until it is
available ReleaseSemaphore() Set semaphore to 1
so that another task can take it
- Only one task can have a semaphore at one time
16Critical Regions
Task 1
Task 2
TakeSemaphore() tank_level ? time_updated
? ReleaseSemaphore()
TakeSemaphore() printf (i i, tank_level,
time_updated) ReleaseSemaphore()
- Semaphores are used to protect critical regions
- Two critical regions sharing a semaphore are
mutually exclusive - Each critical region is atomic, cannot be
separated