Download presentation
Presentation is loading. Please wait.
Published byJasmin Cunningham Modified over 8 years ago
1
Slides created by: Professor Ian G. Harris Operating Systems Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled Car with a camera Car is controlled via the internet Car has its own webserver (http://mycar/)http://mycar/ Web interface allows user to control car and see camera images Car also has “auto brake” feature to avoid collisions Fwd Back LeftRight Web interface view
2
Slides created by: Professor Ian G. Harris Multiple Tasks Assume that one microcontroller is being used At least four different tasks must be performed 1.Send video data - This is continuous while a user is connected 2.Service motion buttons - Whenever button is pressed, may last seconds 3.Detect obstacles - This is continuous at all times 4.Auto brake - Whenever obstacle is detected, may last seconds Detect and Auto brake cannot occur together 3 tasks may need to occur concurrently
3
Slides created by: Professor Ian G. Harris Prioritized 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
4
Slides created by: Professor Ian G. Harris Sharing 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
5
Slides created by: Professor Ian G. Harris Layered OS Architecture Microconrtoller Application Library Functions System Calls Application Microconrtoller 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
6
Slides created by: Professor Ian G. Harris Processes 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
7
Slides created by: Professor Ian G. Harris Memory Management Programs can request memory dynamically with malloc(); int valarr[10]; 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
8
Slides created by: Professor Ian G. Harris OS 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
9
Slides created by: Professor Ian G. Harris Scheduler 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
10
Slides created by: Professor Ian G. Harris Preemption 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
11
Slides created by: Professor Ian G. Harris Scheduling 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 Context switch time Task execution Task 1 Task 2
12
Slides created by: Professor Ian G. Harris Prioritized 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 Low priority High Priority Starvation may occur
13
Slides created by: Professor Ian G. Harris Atomic 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
14
Slides created by: Professor Ian G. Harris Mutual Exclusion While one task updates the shared variables, another task cannot read them tank_level = ?; time_updated = ?; printf (“%i %i”, tank_level, time_updated); Task 1Task 2 Two code segments should be mutually exclusive If Task 2 is an interrupt, it must be disabled
15
Slides created by: Professor Ian G. Harris Semaphores 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
16
Slides created by: Professor Ian G. Harris Critical Regions TakeSemaphore(); tank_level = ?; time_updated = ?; ReleaseSemaphore(); TakeSemaphore(); printf (“%i %i”, tank_level, time_updated); ReleaseSemaphore(); Task 1 Task 2 Semaphores are used to protect critical regions Two critical regions sharing a semaphore are mutually exclusive Each critical region is atomic, cannot be separated
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.