TELE 402 Lecture 11: Advanced UDP… 1 by Dr Z. Huang Overview Last Lecture –Nonblocking I/O and ioctl operations –Source: Chapter 16 & 17 of Stevens’ book.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Threads Programming Thread creation Synchronization.
Chap. 23 Threads (1) Threads v.s. Fork (2) Basic Thread Functions #include int pthread_create ( pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
1 © 2003, Cisco Systems, Inc. All rights reserved. CCNA 1 v3.0 Module 11 TCP/IP Transport and Application Layers.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
TCP. Learning objectives Reliable Transport in TCP TCP flow and Congestion Control.
Advanced UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
1 Transport Layer Computer Networks. 2 Where are we?
UNIX Socket Programming CS 6378
1 Introduction to Raw Sockets 2 IP address Port address MAC address TCP/IP Stack 67 Bootp DHCP OSPF protocol frame type UDP Port # TCP Port.
Assignment 3 A Client/Server Application: Chatroom.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
TCP1 Transmission Control Protocol (TCP). TCP2 Outline Transmission Control Protocol.
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
CCNA 1 v3.0 Module 11 TCP/IP Transport and Application Layers.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Programming with POSIX* Threads Intel Software College.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Network Programming Eddie Aronovich mail:
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
TELE 402 Lecture 10: Unix domain … 1 Overview Last Lecture –Daemon processes and advanced I/O functions This Lecture –Unix domain protocols and non-blocking.
Advanced Sockets API-II Vinayak Jagtap
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
TELE 402 Lecture 9: Daemon … 1 by Dr Z. Huang Overview Last Lecture –Broadcast and multicast This Lecture –Daemon processes and advanced I/O functions.
Consider Letting inetd Launch Your Application. inetd daemon  Problems starting with /etc/rc(without inet daemon)  All the servers contains nearly identical.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
TELE 402 Lecture 12: Signal-Driven I/O & Raw Socket 1 Overview Last Lecture –Advanced UDP sockets and threads –Source: Chapters 22&26 of Stevens’ book.
Computer Networks23-1 PART 5 Transport Layer. Computer Networks23-2 Position of Transport Layer Responsible for the delivery of a message from one process.
Posix Threads Topics PthreadsReadings January 12, 2012 CSCE 713 Advanced Computer Architecture.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Advanced UNIX programming Fall 2002, lecture 16 Instructor: Ashok Srinivasan Acknowledgements: The syllabus and power point presentations are modified.
Today’s topic: UDP Reliable communication over UDP.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
UDP Sockets Chap 8, 14, 22. Elementary UDP Sockets Chap 8.
UDP : User Datagram Protocol 백 일 우
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
TCP/IP1 Address Resolution Protocol Internet uses IP address to recognize a computer. But IP address needs to be translated to physical address (NIC).
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
The Transport Layer Implementation Services Functions Protocols
Chapter 9: Transport Layer
Instructor Materials Chapter 9: Transport Layer
Realizing Concurrency using the thread model
Threads Threads.
Netprog: Threads Programming
Thread Programming.
Linux Processes & Threads
PART 5 Transport Layer Computer Networks.
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Advanced I/O Functions
Presentation transcript:

TELE 402 Lecture 11: Advanced UDP… 1 by Dr Z. Huang Overview Last Lecture –Nonblocking I/O and ioctl operations –Source: Chapter 16 & 17 of Stevens’ book This Lecture –Advanced UDP sockets and threads –Source: Chapters 22&26 of Stevens’ book Next Lecture –Signal-driven I/O, Raw sockets –Source: Chapters 25&28&29 of Stevens’ book

TELE 402 Lecture 11: Advanced UDP… 2 by Dr Z. Huang recvmsg –Prototype: recvmsg(int s, struct msghdr *msg, int flags) struct msghdr { void *msg_name; /* optional address */ socklen_t msg_namelen; /* size of address */ struct iovec *msg_iov; /* scatter/gather array */ size_t msg_iovlen; /* # elements in msg_iov */ void *msg_control; /* ancillary data, see below */ socklen_t msg_controllen; /*ancillary data buffer len */ int msg_flags; /* flags on received message */ };

TELE 402 Lecture 11: Advanced UDP… 3 by Dr Z. Huang Protocol control data Control data –struct cmsghdr { –socklen_t cmsg_len; /* data byte count, including hdr */ –int cmsg_level; /* originating protocol */ –int cmsg_type; /* protocol-specific type */ – /* followed by –u_char cmsg_data[]; */ –};

TELE 402 Lecture 11: Advanced UDP… 4 by Dr Z. Huang Destination address and etc. It is necessary to determine the destination address of a UDP datagram, the interface on which the datagram was received, and to get some receiving flags (refer to advio/recvfromflags.c and advio/dgechoaddr.c) –recvmsg should be used –msg_flags member of msghdr structure returns flags to the application –Use IP_RECVDSTADDR socket option to get the destination address of the received datagram from the msg_control member –Use the IP_RECVIF socket option to get the index of the interface on which the datagram was received from the msg_control member. –Unfortunately the above two options are not supported on Linux

TELE 402 Lecture 11: Advanced UDP… 5 by Dr Z. Huang Datagram truncation When a UDP datagram arrives that is larger than the application’s buffer, the recvmsg sets the MSG_TRUNC flag in the msg_flags member of the msghdr structure There are three possible implementations regarding truncated datagrams –Discard the excess bytes and return the MSG_TRUNC flag to the application (BSD/OS, Linux) –Discard the excess bytes but do not tell the application (Solaris 2.5) –Keep the excess bytes and return them in subsequent read operations on the socket (SVR4) Always allocate an application buffer 1 byte greater than the largest datagram the application should ever receive!

TELE 402 Lecture 11: Advanced UDP… 6 by Dr Z. Huang UDP vs. TCP Advantages of UDP –Support broadcasting and multicasting –No connection setup or teardown UDP minimum transaction time: RTT+SPT TCP minimum transaction time: 2*RTT+SPT If there are multiple request-reply exchanges, the cost of connection management is amortized Features of TCP not provided by UDP –Positive acknowledgments, retransmission of lost packets, duplicate detection, and sequencing of packets –Windowed flow control –Slow start and congestion avoidance

TELE 402 Lecture 11: Advanced UDP… 7 by Dr Z. Huang When to use UDP? Recommendations –UDP must be used for broadcast or multicast applications –UDP can be used for simple request-reply applications but error detection must then be built into the application Exception: HTTP uses TCP –UDP should not be used for bulk data transfer (e.g., file transfer). The reason is that windowed flow control, congestion avoidance, and slow start must all be built into the application (reinvent the wheel!) Exception: TFTP and NFS use UDP for bulk data transfer.

TELE 402 Lecture 11: Advanced UDP… 8 by Dr Z. Huang Adding reliability Two features must be added to a UDP request- reply application (the client) in order to make data transmission reliable –Timeout and retransmission to handle datagrams that are discarded –Sequence numbers so the client can verify that a reply is for the appropriate request –The above features are part of most UDP applications such as DNS, SNMP and RPC. Handling timeout –RTO=RTT+4*RTTvar –If the transmission timer expires, an exponential backoff must be used for next RTO –How to calculate RTT and RTTvar?

TELE 402 Lecture 11: Advanced UDP… 9 by Dr Z. Huang Calculation of RTT Estimated RTT –delta=measuredRTT - srtt –srtt=srtt+g*delta –rttvar=rttvar+h*(|delta|-rttvar) –g is 1/8, and h is 1/4 Retransmission ambiguity problem –When a request is retransmitted, the measured RTT may not be the true RTT –One solution is not to use RTT of retransmitted requests to estimate RTT –Another solution is to use time-stamp: the client prepends to each request a timestamp the server must echo. –Refer to rtt/dg_send_recv.c and lib/rtt.c

TELE 402 Lecture 11: Advanced UDP… 10 by Dr Z. Huang Binding interface addresses The function get_ifi_info can be used for UDP applications to monitor all interfaces on a host to know when a datagram arrives, on which interface it arrives Multiple server processes are forked to bind each interface address Refer to advio/udpserv03.c

TELE 402 Lecture 11: Advanced UDP… 11 by Dr Z. Huang Concurrent UDP servers If the processing of a request is too long, a child UDP server can be forked –If the application is “request-reply” type, the forked child simply processes the request and reply –If the application exchanges multiple datagrams, the child server needs to create a new socket bound to an ephemeral port and use that socket to exchange datagrams with the client

TELE 402 Lecture 11: Advanced UDP… 12 by Dr Z. Huang Concurrent UDP servers (cont.)

TELE 402 Lecture 11: Advanced UDP… 13 by Dr Z. Huang Concurrent UDP servers (cont.)

TELE 402 Lecture 11: Advanced UDP… 14 by Dr Z. Huang Threads Problems with fork –Expensive: each time a copy of the process is created –Interprocess communication is required to pass information between the parent and the child What are Threads? –Called light-weight processes –All threads share the same global memory, which makes the sharing of information easy between threads

TELE 402 Lecture 11: Advanced UDP… 15 by Dr Z. Huang Sharing between Threads 1 All threads within a process share –share process instructions –most data –open files (desrciptors) –signal handlers –current working directory –user and group IDs

TELE 402 Lecture 11: Advanced UDP… 16 by Dr Z. Huang Sharing between Threads 2 Each thread has its own –thread ID –set of registers (including program counter and stack pointer) –stack (for local variables and return addresses) –errno –signal mask –priority

TELE 402 Lecture 11: Advanced UDP… 17 by Dr Z. Huang Posix thread functions 1 pthread_create function int pthread_create (pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void *), void *arg) –tid is the new thread’s ID –attr means attributes: priority, initial stack size, daemon thread or not. –The thread runs the function, func, with one argument, arg –Return value: 0 if OK, positive Exxx value on error

TELE 402 Lecture 11: Advanced UDP… 18 by Dr Z. Huang Posix thread functions 2 pthread_join function int pthread_join(pthread_t tid, void **status) –tid is the thread we want to wait for. –If status is not null, the return value from the thread is stored in the location pointed to by status –Return value: 0 if OK, positive Exxx value on error

TELE 402 Lecture 11: Advanced UDP… 19 by Dr Z. Huang Posix thread functions 3 pthread_self function pthread_t pthread_self(void) –Returns: thread ID of calling thread

TELE 402 Lecture 11: Advanced UDP… 20 by Dr Z. Huang Posix thread functions 4 pthread_detach function int pthread_detach(pthread_t tid) –Detach a thread. A detached thread, like a daemon, is not joinable and its resources are released when terminated –Returns: 0 if OK, positive Exxx value on error –If a thread wants to detach itself: pthread_detach(pthread_self());

TELE 402 Lecture 11: Advanced UDP… 21 by Dr Z. Huang Posix thread functions 5 pthread_exit function void pthread_exit(void *status) –One way for a thread to terminate is to call pthread_exit –status must not point to an object that is local to the calling thread. –Two other ways for a thread to terminate: The function that started the thread calls return If the main function of the process returns or if any thread calls exit, the process terminates, including any threads.

TELE 402 Lecture 11: Advanced UDP… 22 by Dr Z. Huang str_cli Using Threads

TELE 402 Lecture 11: Advanced UDP… 23 by Dr Z. Huang Thread-safe Functions

TELE 402 Lecture 11: Advanced UDP… 24 by Dr Z. Huang Other Considerations Passing arguments to new threads –Don’t share arguments, e.g. connfd in threads/tcpserv01.c Thread-safe functions –Can be called by multiple threads concurrently without data race condition –Posix.1 requires that all functions be thread-safe, with some exceptions in Figure 23.5

TELE 402 Lecture 11: Advanced UDP… 25 by Dr Z. Huang Thread-Specific Data Common problem is due to static variables Solutions to convert a function to be thread-safe –Use thread-specific data –Caller packages all static variables into a structure and to use this structure as an argument to the function –Restructure the interface to avoid any static variables

TELE 402 Lecture 11: Advanced UDP… 26 by Dr Z. Huang Thread-specific data 1

TELE 402 Lecture 11: Advanced UDP… 27 by Dr Z. Huang Thread-specific data 2

TELE 402 Lecture 11: Advanced UDP… 28 by Dr Z. Huang Thread-specific data 3

TELE 402 Lecture 11: Advanced UDP… 29 by Dr Z. Huang Thread-specific data 4

TELE 402 Lecture 11: Advanced UDP… 30 by Dr Z. Huang Thread Termination The call to pthread_key_create has an argument for a destructor function When a thread terminates, the system will call the destructor function It should free allocated memory.

TELE 402 Lecture 11: Advanced UDP… 31 by Dr Z. Huang Mutual Exclusion 1 Sometimes threads must share global data Leads to race conditions

TELE 402 Lecture 11: Advanced UDP… 32 by Dr Z. Huang Mutual Exclusion 2 int pthread_mutex_lock(pthread_mutex_t *mptr) int pthread_mutex_unlock(pthread_mutex_t *mptr)

TELE 402 Lecture 11: Advanced UDP… 33 by Dr Z. Huang Condition Variables 1 Sometimes we need to go to sleep until some condition occurs Do not want to use busy waiting A condition variable with a mutex –Mutex provides mutual exclusion –Condition varialbe provides a signaling mechanism

TELE 402 Lecture 11: Advanced UDP… 34 by Dr Z. Huang Condition Variables 2 int pthread_cond_wait( pthread_cond_t *cptr, pthread_mutex_t *mptr) int pthread_cond_signal( pthread_cond_t *cptr) All return: 0 if OK, positive Exxx value on error Example

TELE 402 Lecture 11: Advanced UDP… 35 by Dr Z. Huang Condition Variables 3 int pthread_cond_broadcast( pthread_cond_t *cptr) int pthread_cond_timewait( pthread_cond_t *cptr, pthread_mutex_t *mptr, const struct timespec *abstime) All return: 0 if OK, positive Exxx value on error