wittyResry / myIssue

My issue mark down^_^ 欢迎吐槽,讨论~~
https://github.com/wittyResry/myIssue/issues
The Unlicense
5 stars 1 forks source link

深入理解计算机系统(csapp) #8

Open wittyResry opened 8 years ago

wittyResry commented 8 years ago

第九章 虚拟存储器

不要使用:

#define MAXN 15213
int array[MAXN];

最好使用下面的方式:

int * array;
scanf("%d", &n);
array = (int *) malloc (n * sizeof(int));
碎片
scanf("%d", val);
在这种情况下,scanf将把val的内容解释为地址:情况1,发现不可以写,程序终止;情况2,val的内容对应于虚拟存储器的某个合法的可读/写区域,于是我们就覆盖了存储器,这通常会导致灾难性的、令人困惑的后果。
wittyResry commented 8 years ago

第七章 链接

动态链接共享库

wittyResry commented 8 years ago

第十章 系统级IO

Unix I/O

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(char *filename, int flags, mode_t mode);
//O_RDONLY:只读;O_WRONLY:只写;O_RDWR:可读写。
fd = open("foo.txt", O_RDONLY, 0);
//O_CREAT:文件不存在,创建一个空文件;O_TRUNC:文件存在,则截断它;O_APPEND:在每次写之前,设置文件位置到文件的末尾处。
fd = open("foo.txt", O_WRONLY|O_APPEND, 0);
#include <unistd.h>
int close(int fd);
#include <unistd.h>
//若成功则返回为读的字节数,若EOF则为0,若出错为-1。
ssize_t read(int fd, void *buf, size_t n);
//若成功则返回为写的字节数,否则出错返回-1。
ssize_t write(int fd, const void *buf, size_t n);

RIO

RIO的无缓冲输入输出函数
#include <csapp.h>
//从fd的当前文件位置最多传n个字节到存储器位置usrbuf
ssize_t rio_readn(int fd, void *usrbuf, size_t n);
//从usrbuf传送n个字节到描述符fd。
ssize_t rio_writen(int fd, void *usrbuf, size_t n);
RIO带缓冲的输入输出函数
//读一个文本行,并且拷贝到存储器位置usrbuf
void rio_readinitb(rio_t *rp, int fd);
//最多读maxlen
ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);
//从文件rp读n个字节到存储器位置usrbuf
ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n);
ssize_t rio_readn(int fd, void *usrbuf, size_t n) {
    size_t nleft = n;
    ssize_t nread;
    char *bufp = usrbuf;
    while (nleft > 0) {
        if ((nread = read(fd, bufp, nleft)) < 0) {
            if (errno == EINTR) {
                nread = 0; /* and call read() again*/
            } else {
                reutrn -1; /* errno set by read()*/
            }
        } else if (nread == 0) {
            break;
        }
        nleft -= nread;
        bufp += nread;
    }
    return (n - nleft);
}
ssize_t rio_writen(int fd, void *usrbuf, size_t n) {
    size_t nleft = n;
    ssize_t nwritten;
    char *bufp = usrbuf;
    while (nleft > 0) {
        if ((nwritten = write(fd, bufp, nleft)) < 0) {
            if (errno == EINTR) {
                nwritten = 0; /* and call read() again*/
            } else {
                reutrn -1; /* errno set by read()*/
            }
        }
        nleft -= nwritten;
        bufp += nwritten;
    }
    return n;
}
#define RIO_BUFSIZE 8192
typedef struct {
    int rio_fd;   /* Descriptor for this internal buf */
    int rio_cnt;  /* Unread bytes in internal buf */
    char *rio_bufptr; /* Next unread byte in internal buf */
    char rio_buf[RIO_BUFSIZE]; /* Internal buffer */
} riot;
static ssize_t rio_read(rio_t *rp, char *usrbuf, size_t n) {
    int cnt;
    while (rp -> rio_cnt <= 0) {
        rp -> rio_cnt = read(rp -> rio_fd, rp -> rio_buf, sizeof(rp -> rio_buf));
        if (rp -> rio_cnt < 0) {
            if (errno != EINTR) {
                return -1;
            }
        } else if (rp -> rio_cnt == 0) {
            return 0;
        } else {
            rp -> rio_bufptr = rp -> rio_buf;
        }
    }
    cnt = n;
    if (rp -> rio_cnt < 0) {
        cnt = rp -> rio_cnt;
    }
    memcpy (usrbuf, rp -> rio_bufptr, cnt);
    rp -> rio_bufptr += cnt;
    rp -> rio_cnt -= cnt;
    return cnt;
}
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) {
        if ((nread = rio_read(rp, bufp, nleft)) < 0) {

            if (errno == EINTR) {
                return 0;  /*Call read() again*/
            } else {
                return -1; /* errno set by read() */
            }
        } else if (nread == 0) {
            break; /* EOF */
        }
        nleft -= nread;
        bufp += nread;
    }
    return (n - nleft);
}
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) {
        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 {
                break; /* EOF, some data read */
            }
        } else {
            return -1;
        }
    }
    *bufp = 0; /*设置行结尾ASCII = '\0'*/
    return n;
}
共享文件