Multithreaded Web Server.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Threads. Readings r Silberschatz et al : Chapter 4.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 8b Monitors.
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Jonathan Walpole Computer Science Portland State University
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Process Concept An operating system executes a variety of programs
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Multithreading.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Class Announcement TA is expected to add/move office hours to tomorrow for Project 0 Project 0 deadline is extended to next Monday April 13 noon. A copy.
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
Synchronizing Threads with Semaphores
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 12: Thread-safe Data Structures, Semaphores.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
1 OS Review Processes and Threads Chi Zhang
Condition Variables Condition variables support three operations: Wait – add calling thread to the condition variable’s queue and put the thread to sleep.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
CS 360 pthreads Condition Variables for threads. Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Web Server Architecture Client Main Thread for(j=0;j
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
pThread synchronization
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 4 – Thread Concepts
PARALLEL PROGRAM CHALLENGES
Processes and Threads Processes and their scheduling
Chapter 4 – Thread Concepts
Shared-Memory Programming with Threads
Threads Threads.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Getting Started with the µC/OS-III Real Time Kernel
Operating Systems CMPSC 473
Thread synchronization
Multithreading Tutorial
Synchronization and Semaphores
CSE 451 Autumn 2003 Section 3 October 16.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
CSE 451 Section 1/27/2000.
“The Little Book on Semaphores” Allen B. Downey
Monitors and Inter-Process Communication
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Multithreaded Web Server

Rula et AquaLab 2 Threads –Allow multiple computation streams to run “in parallel” –Great for operations with blocking calls to the network or IO Types of threads –Kernel Threads –User Threads –Mapping between them – Linux 1:1

Rula et AquaLab 3 Synchronization –Allow resource and access control to multiple concurrent processes Semaphore –Send signals between processes –Binary, counting Mutex –Mutual Exclusion –Traditional Lock Condition Variables

Rula et AquaLab 4 struct sem { mutex m; condvar cv; unsigned int count; }; sem_init(s, value) { mutex_init(s.m); condvar_init(s.cv); count = value; } wait(s) { mutex_lock(s.m); while (s.count <= 0) { condvar_wait(s.cv, s.m); } --s.count; mutex_unlock(s.m); } post(s) { mutex_lock(s.m); ++s.count; condvar_broadcast(s.cv) mutex_unlock(s.m); }

Rula et AquaLab 5 Threadpools allow finite threads to be reused across multiple streams of execution Better performance –Eliminates overhead of thread creation and destruction –Reuse existing threads Typically consist of Worker Queue and Pool of Created Threads

Rula et AquaLab 6 Task Queue Thread Pool Completed Tasks

Rula et AquaLab 7 Task Queue Thread Pool Completed Tasks

Rula et AquaLab 8 POSIX Pthreads library core functions: Go here for tutorial: Threads pthread_create(thread,attr,start_routine,arg) pthread_exit(status) pthread_cancel(thread) pthread_join(threadid, status) Mutex pthread_mutex_init(mutex,attr) pthread_mutex_destroy(mutex) pthread_mutex_lock(mutex) pthread_mutex_unlock(mutex) Condition Variable pthread_cond_init(condition, attr) pthread_cond_destroy(condition) pthread_cond_wait(condition, mutex) pthread_cond_signal(condition)

Rula et AquaLab 9 Create thread pool –Easiest structure Worker queue with pending tasks Each threads running in loop When job is added to the pool, pthread_cond_signal(&(pool->notify)) Add locking to seats.c Standby List –Implement semaphore –More on this Check for job in queue  if job, pull and run  else wait on condition variable  broadcast notify  Check for job in queue...

Rula et AquaLab 10 Adding multithreading to webserver While(1) { connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); //without threads handle_connection(connfd); //with threads pool_add(threadpool, (void *) &handle_connection, (void *) connfd, 0); }

Rula et AquaLab 11 Condition Variables –Should be in a while loop to prevent race conditions Check for spurious wake-ups. When returning from pthread_cond_wait(), you own lock while((pool->count == 0) && (!pool->shutdown)) { pthread_cond_wait(&(pool->notify), &(pool->lock)); }

Rula et AquaLab 12 Only use a binary semaphore on the standby list –Implement semaphore but use it as a lock... –On the standby list data structure only Order of operations w/ standby list –When thread completes a job / wakes from condition variable, it first checks the standby list in the case that the previous operation freed the seat. If seat is Available, assign seat to that user If seat is Pending, do nothing If seat is Occupied, remove entry from the standby list