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

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

CS 31: Intro to Systems Threading & Parallel Applications. Kevin Webb Swarthmore College November 27, 2018

Inter-Process Communication

Why do we care about parallel?

Inter-Process Communication and Synchronization of Processes, Threads and Tasks: Lesson-1: PROCESS

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

Process Management! Goals of this Lecture!

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.

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Processes COMPSCI 386

Process Management 1

Operating System Design

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

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

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

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

Operating Systems. Review ENCE 360

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

CS 351 Week 15. Course Review

CSE 153 Design of Operating Systems Fall 2018

CSC209 Review. Yeah! We made it!

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Design Overview of the FreeBSD Kernel CIS 657

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Processes, Context Switching, and Scheduling. Kevin Webb Swarthmore College January 30, 2018

Sockets 15H2. Inshik Song

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Processes. Process Concept

Lecture 4: Memory Management & The Programming Interface

CS 326: Operating Systems. Process Execution. Lecture 5

W4118 Operating Systems. Junfeng Yang

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

Interprocess Communication

CS 43: Computer Networks. 08:Network Services and Distributed Systems 19 September

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

ECE 435 Network Engineering Lecture 2

Slide 6-1. Processes. Operating Systems: A Modern Perspective, Chapter 6. Copyright 2004 Pearson Education, Inc.

CSE 153 Design of Operating Systems

Processes and Threads

ECE 435 Network Engineering Lecture 2

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

Introduction to Asynchronous Programming Fall 2014

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Concurrent Programming

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Lecture 4: Process Management

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

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Socket Programming TCP UDP

CS 153 Design of Operating Systems Winter 2016

CS 351 Week Advanced UNIX Programming: Rochkind, Marc J. 1. To learn about System Interprocess Communication (IPC). 2. To learn about Sockets.

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

Real Time and Embedded Systems. by Dr. Lesley Shannon Course Website:

628 Lecture Notes Week 4

1 System & Activities

CSci 4061 Introduction to Operating Systems. (Thread-Basics)

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Concurrent Programming

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

Process Management! Goals of this Lecture!

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

What s an API? Do we need standardization?

Lecture 21 Concurrent Programming

ECE 650 Systems Programming & Engineering. Spring 2018

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Embedded System Design

CS 3733 Operating Systems

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Memory Management - Demand Paging and Multi-level Page Tables

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

Process Concept. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

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:

VFS, Continued. Don Porter CSE 506

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 4: Processes

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Chapter 3: Process Concept

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

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Creating a Shell or Command Interperter Program CSCI411 Lab

CSE 120 Principles of Operating Systems

"#' %#& Lecture 7: Organizing Game Clients and Servers. Socket: Network communication endpoints. IP address: IP-level name of a machine

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

Process Description and Control

CSE 410: Systems Programming

G Honors Operating Systems

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Processes. CS3026 Operating Systems Lecture 05

CS5460: Operating Systems

Transcription:

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

Reading Quiz Lecture 5 - Slide 2

Today Under-the-hood look at system calls Data buffering and blocking Inter-process communication Processes, threads and blocking Lecture 7 - Slide 3

Recall Inter-process Communication (IPC) Processes must communicate to cooperate Must have two mechanisms: Data transfer Synchronization On a single machine: Threads (shared memory) Message passing Lecture 7 - Slide 4

Message Passing (local) send (to, buf) Process memory Kernel receive (from, buf) Process-1 Process-2 Operating system mechanism for IPC send (destination, message_buffer) receive (source, message_buffer) Data transfer: in to and out of kernel message buffers Synchronization:? Lecture 7 - Slide 5

Where is the synchronization in message passing Inter-process Communication? A. The OS adds synchronization. B. Synchronization is determined by the order of sends and receives. C. The communicating processes exchange synchronization messages (lock/unlock). D. There is no synchronization mechanism. Lecture 7 - Slide 6

Interprocess Communication (non-local) Processes must communicate to cooperate Must have two mechanisms: Data transfer Synchronization Across a network: Threads (shared memory) NOT AN OPTION! Message passing Lecture 7 - Slide 7

Message Passing (network) Same synchronization Data transfer Copy to/from OS socket buffer Extra step across network: hidden from applications Lecture 7 - Slide 8

Descriptor Table For each Process OS stores a table, per process, of descriptors Kernel Lecture 7 - Slide 9

Descriptors Lecture 7 - Slide 10

Descriptor Table For each Process OS stores a table, per process, of descriptors 0 1 2 stdin stdout stderr Kernel Lecture 7 - Slide 11

socket() For each Process int sock = socket(af_inet, SOCK_STREAM, 0); 7 socket() returns a socket descriptor Indexes into table 0 1 2 stdin stdout stderr 7 Kernel Lecture 7 - Slide 12

socket() For each Process int sock = socket(af_inet, SOCK_STREAM, 0); 7 OS stores details of the socket, connection, and pointers to buffers 0 1 2 7 stdin stdout stderr Family: AF_INET, Type: SOCK_STREAM Local address: NULL, Local port: NULL Send buffer, Receive buffer Kernel Lecture 7 - Slide 13

socket() int sock = socket(af_inet, SOCK_STREAM, 0); 7 For each Process OS stores details of the socket, connection, and pointers to buffers 0 1 2 7 stdin stdout stderr Family: AF_INET, Type: SOCK_STREAM Local address: NULL, Local port: NULL Send buffer, Receive buffer Buffer: Temporary data storage location Kernel Lecture 7 - Slide 14

For each Process Socket Buffers int sock = socket(af_inet, SOCK_STREAM, 0); 7 Application buffer / storage space: 7 Family: AF_INET, Type: SOCK_STREAM Local address: NULL, Local port: NULL Send buffer, Receive buffer Kernel Lecture 7 - Slide 15

For each Process Socket Buffers int sock = socket(af_inet, SOCK_STREAM, 0); 7 Application buffer / storage space: 7 Family: AF_INET, Type: SOCK_STREAM Local address: NULL, Local port: NULL Send buffer, Receive buffer Kernel Internet Lecture 7 - Slide 16

For each Process Socket Buffers int sock = socket(af_inet, SOCK_STREAM, 0); 7 Application buffer / storage space: 7 Family: AF_INET, Type: SOCK_STREAM Local address: NULL, Local port: NULL Send buffer, Receive buffer recv(): Move data from socket buffer to process Kernel Internet Lecture 7 - Slide 17

For each Process Socket Buffers int sock = socket(af_inet, SOCK_STREAM, 0); 7 Application buffer / storage space: 7 Family: AF_INET, Type: SOCK_STREAM Local address: NULL, Local port: NULL Send buffer, Receive buffer send(): Move data from process to socket buffer Kernel Internet Lecture 7 - Slide 18

For each Process Socket Buffers int sock = socket(af_inet, SOCK_STREAM, 0); 7 Application buffer / storage space: 7 Family: AF_INET, Type: SOCK_STREAM Local address: NULL, Local port: NULL Send buffer, Receive buffer Challenge: Your process does NOT know what is stored here! Free space? Is data here? Kernel Lecture 7 - Slide 19

recv() For each Process int sock = socket(af_inet, SOCK_STREAM, 0); (assume we issued a connect() here ) int recv_val = recv(sock, r_buf, 200, 0); r_buf (size 200) 0 1 2 7 Family: AF_INET, Type: SOCK_STREAM Local address:, Local port: Send buffer, Receive buffer Is data here? Kernel Lecture 7 - Slide 20

What should we do if the receive socket buffer is empty? If it has 100 bytes? For each Process int sock = socket(af_inet, SOCK_STREAM, 0); (assume we connect()ed here ) int recv_val = recv(sock, r_buf, 200, 0); r_buf (size 200) Two Scenarios: Socket buffer (receive) Empty 100 bytes Kernel Lecture 7 - Slide 21

What should we do if the receive socket buffer is empty? If it has 100 bytes? For each Process int sock = socket(af_inet, SOCK_STREAM, 0); (assume we connect()ed here ) int recv_val = recv(sock, r_buf, 200, 0); r_buf (size 200) Empty 100 Bytes A Block Block B Block Copy 100 bytes C Copy 0 bytes Block D Copy 0 bytes Copy 100 bytes E Something else Two Scenarios: Socket buffer (receive) Kernel Empty 100 bytes Lecture 7 - Slide 22

What should we do if the send socket buffer is full? If it has 100 bytes? For each Process int sock = socket(af_inet, SOCK_STREAM, 0); (assume we connect()ed here ) int recv_val = recv(sock, r_buf, 200, 0); s_buf (size 200) Two Scenarios: Socket buffer (send) Full 100 bytes Kernel Lecture 7 - Slide 23

What should we do if the send socket buffer is full? If it has 100 bytes? For each Process int sock = socket(af_inet, SOCK_STREAM, 0); (assume we connect()ed here ) int recv_val = recv(sock, r_buf, 200, 0); s_buf (size 200) Full 100 Bytes A Return 0 Copy 100 bytes B Block Copy 100 bytes C Return 0 Block D Block Block E Something else Two Scenarios: Socket buffer (send) Kernel Full 100 bytes Lecture 7 - Slide 24

Blocking Implications send() Do not assume that you will recv() all of the bytes that you ask for. Do not assume that you are done receiving. Always receive in a loop! * recv() Do not assume that you will send() all of the data you ask the kernel to copy. Keep track of where you are in the data you want to send. Always send in a loop! * * Unless you re dealing with a single byte, which is rare. Lecture 7 - Slide 25

ALWAYS check send() return value! When send() return value is less than the data size, you are responsible for sending the rest. Data sent: 0 Data to send: 130 Data: send(sock, data, 130, 0); Lecture 7 - Slide 26

ALWAYS check send() return value! When send() return value is less than the data size, you are responsible for sending the rest. 60 Data sent: 0 Data: Data to send: 130 send(sock, data, 130, 0); Data sent: 60 Data to send: 130 Data: Lecture 7 - Slide 27

ALWAYS check send() return value! When send() return value is less than the data size, you are responsible for sending the rest. 60 Data sent: 0 Data: Data to send: 130 send(sock, data, 130, 0); Data sent: 60 Data: Data to send: 130 // Copy the 70 bytes starting from offset 60. send(sock, data + 60, 130-60, 0); Lecture 7 - Slide 28

ALWAYS check send() return value! When send() return value is less than the data size, you are responsible for sending the rest. 60 Data sent: 0 Data: Data to send: 130 send(sock, data, 130, 0);? Data sent: 60 Data: Data to send: 130 // Copy the 70 bytes starting from offset 60. send(sock, data + 60, 130-60, 0); Repeat until all bytes are sent. (data_sent == data_to_send) Lecture 7 - Slide 29

Blocking Summary send() Blocks when socket buffer for sending is full recv() Blocks when socket buffer for receiving is empty Returns less than requested size when buffer cannot hold full size Returns less than requested size when buffer has less than full size Always check the return value! Lecture 7 - Slide 30

Blocking Summary send() Blocks when socket buffer for sending is full recv() Blocks when socket buffer for receiving is empty Returns less than requested size when buffer cannot hold full size Returns less than requested size when buffer has less than full size Always check the return value! Lecture 7 - Slide 31

Concurrency Think you re the only one talking to that server? Server Lecture 7 - Slide 32

Without Concurrency Think you re the only one talking to that server? Web Server recv() request Lecture 7 - Slide 33

Without Concurrency Think you re the only one talking to that server? Client taking its time Web Server Server Process Blocked! recv() request Ready to send, but server still blocked on first client. If only we could handle these connections separately Lecture 7 - Slide 34

Multiple processes Web Server Server fork()s Server fork()s Child process recv()s Web Server Web Server Services the new client request Lecture 7 - Slide 35

Processes/Threads vs. Parent (More details in an OS class ) Spawned Process Inherits descriptor table Does not share memory New memory address space Scheduled independently Separate execution context Can block independently Spawned Thread Shares descriptor table Shares memory Uses parent s address space Scheduled independently Separate execution context Can block independently Lecture 7 - Slide 36

Processes/Threads vs. Parent (More details in an OS class ) Spawned Process Inherits descriptor table Does not share memory New memory address space Scheduled independently Separate execution context Can block independently Spawned Thread Shares descriptor table Shares memory Uses parent s address space Scheduled independently Separate execution context Can block independently Often, we don t need the extra isolation of a separate address space. Faster to skip creating it and share with parent threading. Lecture 7 - Slide 37

Global variables and static objects are shared Stored in the static data segment, accessible by any thread Dynamic objects and other heap objects are shared Allocated from heap with malloc/free or new/delete Local variables are not shared Refer to data on the stack Threads & Sharing Each thread has its own stack Never pass/share/store a pointer to a local variable on another thread s stack Lecture 7 - Slide 38

Both processes and threads: Several benefits Modularizes code: one piece accepts connections, another services them Each can be scheduled on a separate CPU Blocking I/O can be overlapped Lecture 7 - Slide 39

Which benefit is most critical? A. Modular code/separation of concerns. B. Multiple CPU/core parallelism. C. I/O overlapping. D. Some other benefit. Lecture 7 - Slide 40

Both processes and threads Several benefits Modularizes code: one piece accepts connections, another services them Each can be scheduled on a separate CPU Blocking I/O can be overlapped Still not maximum efficiency Creating/destroying threads still takes time Requires memory to store thread execution state Lots of context switching overhead Lecture 7 - Slide 41

Non-blocking I/O One operation: add a flag to send/recv Permanently, for socket: fcntl() file control Allows setting options on file/socket descriptors int sock, result, flags = 0; sock = socket(af_inet, SOCK_STREAM, 0); result = fcntl(sock, F_SETFL, flags O_NONBLOCK) check result 0 on success Lecture 7 - Slide 42

Non-blocking I/O With O_NONBLOCK set on a socket No operations will block! On recv(), if socket buffer is empty: returns -1, errno is EAGAIN or EWOULDBLOCK On send(), if socket buffer is full: returns -1, errno is EAGAIN or EWOULDBLOCK Lecture 7 - Slide 43

Will this work? server_socket = socket(), bind(), listen() connections = [] while (1) new_connection = accept(server_socket) if new_connection!= -1, add it to connections for connection in connections: recv(connection, ) // Try to receive send(connection, ) // Try to send, if needed Lecture 7 - Slide 44

Will this work? A. Yes, this will work. B. No, this will execute too slowly. C. No, this will use too many resources. D. No, this will still block. server_socket = socket(), bind(), listen() connections = [] while (1) new_connection = accept(server_socket) if new_connection!= -1, add it to connections for connection in connections: recv(connection, ) // Try to receive send(connection, ) // Try to send, if needed Lecture 7 - Slide 45

Event-based concurrency Rather than checking over and over, let the OS tell us when data can be read/written Create set of file/socket descriptors we want to read and write Tell system to block until at least one of those is ready for us to use. The OS worries about selecting which one(s). select() Lecture 7 - Slide 46

Event-based concurrency Rather than checking over and over, let the OS tell us when data can be read/written Tell system to block until at least one of those is ready for us to use. The OS worries about selecting which one(s). Only one process/thread (or one per core) No time wasted on context switching No memory overhead for many processes/threads Lecture 7 - Slide 47