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.

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

10. I/O System Library

CSC209 Review. Yeah! We made it!

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

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

COMP/ELEC 429/556 Introduction to Computer Networks

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

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

Redes de Computadores (RCOMP)

CS118 Discussion Week 2. Taqi

Redesde Computadores(RCOMP)

Network Games Part II. Architecture of Network Game

ISA 563: Fundamentals of Systems Programming

NWEN 243. Networked Applications. Layer 4 TCP and UDP

Universal Serial Bus - USB 2.0

How to Setup an Auto-Subscription (Auto- ) for Your Clients in FlexMLS

Group-A Assignment No. 6

CS419: Computer Networks. Lecture 10, Part 2: Apr 11, 2005 Transport: TCP mechanics (RFCs: 793, 1122, 1323, 2018, 2581)

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

I/O Models. Kartik Gopalan

Ch. 10: Name Control

Network Programming TDC 561

Copyright 2013 Thomas W. Doeppner. IX 1

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

Asynchronous Events on Linux

Concurrent Server Design Multiple- vs. Single-Thread

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Network Programming in Python. based on Chun, chapter 2; plus material on classes

Introduction to Linked List: Review. Source:

UNIT IV -- TRANSPORT LAYER

Runtime Support for Algol-Like Languages Comp 412

CS 431 Introduction to Computer Systems Solutions Mid semester exam 2005

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

TCP : Fundamentals of Computer Networks Bill Nace

REFERENCES, POINTERS AND STRUCTS

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

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

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

CSE 333 Lecture server sockets

Object-Oriented Systems Design RMI

Global Employee Location Server

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

BT SIP Trunk Configuration Guide

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

Multiparty Communications CS 118. Computer Network Fundamentals Peter Reiher. Lecture 4 Page 1 CS 118. Winter 2016

4Serial SIK BINDER //77

Announcements. CS18000: Problem Solving And Object-Oriented Programming

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSCI Computer Networks

CS321: Computer Networks Introduction to Application Layer

CSE 461: Computer Networks John Zahorjan Justin Chan Rajalakshmi Nandkumar CJ Park

Switching / Forwarding

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

Project 1: A Web Server Called Liso

Remove previous versions of ExamSoft (SofTest) from your computer and then download Examplify.

Chapter 3: Client-Server Paradigm and Middleware

Problem. Chapter Outline. Chapter Goal. End-to-end Protocols. End-to-end Protocols. Chapter 5. End-to-End Protocols

UNIT III - APPLICATION DEVELOPMENT. TCP Echo Server

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

iwarp Transport Specific Extensions for DAT 2.0

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

RIPE Atlas and IoT Approach, Experiences and Some Interesting Details

Don Libes. National Institute of Standards and Technology Gaithersburg, MD ABSTRACT

Applications & Application-Layer Protocols: FTP and (SMTP & POP)

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Civil Engineering Computation

Topics for this Week

Faculty of Engineering and Information Technology Embedded Software. Lab 3 Interrupts and Timers

Binghamton University. CS-211 Fall Variable Scope

Name: CS 341 Practice Final Exam. 1 a 20 b 20 c 20 d 20 e 20 f 20 g Total 207

Binghamton University. CS-211 Fall Variable Scope

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Network layer overview

ECE 435 Network Engineering Lecture 11

Q: I just remembered that I read somewhere that enterprise beans don t support inheritance! What s that about?

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

CS450 - Structure of Higher Level Languages

The Adapter Pattern. Interface with anything!

A Generic Multi-node State Monitoring Subsystem

CS 326: Operating Systems. Networking. Lecture 17

Cache Lab Implementation and Blocking

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Collaboration of Tasks

Ping of death Land attack Teardrop Syn flood Smurf attack. DOS Attack Methods

What HTTP is : it forms elements (.html..xml) with programs (processes) running on the server (e.g. java) and on clients (e.g. javascript), cookies).

QUIZ. What are 3 differences between C and C++ const variables?

Heap Arrays and Linked Lists. Steven R. Bagley

UDP CONNECT TO A SERVER

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

NETWORK AND SYSTEM PROGRAMMING. I/O Multiplexing: select and poll function

Appendix Example Code

Pointers in C/C++ 1 Memory Addresses 2

UNIT IV TRANSPORT LAYER

Real Time Operating System: Inter-Process Communication (IPC)

Multicast. Note. Sending Multicast Messages

Transcription:

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. As a simple example, before we use the approach in a networking example, we exam a simple relay: we want to write a program that takes data, via a pipe, from the left source and sends it, via a pipe, to the right sink. At the same time it takes data from the right source and sends it to the left sink.

This solution is probably not what we d want, since it strictly alternates between processing the data stream in one direction and then the other.

The select system call operates on three sets of file descriptors: one of fie descriptors we re interested in reading from, one of file descriptors we re interested in writing to, and one of file descriptors that might have exceptional conditions pending (we haven t covered any examples of such things they come up as a peculiar feature of TCP known as out-of-band data, which is beyond the scope of this course). A call to select waits until at least one of the file descriptors in the given sets has something of interest. In particular, for a file descriptor in the read set, it s possible to read data from it; for a file descriptor in the write set, it s possible to write data to it. The nfds parameter indicates the maximum file descriptor number in any of the sets. The timeout parameter may be used to limit how long select waits. If set to zero, select waits indefinitely.

Here a simplified version of a program to handle the relay problem using select. An fd_set is a data type that represents a set of file descriptors. FD_ZERO, FD_SET, and FD_ISSET are macros for working with fd_sets; the first makes such a set represent the null set, the second sets a particular file descriptor to included in the set, the last checks to see if a particular file descriptor is included in the set.

This and the next three slides give a more complete version of the relay program. Initially our program is prepared to read from either the left or the right side, but it s not prepared to write, since it doesn t have anything to write. The variables left_read and right_read are set to one to indicate that we want to read from the left and right sides. The variables right_write and left_write are set to zero to indicate that we don t yet want to write to either side.

We set up the fd_sets rd and wr to indicate what we are interested in reading from and writing to (initially we have no interest in writing, but are interested in reading from either side).

If there is something to read from the left side, we read it. Having read it, we re temporarily not interested in reading anything further from the left side, but now want to write to the right side. In a similar fashion, if there is something to read from the right side, we read it.

Writing is a bit more complicated, since the outgoing pipe might not have room for everything we have to write, but just some of it. Thus we must pay attention to what write returns. If everything has been written, then we can go back to reading from the other side, but if not, we continue trying to write.

Now we use the event paradigm to implement a simple file-transfer program. A complete version of the program shown here is on the course web page.

We design the protocol in terms of a simple state machine.

Note the use of the enum data type. Variables of this type have a finite set of possible values, as given in the declaration.

Each client of our server is represented by a separate client_t structure. We allocate an array of them and will refer to client s client_t structure by the file descriptor of the socket used to communicate with it. Thus if we re using a socket whose file descriptor is sfd to communicate with a client, then the client s state is in clients[sfd].

lsock is the file descriptor for the listening-mode socket on which the server is waiting for connections. Our server may be handling multiple clients; each will be communicating with the server via a separate connected socket. These sockets have file descriptors greater than lsock. Note that trd, twr, rd and wr are all of type fd_set. rd and wr are initialized so that rd contains just the file descriptor for the listening socket and wr is empty. trd and twr are copied from rd and wr respectively before the loop is entered. rd and wr are global variables that are set by new_client, read_event, and write_event.