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