Concurrent HTTP Proxy with Caching Ashwin Bharambe Monday, Dec 4, 2006.

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

Carnegie Mellon 1 Concurrent Programming / : Introduction to Computer Systems 23 rd Lecture, Nov. 15, 2012 Instructors: Dave O’Hallaron, Greg.
Programming with Threads Topics Threads Shared variables The need for synchronization Synchronizing with semaphores Thread safety and reentrancy Races.
Concurrent Servers May 31, 2006 Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based concurrent.
1 Carnegie Mellon Proxylab and stuff : Introduction to Computer Systems Recitation 13: November 19, 2012 Donald Huang (donaldh) Section M.
Carnegie Mellon 1 Synchronization: Basics : Introduction to Computer Systems 23 rd Lecture, Nov. 16, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
– 1 – , F’02 Traditional View of a Process Process = process context + code, data, and stack shared libraries run-time heap 0 read/write data Program.
Concurrent Programming November 28, 2007 Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Concurrent Servers Dec 3, 2002 Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based concurrent.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Programming with Threads Topics Threads Shared variables The need for synchronization Synchronizing with semaphores Thread safety and reentrancy Races.
Carnegie Mellon 1 Synchronization: Basics / : Introduction to Computer Systems 23 rd Lecture, Jul 21, 2015 Instructors: nwf and Greg Kesden.
1 Concurrent Programming Andrew Case Slides adapted from Mohamed Zahran, Jinyang Li, Clark Barrett, Randy Bryant and Dave O’Hallaron.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Concurrent Servers April 25, 2002 Topics Limitations of iterative servers Process-based concurrent servers Threads-based concurrent servers Event-based.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
1 Concurrent Programming. 2 Outline Semaphores for Shared Resources –Producer-consumer problem –Readers-writers problem Example –Concurrent server based.
1 Concurrent Programming. 2 Outline Concurrent programming –Processes –Threads Suggested reading: –12.1, 12.3.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Concurrent Programming : Introduction to Computer.
CS333 Intro to Operating Systems Jonathan Walpole.
Week 16 (April 25 th ) Outline Thread Synchronization Lab 7: part 2 & 3 TA evaluation form Reminders Lab 7: due this Thursday Final review session Final.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
PROCESS AND THREADS. Three ways for supporting concurrency with socket programming  I/O multiplexing  Select  Epoll: man epoll  Libevent 
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Carnegie Mellon Introduction to Computer Systems /18-243, fall nd Lecture, Nov. 17 Instructors: Roger B. Dannenberg and Greg Ganger.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Programming with Threads Topics Threads Shared variables The need for synchronization Synchronizing with semaphores Thread safety and reentrancy Races.
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
Carnegie Mellon 1 Concurrent Programming / : Introduction to Computer Systems 22 nd Lecture, Nov. 11, 2014 Instructors: Greg Ganger, Greg.
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
1. Concurrency and threads 2. Synchronization: basic 3. Synchronization: advanced.
Concurrency Threads Synchronization Thread-safety of Library Functions Anubhav Gupta Dec. 2, 2002 Outline.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
1 CMSC Laundry List P/W/F, Exam, etc. Instructors: HSG, HH, MW.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Instructor: Haryadi Gunawi
Threads Synchronization Thread-safety of Library Functions
Programming with Threads
Programming with Threads
Threads Threads.
CS399 New Beginnings Jonathan Walpole.
Concurrent Programming November 13, 2008
Concurrency I: Threads April 10, 2001
Concurrent Servers Topics Limitations of iterative servers
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Recitation 14: Proxy Lab Part 2
Programming with Threads
Concurrent Programming
Realizing Concurrency using the thread model
CS703 - Advanced Operating Systems
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using Posix Threads (pthreads)
Concurrent Programming : Introduction to Computer Systems 23rd Lecture, Nov. 13, 2018
Realizing Concurrency using the thread model
Concurrent Programming CSCI 380: Operating Systems
Realizing Concurrency using Posix Threads (pthreads)
Programming with Threads
Programming with Threads
Concurrent Programming November 13, 2008
Programming with Threads
Concurrent Programming December 2, 2004
Lecture 19: Threads CS April 3, 2019.
Instructor: Brian Railing
Programming with Threads
Threads CSE 2431: Introduction to Operating Systems
Concurrent Programming
Presentation transcript:

Concurrent HTTP Proxy with Caching Ashwin Bharambe Monday, Dec 4, 2006

2 Outline Parsing Some quick hintsThreads Review of the lectureSynchronization Using semaphores; preview of Wed. lecture Caching in the proxy Questions?

3 Parsing a HTTP request Things to keep in mind Read all lines of the request, not just the first  rio_readlineb Look for Host:, Connection: headers How do you parse? strtok ? Complex semantics  Modifies the string passed as the argument sscanf  sscanf(“%s %s %s”, line, req, url, version) Hand-coded  strchr(‘ ’) and strdup

4 Allocating Buffer Space Size of request is not known before-hand Client can send an arbitrary number of headers Size of response is not known before-hand Server may not set a Content-Length header Some servers set it incorrectly! How do you allocate space beforehand, then? You cannot! Use realloc(), periodically adding more space n = rio_readnb(…); if (used + n > alloced) { req = realloc (...); alloced += chunk_size; }

5 Concurrent servers Web Browser Web Server Web Browser Web Browser Web Server Web Server Proxy Iterative servers can only serve one client at a time Concurrent servers handle multiple requests in parallel

6 Implementing concurrency 1. Processes Fork a child process for every incoming client connection Difficult to share data among child processes 2. Threads Create a thread to handle every incoming client connection Our focus today 3. I/O multiplexing with Unix select() Use select() to notice pending socket activity Manually interleave the processing of multiple open connections More complex!  ~ implement your own app-specific thread package!

7 Traditional view of a process Process = process context + code, data, & stack shared libraries run-time heap 0 read/write data Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Kernel context: VM structures Descriptor table brk pointer Code, data, and stack read-only code/data stack SP PC brk Process context

8 Alternate view of a process Process = thread + code, data, & kernel context shared libraries run-time heap 0 read/write data Thread context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Code and Data read-only code/data stack SP PC brk Thread (main thread) Kernel context: VM structures Descriptor table brk pointer

9 A process with multiple threads Multiple threads can be associated with a process Each thread has its own logical control flow (instruction flow) Each thread shares the same code, data, and kernel context Each thread has its own thread ID (TID) shared libraries run-time heap 0 read/write data Shared code and data read-only code/data Thread 1 context: Data registers Condition codes SP1 PC1 stack 1 Thread 1 (main thread) Kernel context: VM structures Descriptor table brk pointer Thread 2 context: Data registers Condition codes SP2 PC2 stack 2 Thread 2 (peer thread)

10 Threads vs. processes How threads and processes are similar Each has its own logical control flow. Each can run concurrently. Each is context switched. How threads and processes are different Threads share code and data, processes (typically) do not. Threads are less expensive than processes.  Process control (creating and reaping) is twice as expensive as thread control.  Linux/Pentium III numbers: ~20K cycles to create and reap a process. ~10K cycles to create and reap a thread.

11 Posix threads (pthreads) Creating and reaping threads pthread_create pthread_join pthread_detach Determining your thread ID pthread_self Terminating threads pthread_cancel pthread_exit exit [terminates all threads] return [terminates current thread]

12 Hello World, with pthreads /* * hello.c - Pthreads "hello, world" program */ #include "csapp.h" void *thread(void *vargp); int main() { pthread_t tid; Pthread_create(&tid, NULL, thread, NULL); Pthread_join(tid, NULL); exit(0); } /* thread routine */ void *thread(void *vargp) { printf("Hello, world!\n"); return NULL; } Thread attributes (usually NULL) Thread arguments (void *p) return value (void **p) Upper case Pthread_xxx checks errors

13 Hello World, with pthreads main thread main thread waits for peer thread to terminate exit() terminates main thread and any peer threads peer thread call Pthread_create() call Pthread_join() Pthread_join() returns printf() return NULL; (peer thread terminates) Pthread_create() returns

14 Thread-based echo server int main(int argc, char **argv) { int listenfd, *connfdp, port, clientlen; struct sockaddr_in clientaddr; pthread_t tid; if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(0); } port = atoi(argv[1]); listenfd = open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfdp = Malloc(sizeof(int)); *connfdp = Accept(listenfd,(SA *)&clientaddr,&clientlen); Pthread_create(&tid, NULL, thread, connfdp); }

15 Thread-based echo server /* thread routine */ void *thread(void *vargp) { int connfd = *((int *)vargp); Pthread_detach(pthread_self()); Free(vargp); echo_r(connfd); /* thread-safe version of echo() */ Close(connfd); return NULL; } ? pthread_detach() is recommended in the proxy lab

16 Issue 1: detached threads A thread is either joinable or detached Joinable thread can be reaped or killed by other threads. must be reaped ( pthread_join ) to free resources. Detached thread can’t be reaped or killed by other threads. resources are automatically reaped on termination. Default state is joinable. pthread_detach(pthread_self()) to make detached. Why should we use detached threads? pthread_join() blocks the calling thread

17 Issue 2: avoid unintended sharing What happens if we pass the address of connfd to the thread routine as in the following code? connfdp = Malloc(sizeof(int)); *connfdp = Accept(listenfd,(SA *)&clientaddr,&clientlen); Pthread_create(&tid, NULL, thread, connfdp); connfd = Accept(listenfd,(SA *)&clientaddr,&clientlen); Pthread_create(&tid, NULL, thread, (void *)&connfd);

18 Issue 3: thread-safe Easy to share data structures between threads But we need to do this correctly! Recall the shell lab: Job data structures Shared between main process and signal handler Synchronize multiple control flows

19 Synchronizing with semaphores Semaphores are counters for resources shared between threads Non-negative integer synchronization variable Two operations: P(s) & V(s) Atomic operations P(s): [ while (s == 0) wait(); s--; ] V(s): [ s++; ] If initial value of s == 1 Serves as a mutual exclusive lock Just a very brief description Details in the next lecture

20 Sharing with POSIX semaphores #include "csapp.h" #define NITERS 1000 unsigned int cnt; /* counter */ sem_t sem; /* semaphore */ int main() { pthread_t tid1, tid2; Sem_init(&sem, 0, 1); /* create 2 threads and wait */ exit(0); } /* thread routine */ void *count(void *arg) { int i; for (i=0;i<NITERS;i++){ P(&sem); cnt++; V(&sem); } return NULL; }

21 Thread-safety of library functions All functions in the Standard C Library are thread-safe Examples: malloc, free, printf, scanf Most Unix system calls are thread-safe with a few exceptions: Thread-unsafe functionReentrant version asctime asctime_r ctime ctime_r gethostbyaddr gethostbyaddr_r gethostbyname gethostbyname_r inet_ntoa (none) localtime localtime_r rand rand_r

22 Thread-unsafe functions: fixes Return a ptr to a static variable Fixes: 1. Rewrite code so caller passes pointer to struct  Issue: Requires changes in caller and callee hostp = Malloc(...)); gethostbyname_r(name, hostp, …); struct hostent *gethostbyname(char name) { static struct hostent h; return &h; }

23 Thread-unsafe functions: fixes 2. Lock-and-copy  Issue: Requires only simple changes in caller  However, caller must free memory struct hostent *gethostbyname_ts(char *p) { struct hostent *q = Malloc(...); P(&mutex); /* lock */ p = gethostbyname(name); *q = *p; /* copy */ V(&mutex); return q; }

24 Caching What should you cache? Complete HTTP response  Including headers You don’t need to parse the response  But real proxies do. Why? If size(response) > MAX_OBJECT_SIZE, don’t cache

25 Cache Replacement Least Recently Used (LRU) Evict the cache entry whose “access” timestamp is farthest into the past When to evict? When you have no space! Size(cache) + size(new_entry) > MAX_CACHE_SIZE What is Size (cache)?  Sum of size (cache_entries)

26 Cache Synchronization A single cache is shared by all proxy threads Must carefully control access to the cache What operations should be locked? add_cache_entry remove_cache_entry lookup_cache_entry For the ambitious: Multiple readers can peacefully co-exist But if a writer arrives, that thread MUST synchronize access with others

27 Summary Threading is a clean and efficient way to implement concurrent server We need to synchronize multiple threads for concurrent accesses to shared variables Semaphore is one way to do this Thread-safety is the difficult part of thread programming Final review session: Friday 1-2:30pm WeH 7500 (all TAs)

28 Questions?