Concurrent Servers Topics Limitations of iterative servers

Slides:



Advertisements
Similar presentations
Carnegie Mellon 1 Concurrent Programming / : Introduction to Computer Systems 23 rd Lecture, Nov. 15, 2012 Instructors: Dave O’Hallaron, Greg.
Advertisements

Concurrent Servers May 31, 2006 Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based concurrent.
Today’s topic Issues about sending structures with TCP. Server design alternatives: concurrent server and multiplexed server. I/O multiplexing.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
– 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.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
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.
Concurrent HTTP Proxy with Caching Ashwin Bharambe Monday, Dec 4, 2006.
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.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
Concurrent Servers April 25, 2002 Topics Limitations of iterative servers Process-based concurrent servers Threads-based concurrent servers Event-based.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
1 Concurrent Programming. 2 Outline Concurrent programming –Processes –Threads Suggested reading: –12.1, 12.3.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
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 Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
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.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Threads A thread is an alternative model of program execution
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.
Concurrency Threads Synchronization Thread-safety of Library Functions Anubhav Gupta Dec. 2, 2002 Outline.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CS703 – Advanced Operating Systems By Mr. Farhan Zaidi.
Realizing Concurrency using the thread model
Instructor: Haryadi Gunawi
Operating Systems Review ENCE 360.
Threads Synchronization Thread-safety of Library Functions
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
Threads Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Concurrent Programming November 13, 2008
Concurrency I: Threads April 10, 2001
Threads and Cooperation
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
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)
CS510 Operating System Foundations
Concurrent Programming November 13, 2008
Concurrent Programming December 2, 2004
Lecture 19: Threads CS April 3, 2019.
Instructor: Brian Railing
Threads CSE 2431: Introduction to Operating Systems
Concurrent Programming
Presentation transcript:

Concurrent Servers Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based concurrent servers

Fundamental Flaw of Iterative Servers client 1 server client 2 call accept call connect continue continue call read call fgets Client 1 blocks waiting for user to type in data. User goes to lunch. call connect Client 2 blocks waiting to complete its connection request until after lunch! Server blocks waiting for data from Client 1 call write Iterative server blocks on each client (see echoserveri.c) Concurrent servers use multiple concurrent flows to serve multiple clients at the same time.

Concurrent Servers Concurrent servers handle multiple requests concurrently. client 1 server client 2 call accept call connect call connect ret connect ret accept call fgets child 1 fork call accept call read User goes out to lunch Client 1 blocks waiting for user to type in data ret connect call fgets ret accept fork child 2 write call read call read ... write end read close close

Three Basic Mechanisms for Creating Concurrent Flows 1. Processes Separate process for each task (fork) Each process has private address space Kernel schedules processes Interprocess communication (IPC) is difficult 2. Event-Driven based on I/O multiplexing Single process Same address space, easy to communicate Main program schedules logic flows Popular for high-performance server designs 3. Threads Single process (like #2) Same address space, easy to communicate (like #2) Kernel schedules logical flows (like #1) Hybrid of processes and I/O multiplexing

1. Process-Based Servers

Process-Based Echo Server Source: echoserverp.c, p. 852

Practice Problem #13.1, p. 853

Practice Problem #13.2, p. 853

Implementation Issues With Process-Based Designs Server should restart accept call if it is interrupted by a transfer of control to the SIGCHLD handler Not necessary for systems with POSIX signal handling. Our Signal wrapper tells kernel to automatically restart accept (../src/csapp.c) Required for portability on some older Unix systems. Server must reap zombie children to avoid fatal memory leak. Server must close its copy of connfd. Kernel keeps reference for each socket. After fork, refcnt(connfd) = 2. Connection will not be closed until refcnt(connfd)=0.

Pros and Cons of Process-Based Designs + Handles multiple connections concurrently + Clean sharing model + Simple and straightforward - Nontrivial to share data between processes Child has copy of parent’s data (copy on write) Requires IPC (interprocess communication) mechanisms FIFO’s (named pipes), System V shared memory and semaphores - Additional overhead for process control (fork) I/O multiplexing provides more control with better communication and less overhead...

2. Event-Driven Servers Based on I/O Multiplexing

I/O Multiplexing Maintain a pool of connected descriptors. Repeat the following forever: Use the Unix select function to block until: (a) New connection request arrives on the listening descriptor (b) New data arrives on an existing connected descriptor If (a), add the new connection to the pool of connections. If (b), read any available data from the connection Close connection on EOF and remove it from the pool Select bit vector 5 4 3 2 1 maxfdp1 = 5 1 1 stdin stdout stderr

The select Function select() sleeps until one or more file descriptors in the set readset are ready for reading. #include <sys/select.h> int select(int maxfdp1, fd_set *readset, NULL, NULL, NULL); readset Bit vector that indicates membership in a descriptor set. If bit k is 1, then descriptor k is a member of the descriptor set. maxfdp1 Maximum descriptor in descriptor set plus 1. Tests descriptors 0, 1, 2, ..., maxfdp1 - 1 for set membership. select() returns the number of ready descriptors and sets each bit of readset to indicate the ready status of its corresponding descriptor.

Macros for Manipulating Set Descriptors void FD_ZERO(fd_set *fdset); Turn off all bits in fdset. void FD_SET(int fd, fd_set *fdset); Turn on bit fd in fdset. void FD_CLR(int fd, fd_set *fdset); Turn off bit fd in fdset. int FD_ISSET(int fd, *fdset); Is bit fd in fdset turned on?

Select Example Iterative Echo Server Source: sel.c (similar to p. 855)

Select Example Concurrent Echo Server Source: echoservers.c (p. 858) echoserveri 8000, telnet with 2 clients echoservers 8000, telnet with 2 clients

Practice Problem #13.3, p. 861

Practice Problem #13.4, p. 861

Pro and Cons of Event-Based Designs + One logical control flow + Can control priority from main + Single address space (debugging, communication) + No process control overhead Design of choice for high-performance Web servers and search engines - Significantly more complex to code than process- or thread-based designs (3x code for our echoserver) - Can be vulnerable to denial of service attack What if client sends partial line and halts? Handling partial lines is non-trivial Threads provide a middle ground between processes and I/O multiplexing...

3. Threads

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

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

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

Logical View of Threads Threads associated with a process form a pool of peers Unlike processes which form a tree hierarchy Threads are considered peers Threads associated with a process Process hierarchy P0 T2 T4 T1 P1 shared code, data and kernel context sh sh sh T3 T5 foo bar

Concurrent Thread Execution Two threads run concurrently if their logical flows overlap in time. Otherwise, they are sequential. Examples: Concurrent: A & B, A&C Sequential: B & C Thread A Thread B Thread C Time

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

Posix Threads (Pthreads) Interface Pthreads: Standard interface for ~60 functions that manipulate threads from C programs. (p. 864) Creating and reaping threads. pthread_create pthread_join [wait for thread to terminate, reap] pthread_detach [detach from joinable thread] Determining your thread ID pthread_self Terminating threads pthread_cancel [kill another thread] pthread_exit [wait for all peers to exit, then exit main] exit [terminates all threads] return [terminates current thread] Synchronizing access to shared variables pthread_mutex_init pthread_mutex_[un]lock pthread_cond_init pthread_cond_[timed]wait

The Pthreads "hello, world" Program // 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)

Execution of Threaded“hello, world” main thread call Pthread_create() Pthread_create() returns peer thread call Pthread_join() printf() main thread waits for peer thread to terminate return NULL; (peer thread terminates) Pthread_join() returns exit() terminates main thread and any peer threads

Detached Threads At any point in time, a thread is either joinable or detached. Joinable thread can be reaped and killed by other threads. must be reaped (with pthread_join) to free memory resources. Detached threads are reaped automatically on termination Detached thread cannot be reaped or killed by other threads. Default state is joinable. use pthread_detach(pthread_self()) to make detached.

Demo: Parameters and return values Demo: Concurrent Echo Server parm.c Demo: Concurrent Echo Server echoservert.c (p. 867)

Issues With Thread-Based Servers Must run “detached” to avoid memory leak. Must be careful to avoid unintended sharing. For example, what happens if we pass the address of connfd to the thread routine? Pthread_create(&tid, NULL, thread, (void *)&connfd); All functions called by a thread must be thread-safe (next lecture)

Pros and Cons of Thread-Based Designs + Easy to share data structures between threads logging information, statistics + Threads are more efficient than processes. --- Unintentional sharing can introduce subtle and hard-to-reproduce errors! The ease with which data can be shared is both the greatest strength and the greatest weakness of threads. (next lecture)

Summary: Three Mechanisms for Concurrency Process Model Fork to separate process for each flow Hard to share data Kernel schedules processes Event-based Design with I/O Multiplexing One process Easy to share data Program schedules (complex) Threads Kernel schedules (easy)