Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Similar documents
33. Event-based Concurrency

I/O Models. Kartik Gopalan

CSE 333 Lecture non-blocking I/O and select

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

COMP/ELEC 429/556 Introduction to Computer Networks

December 8, epoll: asynchronous I/O on Linux. Pierre-Marie de Rodat. Synchronous/asynch. epoll vs select and poll

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

Exception-Less System Calls for Event-Driven Servers

CSE 333 Lecture fork, pthread_create, select

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

Today: VM wrap-up Select (if time) Course wrap-up Final Evaluations

Input / Output. Kevin Webb Swarthmore College April 12, 2018

CISC2200 Threads Spring 2015

CS118 Discussion Week 2. Taqi

ISA 563: Fundamentals of Systems Programming

Asynchronous Events on Linux

10. I/O System Library

Parallel I/O Paradigms. Files and File Semantics. Non-Blocking I/O. Enabling Parallel Operations. Multi-Channel Poll/Select. Worker Threads 3/28/2017

Event-based Concurrency (Advanced)

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Network Games Part II. Architecture of Network Game

The POSIX AIO interface consists of the following functions: Enqueue a write request. This is the asynchronous analog of write(2).

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Group-A Assignment No. 6

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

Network Programming TDC 561

ECE 650 Systems Programming & Engineering. Spring 2018

NPRG051 Advanced C++ Programming 2016/17 Assignment 2

Some Notes on R Event Handling

JS Event Loop, Promises, Async Await etc. Slava Kim

Εργαστήριο 9 I/O Multiplexing

Midterm Exam Answers

Project 1: A Web Server Called Liso

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

CS 33. More Network Programming. CS33 Intro to Computer Systems XXXI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSE 43: Computer Networks Structure, Threading, and Blocking. Kevin Webb Swarthmore College September 14, 2017

Version Control with Git and What is there in Project 1 PALLABI GHOSH COMPUTER NETWORKS RECITATION 1

Concurrent Programming

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

Concurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming

Concurrent Servers Dec 2, 2009"

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

Any of the descriptors in the set {1, 4} have an exception condition pending

VFS, Continued. Don Porter CSE 506

Servers: Concurrency and Performance. Jeff Chase Duke University

Introduction to Asynchronous Programming Fall 2014

We start by looking at what s known as event-based programming: we write code that responds to events coming from a number of sources.

CPSC 213. Introduction to Computer Systems. I/O Devices, Interrupts and DMA. Unit 2a Mar 12 and 14. Winter Session 2017, Term 2

CSE 124: OPTIONS, SIGNALS, TIMEOUTS, AND CONCURRENCY. George Porter Oct 13, 2017

CS118 Discussion 1A, Week 3. Zengwen Yuan Dodd Hall 78, Friday 10:00 11:50 a.m.

Overview. POSIX signals. Generation of signals. Handling signals. Some predefined signals. Real-time Systems D0003E 3/3/2009

I/O. CS 416: Operating Systems Design Department of Computer Science Rutgers University

Emulation 2. G. Lettieri. 15 Oct. 2014

CPSC 213. Introduction to Computer Systems. I/O Devices, Interrupts and DMA. Unit 2a Oct 27, 29, 31. Winter Session 2014, Term 1

CONCURRENT PROGRAMMING. Lecture 5

Architecture and Design of Distributed Dependable Systems TI-ARDI. POSA2: Reactor Architectural Pattern

Thursday, July 19, 12. [Introduction to] Writing nonblocking code... in Node.js and Perl

ECE454 Tutorial. June 16, (Material prepared by Evan Jones)

libnetfilter_log Reference Manual

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

NODE.JS MOCK TEST NODE.JS MOCK TEST I

Concurrent Server Design Multiple- vs. Single-Thread

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

Swift 5, ABI Stability and

Asynchronous I/O With boost.asio

Computer Systems Assignment 2: Fork and Threads Package

A Scalable Event Dispatching Library for Linux Network Servers

C# Asynchronous Programming Model

CS 140 Project 4 File Systems Review Session

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

call connect call read call connect ret connect call fgets Client 2 blocks waiting to complete its connection request until after lunch!

[Introduction to] Writing non-blocking code... in Node.js and Perl

UNIT III - APPLICATION DEVELOPMENT. TCP Echo Server

G Honors Operating Systems

First Midterm Exam Solutions October 1, 2018 CS162 Operating Systems

Inter-process Communication using Pipes

Linux Device Drivers Interrupt Requests

Start of Lecture on January 20, Chapter 3: Processes

Introduction to Berkeley Sockets

CS-537: Final Exam (Spring 2011) The Black Box

CS-537: Midterm Exam (Fall 2008) Hard Questions, Simple Answers

Global Employee Location Server

Vector and Free Store (Pointers and Memory Allocation)

Operating Systems CS 217. Provides each process with a virtual machine. Promises each program the illusion of having whole machine to itself

Introduction to Socket Programming

p1 : Distributed Bitcoin Miner /640 9/26/16

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

15-213: Final Exam Review

Redes de Computadores (RCOMP)

Reversing. Time to get with the program

Three Basic Mechanisms for Creating Concurrent Flows. Systemprogrammering 2009 Föreläsning 10 Concurrent Servers. Process-Based Concurrent Server

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall 2010.

CS 43: Computer Networks. 07: Concurrency and Non-blocking I/O Sep 17, 2018

Process Management 1

Operating Systems and Networks Assignment 2

Transcription:

Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst

Are threads it? Threads are not the only way to achieve concurrency Recall that our primary goal is to overlap I/O and CPU Our other goal is to user multiple CPUs (let s put that aside for a moment).

Threads review Recall why we need threads Our I/O calls block until done So while this thread waits, we need another thread to do useful work (or wait for other slow/ I/O) void function(){ do some slow I/O return;

Event-Based Concurrency If you program in Javascript (and some other languages), you will notice there are no threads! Then how do we overlap CPU and I/O? We do CPU work, then we fire off I/O, continue working, then a function we designate gets called when the I/O is done But before we talk about Javascript, lets do C++

Event Loops The heart of an event-based concurrency system is an event loop We wait for one of many things to happen, then we do something based on what happened while (1) { events = getevents(); for (e in events) processevent(e);

select() Systems (such as libc) provide a way to wait for one of many things to happen via select() We give select a set of file descriptors and it returns when the file descriptors have data to read, can accept a write, or has an error int select( int nfds, fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict errorfds, struct timeval *restrict timeout );

Clicker Question #1 Is select a blocking or non-blocking call? (A) It is a blocking call (B) It is a non-blocking call (C) Neither (D) Both

Answer on Next Slide

Simple Example Let s start with a simple example: waiting for data arriving on a set of file descriptors (like sockets) These are external events and happen at unpredictable times

What about locks? There aren t any. Really? Yes.

Single Threaded This event-based systems is single threaded. There is no interleaving of computation So no need for locks

Blocking calls When an event occurs the event loop will call an event handler for that kind of event What if the event handler wants to do I/O? It absolutely cannot make any blocking calls What happens if it does? The world stops

Asynchronous I/O Issue I/O calls that return immediately They don t have the results immediately, but the program can continue When the results are ready the event handler finds the right function to handle the results function get_image(){ aio_read(params ); function process_image(data){ do something with image function event_loop(){ while(1){ select(..); if event == image done process_image(data);

Spaghetti Quickly this leads to spaghetti code. It is really hard to tell where control goes after reading the image Enter: continuations Continuations are a general term for defining where to pass control when the I/O is done. Where does it continue

Callbacks function get_image(){ aio_read(params, process_image); function process_image(data){ do something with image

void setup_io(... ) { int fd; struct aiocb my_aiocb;... /* Set up the AIO request */ bzero( (char *)&my_aiocb, sizeof(struct aiocb) ); my_aiocb.aio_fildes = fd; my_aiocb.aio_buf = malloc(buf_size+1); my_aiocb.aio_nbytes = BUF_SIZE; my_aiocb.aio_offset = next_offset; /* Link the AIO request with a thread callback */ my_aiocb.aio_sigevent.sigev_notify = SIGEV_THREAD; my_aiocb.aio_sigevent.notify_function = aio_completion_handler; my_aiocb.aio_sigevent.notify_attributes = NULL; my_aiocb.aio_sigevent.sigev_value.sival_ptr = &my_aiocb;... ret = aio_read( &my_aiocb ); https://www.ibm.com/developerworks/linux/library/l-async/

Callback void aio_completion_handler( sigval_t sigval ) { struct aiocb *req; req = (struct aiocb *)sigval.sival_ptr; /* Did the request complete? */ if (aio_error( req ) == 0) { /* Request completed successfully, get the return status */ ret = aio_return( req ); return; https://www.ibm.com/developerworks/linux/library/l-async/

Javascript AIO in C++ is pretty hairy (isn t everything in C++?) But in Javascript it is how you *always do it*

Javascript Callbacks fs = require('fs') function callback(err,data) { if (err) { return console.log(err); console.log(data); fs.readfile('/etc/hosts', 'utf8', callback);

Clicker Question #2 What order will the files be printed out? (A) hosts then networks (B) networks then hosts (C) don t know fs = require('fs') function callback(err,data) { if (err) { return console.log(err); console.log(data); fs.readfile('/etc/hosts', 'utf8', callback); fs.readfile('/etc/networks', 'utf8', callback);

Answer on Next Slide