Download presentation
Presentation is loading. Please wait.
1
System-Level I/O
2
Outline RIO Suggested Reading 11.4
3
Buffered Input and Output
Robust I/O Buffered Input and Output Efficiently read text lines and binary data from a file whose contents are cached in an application-level buffer #include "csapp.h" void rio_readinitb(rio_t *rp, int fd) ; ssize_t readlineb(rio_t *rp, void *usrbuf, size t maxlen); ssize_t readnb(rio_t *rp, void *usrbuf, size t maxlen); returns: number of bytes read (0 if EOF), -1 on error
4
Robust I/O #define RIO_BUFSIZE 8192 typedef struct { int rio_fd;
int rio_cnt; char *rio_bufptr; char rio_buf[RIO_BUFSIZE]; } rio_t ; void rio_readinitb(rio_t *rp, int fd) { rp->rio_fd = fd ; rp->rio_cnt = 0 ; rp->rio_bufptr = rio_buf ; }
5
Robust I/O #include “csapp.h” int main(int argc, char **argv) { int n;
rio_t rio; char buf[MAXLINE]; rio_readinitb(&rio, STDIN_FILENO); while((n = rio_readlineb(&rio, buf, MAXLINE)) != 0 ) rio_writen(STDOUT_FILENO, buf, n) ; }
6
1 static ssize_t rio_read(rio_t *rp, char *usrbuf, size_t n)
2 { 3 int cnt = 0; 4 /* refill if buf is empty */ 5 while (rp->rio_cnt <= 0) { rp->rio_cnt = read(rp->rio_fd, rp->rio_buf, sizeof(rp->rio_buf)); 8 if (rp->rio_cnt < 0) { 9 if (errno != EINTR) return -1 ; 11 } 12 else if (rp->rio_cnt == 0) /* EOF */ 13 return 0; 14 else rp->rio_bufptr = rp->rio_buf; /* reset buffer ptr */ } 17
7
18. /. Copy min(n, rp->rio_cnt) bytes from internal buf to user buf
19 cnt = n ; if ( rp->rio_cnt < n) cnt = rp->rio_cnt ; memcpy(usrbuf, rp->rio_bufptr, cnt) ; rp->rio_buffer += cnt ; rp->rio_cnt -= cnt ; return cnt ; }
8
ssize_t rio_readlineb (rio_t *rp, void *usrbuf, size_t maxlen)
{ int n, rc; char c, *bufp = usrbuf ; for (n=1; n < maxlen; n++) {
9
if ((rc = rio_read(rp, &c, 1)) == 1) {
*bufp++ = c ; if (c == ‘\n’) break ; } else if (rc == 0) { if (n == 1) return 0; /* EOF, no data read*/ else } else return –1 ; /* error */ } *bufp = 0 ; return n ;
10
ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n)
{ size_t nleft = n ; ssize_t nread ; char *bufp = usrbuf; while (nleft > 0) {
11
if ((nread = rio_read(rp, bufp, nleft)) < 0) {
if (errno = EINTR) /* interrupted by sig handler return */ nread = 0 ; else return –1 ; } else if (nread == 0) break ; nleft -= nread ; bufp += nread ; return (n – nleft );
12
Networking Programming
13
Outline Networks Suggested Reading: 12.2
14
Hardware organization of a network host
main memory I/O bridge MI ALU register file system bus memory bus disk controller graphics adapter USB mouse keyboard monitor I/O bus Expansion slots network CPU chip
15
Computer networks A network is a hierarchical system of boxes and wires organized by geographical proximity LAN (local area network) spans a building or campus. Ethernet is most prominent example. WAN (wide-area network) spans country or world. typically high-speed point-to-point phone lines.
16
An internetwork (internet) is an interconnected set of networks.
Computer networks An internetwork (internet) is an interconnected set of networks. The IP Internet is the most famous example of an internetwork. Let’s see how we would build an internetwork from the ground up.
17
Lowest level: Ethernet segment
Ethernet segment consists of a collection of hosts connected by wires (twisted pairs) to a hub. Spans room or floor in a building. host hub 100 Mb/s ports
18
Lowest level: Ethernet segment
Operation Each Ethernet adapter has a unique 48-bit address. Hosts send bits to any other host in chunks called frames. Hub slavishly copies each bit from each port to every other port. every host sees every bit.
19
Next level: Bridged Ethernet segment
Spans building or campus Bridges cleverly learn which hosts are reachable from which ports and then selectively copy frames from port to port
20
Next level: Bridged Ethernet segment
host hub bridge 100 Mb/s 1 Gb/s A B C X Y
21
Conceptual view of LANs
For simplicity, hubs, bridges, and wires are often shown as a collection of hosts attached to a single wire: host ...
22
Next level: internets Multiple incompatible LANs can be physically connected by specialized computers called routers. The connected networks are called an internet.
23
Next level: internets host LAN 1 ... LAN 2 router WAN LAN 1 and LAN 2 might be completely different, totally incompatible LANs
24
Client-Server Programming Model Global IP Internet Suggested Reading:
Outline Client-Server Programming Model Global IP Internet Suggested Reading: 12.1、12.3
25
A client-server transaction
Every network application is based on the client-server model: a server process and one or more client processes server manages some resource. server provides service by manipulating resource for clients.
26
A client-server transaction
process server 1. client sends request 2. server handles request 3. server sends response 4. client response resource Note: clients and servers are processes running on hosts (can be the same or different hosts).
27
The notion of an internet protocol
How is it possible to send bits across incompatible LANs and WANs? Solution: protocol software running on each host and router smoothes out the differences between the different networks.
28
The notion of an internet protocol
Implements an internet protocol (i.e., set of rules) that governs how hosts and routers should cooperate when they transfer data from network to network. TCP/IP is the protocol for the global IP Internet.
29
What does an internet protocol do?
Naming scheme The internet protocol defines a uniform format for host addresses. Each host (and router) is assigned at least one of these internet addresses that uniquely identifies it.
30
What does an internet protocol do?
Delivery mechanism The internet protocol defines a standard transfer unit (packet) Packet consists of header and payload header: contains info such as packet size, source and destination addresses. payload: contains data bits sent from source host.
31
Transferring data over an internet
protocol software client LAN1 adapter Host A data PH FH1 FH2 LAN2 (1) (2) (3) (4) (5) (6) (7) (8) internet packet LAN2 frame Router LAN1 frame server Host B
32
We are glossing over a number of important questions:
Other issues We are glossing over a number of important questions: What if different networks have different maximum frame sizes? (segmentation) How do routers know where to forward frames? How are routers informed when the network topology changes? What if packets get lost?
33
Other issues These questions form the heart of the area of computer systems known as networking.
34
Most famous example of an internet.
Global IP Internet Most famous example of an internet. Based on the TCP/IP protocol family. IP (Internet protocol) : provides basic naming scheme and unreliable delivery capability of packets (datagrams) from host-to-host.
35
Based on the TCP/IP protocol family.
Global IP Internet Based on the TCP/IP protocol family. UDP (Unreliable Datagram Protocol) uses IP to provide unreliable datagram delivery from process-to-process. TCP (Transmission Control Protocol) uses IP to provide reliable byte streams (like files) from process-to-process. Accessed via a mix of Unix file I/O and functions from the Berkeley sockets interface.
36
Hardware and software organization of an Internet application
Internet client host Internet server host client server user code sockets interface (system calls) TCP/IP TCP/IP kernel code hardware interface (interrupts) network adapter network adapter hardware Global IP Internet
37
Programmer’s view of the Internet
Hosts are mapped to a set of 32-bit IP addresses. The set of IP addresses is mapped to a set of identifiers called Internet domain names. is mapped to A process on one host communicates with a process on another host over a connection.
38
Dotted decimal notation
By convention, each by in a 32-bit IP address is represented by its decimal value and separated by a period IP address 0x8002C2F2 =
39
Dotted decimal notation
Functions for converting between binary IP addresses and dotted decimal strings: inet_aton: converts a dotted decimal string to an IP address in network byte order. inet_ntoa: converts an IP address in network by order to its corresponding dotted decimal string. “n” denotes network representation. “a” denotes application representation.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.