Concurrent Architectures - Unix: Sockets, Select & Signals

Similar documents
Overview: Concurrent Architectures - Unix: Forks and Pipes

THE AUSTRALIAN NATIONAL UNIVERSITY Practice Final Examination, October 2012

Networks. Other Matters: draft Assignment 2 up (Labs 7 & 8 v. important!!) Ref: [Coulouris&al Ch 3, 4] network performance and principles

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

Operating System: Chap13 I/O Systems. National Tsing-Hua University 2016, Fall Semester

3.1 Introduction. Computers perform operations concurrently

Hardware OS & OS- Application interface

Chapter 8: I/O functions & socket options

ECE 650 Systems Programming & Engineering. Spring 2018

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

Operating Systems. Review ENCE 360

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

Concurrent Execution

The control of I/O devices is a major concern for OS designers

Models, Notation, Goals

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

(MCQZ-CS604 Operating Systems)

CS 351 Week 15. Course Review

Mid Term from Feb-2005 to Nov 2012 CS604- Operating System

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

Control-Flow Graph and. Local Optimizations

* What are the different states for a task in an OS?

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Processes. Johan Montelius KTH

What is an Operating System? A Whirlwind Tour of Operating Systems. How did OS evolve? How did OS evolve?

An Esterel Virtual Machine

by I.-C. Lin, Dept. CS, NCTU. Textbook: Operating System Concepts 8ed CHAPTER 13: I/O SYSTEMS

Process Scheduling Queues

Processes and Threads

A process. the stack

Problem Set: Processes

Lecture 15: I/O Devices & Drivers

CSCE Introduction to Computer Systems Spring 2019

CS 450 Operating System Week 4 Lecture Notes

What s an API? Do we need standardization?

Interprocess Communication

A Client-Server Exchange

CSE 153 Design of Operating Systems

Introduction. Interprocess communication. Terminology. Shared Memory versus Message Passing

Systems Programming and Computer Architecture ( ) Timothy Roscoe

Process Description and Control

Operating Systems 2010/2011

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

CSE398: Network Systems Design

CISC2200 Threads Spring 2015

Chapter 13: I/O Systems. Operating System Concepts 9 th Edition

I/O Systems. Amir H. Payberah. Amirkabir University of Technology (Tehran Polytechnic)

Ausgewählte Betriebssysteme - Mark Russinovich & David Solomon (used with permission of authors)

CSEP 561 Connections. David Wetherall

Administrivia. Lab 1 will be up by tomorrow, Due Oct. 11

Ancillary Software Development at GSI. Michael Reese. Outline: Motivation Old Software New Software

Introduction. CS3026 Operating Systems Lecture 01

Blocking System Calls in KRoC/Linux

Computer Architecture CS 355 Busses & I/O System

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

Administrivia. p. 1/20

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

CS 5460/6460 Operating Systems

CSE 153 Design of Operating Systems Fall 18

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Noorul Islam College Of Engineering, Kumaracoil MCA Degree Model Examination (October 2007) 5 th Semester MC1642 UNIX Internals 2 mark Questions

CSC209 Review. Yeah! We made it!

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

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.

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

CS 326: Operating Systems. Process Execution. Lecture 5

Processes. Dr. Yingwu Zhu

I/O Handling. ECE 650 Systems Programming & Engineering Duke University, Spring Based on Operating Systems Concepts, Silberschatz Chapter 13

Linux Driver and Embedded Developer

What is a Process. Processes. Programs and Processes. System Classification 3/5/2013. Process: An execution stream and its associated state

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

Chapter 2 Applications and

CS 326: Operating Systems. Networking. Lecture 17

Probabilistic analysis of algorithms: What s it good for?

SMD149 - Operating Systems

Computer architecture. A simplified model

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Signal Example 1. Signal Example 2

CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF MCA QUESTION BANK UNIT 1

Chapter 4: Multithreaded Programming

Application Programming Interfaces

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

ELEC 377 Operating Systems. Week 1 Class 2

The Kernel Abstraction

Lecture 5: Process Description and Control Multithreading Basics in Interprocess communication Introduction to multiprocessors

Multiprogramming. Evolution of OS. Today. Comp 104: Operating Systems Concepts 28/01/2013. Processes Management Scheduling & Resource Allocation

Problem Set: Processes

CHAPTER 11 INTERRUPTS PROGRAMMING

Processes. Process Scheduling, Process Synchronization, and Deadlock will be discussed further in Chapters 5, 6, and 7, respectively.

CSEP 561 Connections. David Wetherall

Asynchronous Events on Linux

Transport Layer (TCP/UDP)


Transcription:

Concurrent Architectures - Unix: Sockets, Select & Signals Assignment 1: Drop In Labs reminder check compiles in CS labs & you have submitted all your files in StReAMS! formatting your work: why to 80 chars? Overview (Ref: [Coulouris&al Ch 4]): support for concurrency in Unix Berkeley Sockets: protocols: TCP, UDP connection: Ó Ø µ, Ò µ, Ð Ø Ò µ, ÔØ µ, ÓÒÒ Ø µ data transfer Ò µ, Ö Ú µ; advice non-determinism: Ð Ø µ interrupts: motivation, H/W & S/W handling, context soft interrupts (exceptions) and signals; shared memory segments COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 1

creating processes Support for Concurrency in Unix ÓÖ µ and Ü µ (Lab 6, Ass 2) communicating between processes Ô Ô µ (Lab 6) enabling communication between computers sockets (Lab 7, Ass 2) handling non-deterministic events Ð Ø µ (Lab 7, Ass 2) interrupts/signals some other stuff semaphores message queues (not covered in detail) shared memory segments (not covered in detail) COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 2

Berkeley Sockets networking protocol originally provided by BSD 4.1c (1982) API exported by most OS s the principal abstraction is a socket where the application attaches to the network defines operations for creating connections, attaching to network, sending/receiving data and closing connection two forms: connection oriented: Transmission Control Protocol (TCP) connection-less: User Datagram Protocol (UDP) addressing is via an IP address + a port number [Coulouris&al Fig 4.4] COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 3

Connection-oriented and Connection-less Sockets Server Socket() Bind() Client Server Socket() Client Socket() Block until connected Listen() Accept() Establish Connection Socket() Connect() Block until data from client Bind() Recvfrom() Bind() Sendto() Process Request Recv() Send() Send() Recv() Process Request Sendto() Recvfrom() (a) connection-oriented (TCP) (b) connection-less (UDP) COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 4

The Socket Call means by which application attaches to the network ÒØ Ó Ø ÒØ Ñ ÐÝ Ò ØÝÔ ÒØ ÔÖÓØÓÓе Ñ ÐÝ: address family (protocol family) ÍÆÁ, ÁÆÌ, ÆË, ÁÅÈÄÁÆà ØÝÔ : semantics of communication ËÇ Ã ËÇ Ã ËÌÊ Å, ËÇ Ã Ê Ï Ê Å, not all combinations allowed ÔÖÓØÓÓÐ: usually set to 0 but can be set to specific value family and type usually implied by the protocol the return value is a handle (a file descriptor, fd) for the new socket COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 5

The Bind and Listen Calls bind a newly created socket to the specified address ÒØ Ò ÒØ Ó Ø ØÖÙØ Ó Ö Ö ÒØ Ö Ð Òµ Ó Ø: newly created socket handle (fd) Ö : data structure of address of local system IP address and port number same operation for both connection-oriented and connection-less servers can use a well known port or a unique port bunch of routines to convert different forms of address representation (Lab 8) the listen call (server side) used by connection-oriented servers to indicate that an application is willing to receive connections ÒØ Ð Ø Ò ÒØ Ó Ø ÒØ ÐÓ µ Ó Ø: handle (fd) of newly created socket ÐÓ : number of connection requests than can be queued by the system while waiting for server to execute the ÔØ call COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 6

The Accept and Connect Calls the accept call (server side) after the execution of a Ð Ø Ò µ, the accept call carries out a passive opening (server is prepared to accept connects) ÒØ ÔØ ÒØ Ó Ø ØÖÙØ Ó Ö Ö ÒØ Ö Ð Òµ blocks until a remote client carries out a connection request returns with a new socket for the new connection Ö where contains the client s address the connect call (client side) executes an active opening of a connection ÒØ ÓÒÒ Ø Ò Ó Ø ØÖÙØ Ó Ö Ö ÒØ Ö Ð Òµ call does not return until the three-way handshake (TCP) is complete (or an error) Ö : contains remote server s address client OS usually selects a random unused port COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 7

Sockets: Transferring Data & Advice after a connection has been made, Ò µ is used to send a message on a socket: Ò ÒØ Ó Ø Ö Ñ ÒØ Ñ Ð Ò ÒØ Ð µ ÒØ and to receive from a socket into a specified buffer: Ö Ú ÒØ Ó Ø Ö Ù Ö ÒØ Ù Ð Ò ÒØ Ð µ ÒØ advice: read the man pages very carefully for systems you are using use a separate process to implement each protocol applications use buffers as do protocols, copies are expensive, message abstraction aims to minimize this do not use ports<1024, these are reserved for the root demon (server) processes tools and info include netstat, ifconfig, ping, traceroute, /proc, tcpdump etc COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 8

Ø Ø Ö Ù Æ ÓÒÒ Ø ºººµ»» Ó Ø ÓÖ Ö Ò ÒØ Ê Ç ² Ø µ»» Ø ÓÑ Ø ÑÔØÝ Ø Ë Ì ² Ø µ»» ØÓ Ø ººº Ò ººº»» Ñ Ü Ó Ò Ø µ ½ ÒØ Ò ² Ø ÆÍÄÄ ÆÍÄÄ ÆÍÄÄ µ Ð Ø ÁËË Ì ² Ø µµ»» Ø Ú Ð º ÓÒ Non-Determinism: Unix Select the Ð Ø µ system call can blocks on more than one source at a time, waking up when there is information from any of them Ð Ø actually allows you to wait at the same time for messages, events, or write operations compared to Ada select, UNIX select is very messy! (and limited) e.g. wait until any data can be read on any file descriptor in Ø: Ö Ù Æ µ»» Ö ÒØÓ Ù COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 9

interrupt: Non-Determinism: Interrupts and Signals an event external to the currently executing process that causes a change in the normal flow of instruction execution (usually generated by hardware devices external to the CPU) the key issue is that interrupts are asynchronous to current process typically implies that some device needs service an interrupt is a special case of an exception: an event that causes the processor to transition to privileged mode, with control being transferred to the kernel s exception handler code area why interrupts: allow multiple devices to be connected devices need CPU service but the CPU can t predict when external events typically happen on a different timescale compared to CPU want to keep CPU busy between events need a way for CPU to find out a device needs attention alternate solution: polling (why is this bad, when is it good?) COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 10

Hardware Interrupt Handling give each device a wire (interrupt line) that it can use to signal the processor when interrupted, the processor executes a routine called an interrupt handler there is no overhead when there are no requests pending CPU Maskable Non-maskable Interrupt Controller Device Device Device Device controller signals CPU that interrupt has occurred, passes interrupt number interrupts are assigned priorities, in order to handle simultaneous interrupts lower priority interrupts may be disabled during service the CPU senses (checks) interrupt request line after every instruction: if raised, uses the interrupt number to determine which handler to start basic (user process) state saved (as for system call) CPU jumps to the interrupt handler when interrupt is done, process state is reloaded and the process resumes COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 11

Interrupt Context and Software Handling typically there are two parts to interrupt handling what must be done immediately, so that the device can keep running what can be deferred until later, so that response is faster the first part of an handler records the context from whatever was interrupted the handler is not allowed to block (or call functions that might) the processor (may) have no further structure to save state the handler needs to be kept as fast and simple as possible typically sets up work for second part, and flags that this in turn needs to be handled by asserting the corresponding second-level interrupt the deferred parts of handling are sometimes referred to as software interrupts e.g. networking: time critical: copy incoming packet from the network device deferred: process packet, pass to correct application e.g. timer interrupts: time-critical: increment current time-of-day deferred: recalculate process priorities COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 12

Signals software equivalent of (hardware) interrupts allows process or thread to respond to asynchronous external events a process may specify its own signal handlers or may use the OS default action, typically one of: ignore the signal terminate all threads in the process stop or resume all threads in a process signals provide a simple form of inter-process communication basic mechanism: the process state includes flags for possible signals and table for actions to take when a signal is posted to process, the signal pending flag is marked when process is next scheduled to run, pending signals are checked and appropriate action taken signal delivery is not instantaneous COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 13

Signal Terminology and Issues posting (aka signal generation, throwing ) action taken when event occurs that process needs to be notified of delivery (aka signal handling) action taken when process recognizes the arrival of an event catching if a user-level signal handler is invoked, process is said to catch the signal pending signals that have been posted but no yet delivered issues: handler may execute at any time need to be careful of manipulating global state in a signal handler signal delivery may interrupt execution of signal handler code should be re-entrant only one signal handler per signal per process (can t use in library code) usually no signal queuing COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 14

Ø ÓÒ Ò Û Ø ÓÒ ÓÐ Ø ÓÒ ØÖÙØ Ø ÓÒ º Ò Ð Ö Ò ÒØ Ò Ð Ö Ò Û Signal Usage write a signal handler function to handle ËÁ ÁÆÌ (Ctrl-C from keyboard) Ò ÒØ Ò Ð Ö ÒØ µ{ ÚÓ " I n t e r r u p t e d! \ n " µ ÔÖ ÒØ } ÐÙ Ø ÓÙØ µ install it: block signal delivery by masking signals: ØÑ µ Ø ÓÒ ËÁ ÁÆÌ ² Ò Û Ø ÓÒ ² ÓÐ Ø ÓÒ µ specify that signal handlers run on separate ÐØ Ø µ stack: useful if signal is caused by stack overflow retrieve list of pending ÒÔ Ò Ò µ signals: block process until signal is Ù Ô Ò µ posted: send signal to process: ÐÐ µ COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 15

Process Synchronization Using Signals: sigstop.c ÚÓ Ø ØÓÔ ÒØ µ { } " H a n d l e r c a u g h t s i g n a l % d \ n " µ ÔÖ ÒØ Ø ÓÙØ µ Ö ØÙÖÒ ÐÙ Ñ Ò ÒØ Ö Ö Ö Ú µ{ ÒØ Ø ÓÖ µ Ô " H e l l o f r o m P r o c e s s I D % d \ n " ØÔ µµ ÔÖ ÒØ ¼µ { Ø Ø Ò Û Ñ Ø ÓÒ Ò Û Ø ÓÒ ÓÐ Ø ÓÒ ØÖÙØ Ø ÓÒ º Ò Ð Ö Ø ØÓÔ Ò Û ËÁ Ì ÊÅ ² Ò Û Ø ÓÒ ² ÓÐ Ø ÓÒ µ Ø ÓÒ ² Ò Û Ñ µ ÑÔØÝ Ø ² Ò Û Ñ µ Ù Ô Ò " p r o c e s s I D % d : e x i t i n g \ n " ØÔ µµ ÔÖ ÒØ ¼µ Ü Ø Ð } { ½µ Ð Ô ËÁ Ì ÊÅ µ ÐÐ } ÆÍÄÄ µ Û Ø " p r o c e s s I D % d : c h i l d % d h a s e x i t e d \ n " ØÔ µ µ ÔÖ ÒØ COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 16 Ö ØÙÖÒ ¼

Programming with Signals collect: a Shell script cleans up temporary file when interrupted tmpdir=/tmp/collect.$$ trap cd /tmp; rm -rf $tmpdir; exit 0 1 2 3 15 exception handling in Java: done via signals arguably a better method than returning error values (e.g. in C system calls) any method that calls another which Ø ÖÓÛ an exception must either Ø it or throw it upwards e.g. SingleLaneBridge.java Û Ø µ or Ì Ö º Ð Ô µ throw an ÁÒØ ÖÖÙÔØ Ü ÔØ ÓÒ the applet s ØÓÔ µ method calls Ì Ö º ÒØ ÖÖÙÔØ µ to stop the threads whenever the number of cars is reset essential that the ÖÙÒ µ method in the car thread Ø s this properly (and then exits) COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 17

Shared Memory Segments Process Attach Shared Memory Segment Attach Process a process creates a shared memory segment using Ñ Ø µ the original owner of a shared memory segment can assign (or revoke) ownership to another user with ÑØÐ µ. once created, a shared segment can be attached to a process address space using Ñ Ø µ it can be detached Ñ Ø µ using (see ÑÓÔ µ). the attaching process must have the appropriate permissions once attached, the process can perform normal read or write operations to the segment, as allowed by the permissions a shared segment can be attached multiple times by the same process shared memory segments persist after end of process (!!) COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 18

Review: Operating Systems as a Concurrent Architecture process creation by cloning ( ÓÖ µ) with copy-on-write semantics supports parent-child concurrent process relationships limited co-ordination via Û Ø µ; also has Ð Ô µ facility data sharing via copy-on-write (including file descriptors) at start, and transfer via pipes afterwards file descriptor duplication facilitates compound shell commands sockets and pipes uses a byte stream abstraction sockets support a general rendezvous-like communication model has a (limited) select mechanism, used to detect the next receipt of data over several byte streams signals form a limited way for processes to co-ordinate purely a software mechanism: applied when OS next considers the target process for scheduling are also generated via (HW) interrupts or (SW) exceptions special shared memory segments can also be used for process communication COMP2310 Lecture 21: Operating Systems: Sockets, Select & Signals2014 19