160 likes | 177 Views
Explore the evolution of thread and event-based concurrency models, with a focus on their advantages, disadvantages, and implementation. Learn about I/O models and the challenges of managing events in high-concurrency environments.
E N D
Chapter 33.Event-based Concurrency(Advanced) Kihyun Cho(choki@snu.ac.kr) School of Computer Science and Engineering Seoul National University
Introduction : Thread vs Event • 1995 - Why Threadsare a Bad Idea (for most purposes) • John Ousterhout, (UC Berkeley, Sun Labs) • 2003 - Why Eventsare a Bad Idea (for high-concurrency servers) • R. von Behren, J. Condit, Eric Brewer (UC Berkeley)
Introduction : Why do we need threads? Threads Process address space • To utilize the idle time of the CPU • e.g. Blocking I/O operations • To enhance responsiveness of the system • e.g. GUI system
Introduction : Why threads are bad? “ How to build concurrent servers without threads? ” Source : http://www.redthreadproductions.com/wp-content/uploads/2012/01/WavyThreadImage.jpg Retrieved on 2015/05/27 • Difficult to program • Synchronizing access to shared state • Deadlockrisk • Modules must be designed “thread safe” • Difficult to achieve good performance • Simple locking lowers concurrency • Context switching costs • Difficult to control over scheduling
The basic idea: An event loop Event Loop Event Handlers “ How do we know if a message has arrived? ” Event-based concurrency
API : select The highest-numbered fd in the three sets + 1 Descriptorsaddresses that will be examined the interval that select() should block waiting for a fd to become ready Check whether there is incoming I/O
Simple code “ Why simpler? ” - No locks needed • “ However, what if…. • Open • Read • Write • in event handler? ” “ fd_set ” 0 1 2 3 4 5 6 7 8 …. FD_ZERO FD_SET FD_ISSET
A solution: Asynchronous I/O Blocking Non-blocking Synchronous Asynchronous
I/O models: Synchronous blocking I/O Source : http://www.ibm.com/developerworks/linux/library/l-async/ Retrieved on 2015/05/27
I/O models : Synchronous non-blocking I/O Source : http://www.ibm.com/developerworks/linux/library/l-async/ Retrieved on 2015/05/27
I/O models : Asynchronous blocking I/O Source : http://www.ibm.com/developerworks/linux/library/l-async/ Retrieved on 2015/05/27
I/O models : Asynchronous non-blocking I/O Source : http://www.ibm.com/developerworks/linux/library/l-async/ Retrieved on 2015/05/27
Asynchronous non-blocking I/O (AIO) AIO control block APIs
Asynchronous non-blocking I/O : Sample code • Non-blocking notification • Signal • Callback Source : http://www.ibm.com/developerworks/linux/library/l-async/ Retrieved on 2015/05/27
Why events are bad? • Manual stack management • Can't maintain local state across events • Multiple CPUs system • The synchronization problems arise again • Paging system • If an event-handler page faults, it will block again • Management • Hard to manage over time
Summary • Thread • Why threads are bad? • Event • I/O models • Asynchronous blocking I/O • select • Asynchronous non-blocking I/O • aio • Why events are bad?