Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE451 – Section 5.

Similar presentations


Presentation on theme: "CSE451 – Section 5."— Presentation transcript:

1 CSE451 – Section 5

2 Reminders/Overview Rest of project 2 due May 9 Today:
Turnin code + writeup Today: Project 2 parts 4-6 A couple of midterm sample questions & review (time permitting)

3 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

4 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);

5 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.

6 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

7 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!

8 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 Mac or other non-x86 architecture!

9 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);

10 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?

11 Preemption + Critical Sections
Safest way: disable interrupts before critical sections Example: Where is the critical section? Why? NON-threadsafe soln threadsafe soln /*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; } 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; }

12 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

13 Project 2 questions? “Bueller? Bueller? Bueller?”

14 Midterm – top 3 topics Scheduling Synchronization
Virtual Memory – you need to see more in class!

15 Scheduling review FIFO: RR: SJF: Multi-level feedback: + simple
- short jobs can get stuck behind long ones; poor I/O device utilization RR: + better for short jobs - hard to select right time slice - poor turnaround time when jobs are the same length SJF: + optimal (ave. waiting time, ave. time-to-completion) - hard to predict the future - unfair Multi-level feedback: + approximate SJF - unfair to long running jobs Ex, for RR same length: P1, P2 – 100 time units, time slice=1 time unit ( )/2 = 199.5 ( )/2 = 150

16 A simple scheduling problem
Thread Arrival Time Burst Time A 10 B 1 5 C 3 2 FIFO Turnaround time: (completion - arrival) FIFO Waiting Time: (time in wait queue)

17 A simple scheduling problem
Thread Arrival Time Burst Time A 10 B 1 5 C 3 2 FIFO Turnaround Time: (completion - arrival) A: (10-0) = 10 B: (15-1) = 14 C: (17-3) = 14 ( )/3 = 12.66 FIFO Waiting Time: (time in wait queue) A: 0 B: (10-1) = 9 C: (15-3) = 12 ( )/3 = 10.33

18 A simple scheduling problem
What about SJF? Thread Arrival Time Burst Time A 10 B 1 5 C 3 2 Ave Turnaround Time: B: 8-1 = 7 C: 5-3 = 2 A: 17-0 = 17 (17+2+7)/3 = 8.67 Ave Waiting Time: B: 2 C: 0 A: = 7 (2+0+7)/3 = 3 1 3 5 8 17 A B C B A A B C

19 Priority Inversion Have three processes Have this code for P1 and P3:
P1:Highest priority; P2:Medium; P3:Lowest Have this code for P1 and P3: P(mutex); critical section; V(mutex); P3 acquires mutex; preempted by P1 P1 tries to acquire mutex; blocks P2 enters the system at medium priority; runs without acquiring mutex P3 never gets to run since it’s too low priority => P1 never gets to run!! This happened on Mars Pathfinder in 1997! a thread’s importance should increase with the importance of those that depend on it Pathfinder contained an "information bus", which you can think of as a shared memory area used for passing information between different components of the spacecraft. A bus management task ran frequently with high priority to move certain kinds of data in and out of the information bus. Access to the bus was synchronized with mutual exclusion locks (mutexes). The meteorological data gathering task ran as an infrequent, low priority thread, and used the information bus to publish its data. When publishing its data, it would acquire a mutex, do writes to the bus, and release the mutex. If an interrupt caused the information bus thread to be scheduled while this mutex was held, and if the information bus thread then attempted to acquire this same mutex in order to retrieve published data, this would cause it to block on the mutex, waiting until the meteorological thread released the mutex before it could continue. The spacecraft also contained a communications task that ran with medium priority. More at

20 Deadlock-related questions
Can there be a deadlock with only one process? Given two threads, what sequence of calls to transfer(...) causes the following to deadlock? /* transfer x dollars from a to b */ void transfer(account *a, account *b, int x) P(a->sema); P(b->sema); a->balance += x; b->balance -= x; V(b->sema); V(a->sema); T1 T2 Transfer( ); Transfer( );

21 The rest… After completing project 2, you should feel pretty comfortable with locks, semaphores, cond-vars Virtual Memory? More to learn in class first!


Download ppt "CSE451 – Section 5."

Similar presentations


Ads by Google