Cirrus Logic CS485 Manual de usuario Pagina 47

  • Descarga
  • Añadir a mis manuales
  • Imprimir
  • Pagina
    / 67
  • Tabla de contenidos
  • MARCADORES
  • Valorado. / 5. Basado en revisión del cliente
Vista de pagina 46
CS485G Spring 2015 47
(a) open() and close(): open() returns a file descriptor; the
kernel allocates and initializes a data structure. close() lets
the kernel deallocate the structure.
(b) read() and write()
1 #include <unistd.h> // defines ssize_t: signed integer
2 char buf[512];
3 int fd1, fd2;
4 ssize_t nbytes;
5 if ((fd1 = open("/etc/hosts", O_RDONLY) < 0)) {
6 perror("/etc/hosts");
7 exit(1);
8 }
9 if ((nbytes = read(fd1, buf, sizeof(buf))) < 0) {
10 perror("read");
11 exit(1);
12 }
13 if ((fd2 = creat("/tmp/hosts", 0664)) < 0)) {
14 perror("/tmp/hosts");
15 exit(1);
16 }
17 if ((nbytes = write(fd2, buf, nbytes) != nbytes) {
18 perror("write");
19 exit(1);
20 }
A value of nbytes less than sizeof(buf) is valid.
(c) lseek()
53 Sockets
1. Lecture 28, 4/8/2015
2. A socket is an abstraction that lets two processes communicate, even
if they are on different machines.
3. Setting up a socket is asymmetric; the client and the server do dif-
ferent things, after which both can read() and write().
4. Client
(a) socket() to create a local file descriptor (like “buy a phone”)
Vista de pagina 46
1 2 ... 42 43 44 45 46 47 48 49 50 51 52 ... 66 67

Comentarios a estos manuales

Sin comentarios