Winter 2k7 Kurtis Heimerl Aaron Kimball

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 8 SCHEDULING.
Advertisements

CS5270 Lecture 31 Uppaal, and Scheduling, and Resource Access Protocols CS 5270 Lecture 3.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
CSE 451: Operating Systems Section 6 Project 2b; Midterm Review.
CSE 451: Operating Systems
CS 3013 & CS 502 Summer 2006 Scheduling1 The art and science of allocating the CPU and other resources to processes.
CSE451 Section 5: Spring 2006 AA: Kurtis AB: YongChul
1 CSE451 – Section 5. 2 Reminders/Overview Rest of project 2 due Monday February 13 Turnin code + writeup Midterm Wednesday the 8th Today: Project 2 parts.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Wk 2 – Scheduling 1 CS502 Spring 2006 Scheduling The art and science of allocating the CPU and other resources to processes.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Nachos Phase 1 Code -Hints and Comments
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CS333 Intro to Operating Systems Jonathan Walpole.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Deadlock Detection and Recovery
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Introduction to Embedded Systems Rabie A. Ramadan 5.
CSE 451: Operating Systems Section 4 Scheduling, Project 2 Intro, Threads.
For a good summary, visit:
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
CSE 451: Operating Systems Section 6 Project 2b. Midterm  Scores will be on Catalyst and midterms were handed back on Friday(?) in class  Talk to Ed,
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CSE 451: Operating Systems Section 4 Project 2 Intro; Threads.
SE3910 Week 8, Class 3 Week 4 Lab: Please return your graded Lab 4 to me so I can enter it in my gradebook Week 9 Lab: Individual demos of working sub-modules.
CPU Scheduling Scheduling processes (or kernel-level threads) onto the cpu is one of the most important OS functions. The cpu is an expensive resource.
REAL-TIME OPERATING SYSTEMS
Chapter 19: Real-Time Systems
CSE 120 Principles of Operating
CS 6560: Operating Systems Design
CS703 – Advanced Operating Systems
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Preemption Set timer interrupts But now, must synchronize Two tools:
Synchronization.
January 29, 2004 Adrienne Noble
CS399 New Beginnings Jonathan Walpole.
Threading and Project 2 (Some slides taken from Sec. 3 Winter 2006)
Lecture 21 Concurrency Introduction
Reminders Form groups of 3 by tomorrow Start project 2! Today:
Jonathan Walpole Computer Science Portland State University
CSE451 – Section 6.
Multithreading.
Thread Implementation Issues
Lecture 2 Part 2 Process Synchronization
Chapter 30 Condition Variables
CSE 451 Autumn 2003 Section 3 October 16.
CSE451 – Section 5.
Chapter 19: Real-Time Systems
Processes and operating systems
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
First slide Rest of project 2 due next Friday Today:
Reminders Homework 4 due next Friday Rest of project 2 due next Friday
Why Threads Are A Bad Idea (for most purposes)
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Project 2, part 4 - web server
CSE 451 Section 1/27/2000.
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
CSE 153 Design of Operating Systems Winter 2019
Threads CSE 2431: Introduction to Operating Systems
CS444/544 Operating Systems II Scheduler
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Winter 2k7 Kurtis Heimerl (kheimerl@) Aaron Kimball (ak@) CSE451 – Section 5 Winter 2k7 Kurtis Heimerl (kheimerl@) Aaron Kimball (ak@)

Project 2, part 4 - web server web/sioux.c – singlethreaded web server Read in command line args, run the web server loop web/sioux_run.c – the webserver loop Open a socket to listen for connections (listen) Wait for a connection (accept) Handle it Parse the HTTP request Find and read the requested file (www root is ./docs) Send the file back Close the connection web/web_queue.c – an empty file for your use

A peek at the code… In web/sioux.c is main: In web/sioux_run.c: int main(int argc, char **argv) { int port; const char *host; const char *docroot; sthread_init(); /* Get the configuration information */ host = web_gethostname(); port = web_getport(); docroot = web_getdocroot(); /* Tell the user where to look for server */ web_printurl(host, port); /* Handle requests forever */ web_runloop(host, port, docroot); return 0; } void web_runloop(const char *host, int port, const char *docroot) { int listen_socket, next_conn; listen_socket = web_setup_socket(port); while ((next_conn = web_next_connection(listen_socket)) >= 0) { web_handle_connection(next_conn, docroot); } close(listen_socket);

What you need to do Make the web server multithreaded Create a thread pool A bunch of threads waiting for work Number of threads = command-line arg Wait for a connection Find an available thread to handle connection Current request waits if all threads busy Once a thread grabs onto connection, it uses the same processing code as before.

What to change? You’ll need a queue (web_queue.c) void web_runloop(const char *host, int port, const char *docroot) { int listen_socket, next_conn; listen_socket = web_setup_socket(port); //create a queue of waiting connections //create a bunch of threads to handle connections while ((next_conn = web_next_connection(listen_socket)) >= 0) { //add connection to queue //wake-up a thread to handle connection //yield to awoken thread } …. You’ll need a queue (web_queue.c) What about critical sections? What shared/global data structures are there? How do I protect them? Cond-vars are good ways to “wake-up” a thread…hint hint

Hints Each connection is identified by a socket returned by accept Which is just an int Simple management of connections among threads Threads should sleep while waiting for a new connection Condition variables are perfect for this Don’t forget to protect any global variables Use part 2 mutexes, CVs Develop + test with pthreads initially Mostly modify sioux_run.c and/or your own files Stick to the sthread.h interface!

Part 5 - Preemption Requires a relatively small amount of code, but tough to get 100% right What you have to do: Add code that will run every time a timer interrupt is generated Add synchronization to your part 1 and part 2 implementations so that everything works with preemptive thread scheduling What we give you: Timer interrupts Primitives to turn interrupts on and off Synch primitives atomic_test_and_set, atomic_clear WARNING: The code that we have provided to support preemption works correctly only on the x86 architecture! Do not attempt this portion of the assignment on a non-x86 architecture!

What we give you: sthread_preempt.h (implemented in .c): /* start preemption - func will be called every period microseconds */ void sthread_preemption_init(sthread_ctx_start_func_t func, int period); /* Turns inturrupts ON and off * Returns the last state of the inturrupts * LOW = inturrupts ON * HIGH = inturrupts OFF */ int splx(int splval); /* * atomic_test_and_set - using the native compare and exchange on the * Intel x86. * * Example usage: * lock_t lock; * while(atomic_test_and_set(&lock)) { } // spin * _critical section_ * atomic_clear(&lock); int atomic_test_and_set(lock_t *l); void atomic_clear(lock_t *l);

What to do? Add a call to sthread_preemption_init as the last line in your sthread_user_init() function. init specifies a function that is called on each timer interrupt (done for you!) This func should cause thread scheduler to switch to a different thread Hard part: add synchronization to thread management routines Where are the critical sections from part 1 and 2?

Preemption + Critical Sections Safest way: disable interrupts before critical sections Example: Where is the critical section? Why? threadsafe soln NON-threadsafe soln sthread_t sthread_user_next() { sthread_t next; int old = splx(HIGH); next = sthread_dequeue(ready_q); splx(old); if (next == NULL) exit(0); return next; } /*returns the next thread on the ready queue*/ sthread_t sthread_user_next() { sthread_t next; next = sthread_dequeue(ready_q); if (next == NULL) exit(0); return next; }

Part 6 – Report Design discussion & functionality Results Make it short Results Run a few experiments with the new webserver Use given web benchmark: /cse451/projects/webclient Present results in a graphical easy-to-understand form. Explain Are the results what you expected? Try to justify any discrepancies you see Answer a few of our questions

Real-time scheduling Tasks usually accompany deadlines You can’t naively hop from task to task What happens when you violate deadline? Get penalty Real-time system Hard real-time system Once you miss the deadline, system fails Soft real-time system The quality of your task degrades However, soft/hard boundaries can be changed by requirement

Real-time tasks Periodic task Aperiodic task Must be completed within periodic deadline Regular task Aperiodic task Must be completed within a specific deadline Example real-time task & deadline Hard – periodic Giving insulin to glycosuria patient Hard – aperiodic Course project, breaking your car before collision Soft – periodic Decoding multi-media stream (MPEG) Soft – aperiodic Course project: if you don’t care about your grade 

Real-time scheduling Earliest Deadline First Rate monotonic Greedy approach What you do to survive throughout quarter Guarantees to schedule up to 100% CPU utilization Theoretically Preemptive vs. non-preemptive EDF Rate monotonic Static priorities shorter period/deadline gets higher priority Deadline is exactly the same as period No resource sharing between other processes Guarantees to schedule up to n(2^(1/2)-1) utilization N = number of processes U is about 82% when n=2. About 69% when n=inf. Used in real-time operating systems

Scheduling Assume following tasks Utilization Can EDF schedule them? P1: takes 1 unit time. Period is 10 P2: takes 4 unit time. Period is 9 P3: takes 3 unit time. Period is 13 Utilization 1/10 + 4/9 + 3/13 = 0.7753 Can EDF schedule them? Yes Can rate monotonic schedule them? The limit for 3 processes = 0.779763

Starvation Pathfinder @ 1997 Mission on Mars VxWorks: realtime OS supports preemptive priority thread scheduling Information bus data path between components Synchronized by using mutex Priority Bus Management > COMMunication > Meteorological Data Gathering BM task: Use information bus. Run frequently COMM task: Long running time MDG: Run not very often. Use information bus Result System reset Data loss Why? How can we solve this?

Deadlock What is it? Why does it happen? What are the strategies to resolve it?

Deadlock prevention Linux kernel documentation contains a lot of rules about locking find . –name “*[lL]ock*” grep deadlock * Why? Because there are 5000 locks in 2003 Lots of them specifying explicit rules about acquiring locks The rules are: 1. To scan the vmlist (look but don't touch) you must hold the mmap_sem with read bias, i.e. down_read(&mm->mmap_sem) 2. To modify the vmlist you need to hold the mmap_sem with read&write bias, i.e. down_write(&mm->mmap_sem) *AND* you need to take the page_table_lock. 3. The swapper takes _just_ the page_table_lock, this is done because the mmap_sem can be an extremely long lived lock and the swapper just cannot sleep on that. 4. The exception to this rule is expand_stack, which just takes the read lock and the page_table_lock, this is ok because it doesn't really modify fields anybody relies on. 5. You must be able to guarantee that while holding page_table_lock or page_table_lock of mm A, you will not try to get either lock for mm B. -- From vm/locking 1. The symbolic link name, if any (/var/lock/LCK..modem) 2. The "tty" name (/var/lock/LCK..ttyS2) 3. The alternate device name (/var/lock/LCK..cua2) -- From devices.txt Let’s have a look at file system