The eighth week summary of the Design foundation of information security system

Source: Internet
Author: User
Tags signal handler types of functions

System-Level I/O 10.1 Unix I/O

(1) A UNIX file is a sequence of M bytes: b0,b1,b2,b3 ... Bk... Bm-1.

(2) All I/O devices, such as networks, disk box terminals, are modeled as files, and all inputs and outputs are executed as read and write to the corresponding file. This is an application interface that becomes UNIX I/O.

This allows all inputs and outputs to be executed in a uniform and consistent manner:

① Open File

② changes the current file location.

③ read and write files.

④ close the file.

(3)

The ① input is copying data from the I/O device to main memory, and the output is copying data from main memory to I/O devices.

② a file is a sequence of bytes.

③ all I/O devices, such as networks, disks, and terminals, are modeled as files, and all inputs and outputs are executed as read-write to the corresponding file.

10.2 Opening and closing files

(1)进程是通过调用open函数来打开一个已存在的文件或者创建一个新文件的

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

(2) Create a new file, the owner of the file has read and write permissions, and all other users have Read permissions

Umask (Def_umask);

Fg=open ("Foo.txt", o_creat| o_trunc| O_weonly,def_mode);

(3) Close an open file by calling the Close function

int close(int fd);//返回值成功为0,出错为-1

10.3 Reading and writing files

(1) The application performs input and output by invoking the read and write functions, respectively.

(2) The Read function copies a maximum of n bytes from the current file position of FD to the memory location buf, and a return value of 1 indicates an error. The return value of 0 means EOF. Otherwise, the return value represents the actual number of bytes transferred

(3) The Write function copies the current file position of up to n bytes to the descriptor FD from the memory location BUF

(4) by invoking the Lseek function, the application can modify the location of the current file in a display manner

(5) In some cases, read and write transmit fewer bytes than the application requires, and these insufficient values do not indicate an error

读时遇到EOF。假设我们猪呢比读一个文件,该文件从当前文件位置开始只含有20多个字节,而我们以50个字节的片进行读取。这样一来,下一个read返回的不足值为20,此后的read将通过返回不足值0来发出EOF信号。

从终端读文本行。如果打开文件是与终端相关联的(如键盘和显示器),那么每个read函数将以此传送一个文本行,返回的不足值等于文本行的大小。

读和写网络套接字。如果打开的文件对应于网络套接字,那么内部缓冲约束和较长的网络延迟会引起read和write返回不足值。对Unix管道调用read和write时,也有可能出现不足值,这种进程间的通信机制不在我们讨论的范围之内。

实际上,除了EOF,在读磁盘文件时,将不会遇到不足值,而且在写磁盘文件时,也不会遇到不足值。如果想创建简装的诸如web服务器这样的网络应用,就必须通过反复调用read和write处理不足值,直到所有需要的字节都传送完毕。

10.4 Robust read and write with Rio packet

(1) The Rio package will automatically handle the insufficient value. Rio provides two different types of functions

① input-output function without buffering. These functions transmit data directly between the memory and the file, without application-level buffering, and they are particularly useful for reading and writing binary data to and from the network.

② the input function with buffering. These functions allow you to efficiently read lines of text and binary data from a file that is cached in an application-level buffer, similar to the one provided by standard I/O functions like printf

(2) Non-buffered input and output function for Rio

① the application can transfer data directly between the memory and the file by invoking the RIO_READN and Rio_writen functions

The ②RIO_READN function transmits up to n and bytes from the current file position of the descriptor FD to the memory location Usrbuf

③ if the RIO_READN and Rio_writen functions are released by a signal handler from the application you will be interrupted, then each function will manually restart read or write. In order to be as portable as possible, allow interrupted system calls and restart them if necessary

(3) The buffered input function of Rio

① a line of text is a sequence of ASCII characters that ends with a newline character. In Unix systems, the newline character (' \ n ') is the same as the ASCII line break (LF) and the numeric value is 0x0a

#include "Csapp.h"

void Rio_readinitb (rio_t *rp,int FD);//No Return

ssize_t Rio_readlineb (rio_t *rp,void *usrbuf,size_t maxlen);

ssize_t rio_readnb (rio_t *rp,void *usrbuf,size_t N);//return value: If the number of bytes succeeded is read, if EOF is 0, if error is-1

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;

Else

return-1;

}

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;

Else

return-1;

}

Nleft-=nwritten;

Bufp+=nwritten;

}

return n;

}

(4) Rio reading program core: Rio-read function

Static ssize_t Rio_read (rio_t *rp,char *usrbuf,size_t N)

{

int cnt;

while (rp->rio_cnt<=0)//If buffer is empty, call function to fill buffer reread data first

{

Rp->rio_cnt=read (rp->rio_fd,rp->rio_buf,sizeof (RP->RIO_BUF));//Call the Read function to fill the buffer

if (rp->rio_cnt<0)//Exclude file cannot read data condition

{

if (Error! = eintr)

{

return-1;

}

}

else if (rp->rio_cnt=0)

return 0;

Else

rp->rio_bufptr = rp->rio_buf;//Update where it is now read

}

Cnt=n;

if (rp->rio_cnt<n)

cnt=rp->rio_cnt;//above three steps to assign the smaller values of N and rp->rio_cnt to CNT

memcpy (usrbuf,rp->rio_bufptr,cnt); Copy the contents of the read buffer to the user buffer

rp->rio_bufptr+=cnt;

rp->rio_cnt-=cnt;

return CNT;

}

10.5 Read file meta data

(1) The application can retrieve information about the file (metadata) by invoking the stat and FSTAT functions

(2) file type

① normal file: Binary or text data, macro directive: S_isreg ()

② Directory file: Information containing other files, macro directive: S_isdir ()

③ sockets: Files that communicate over the network and other processes, macros: S_issock ()

10.6 Sharing Files

(1) The kernel represents an open file with three related data structures

①描述符表

文件表

③v -node表(2) Types of three open files

① Typical

② sharing

③ inheritance

10.7 i/0 redirect

(1) Unix shell provides an I/O redirection operator that allows users to connect disk files to standard input and output

(2) redirect using Dup2 function

10.8 Standard I/O

(1) Benefits of using standard I/O

because this library is implemented on many UNIX operating systems (including Windows, Linux), all software-friendly porting(2) header file for standard I/Ostdio.hthree predefined pointers for standard I/O flow: stdin, stdout, stderr(Unix I/o: Stdin_fileno, Stdout_fileno, Stderr_fileno)(3) Cachethere is a delay in having the cache, that is, the content on the output device and the content in the cache are likely to be different. You can call Fflush to flush the cache. There are several types of caches that can be called to change the default cache type by calling the following API. setbuf, SetvbufSetvbuf can accurately describe the type of cache. Fclose The stream is also flushed when the stream is closed. when a process terminates normally (calling exit directly or returning from the main function), all standard I/O streams with non-writable cached data are flushed and all open standard I/O streams are closed. problems encountered(1) Key differences between standard I/O and unix I/O References(1) Textbook in-depth understanding of computer systems(2) Blog Park related knowledge http://www.cnblogs.com/NeilHappy/archive/2012/12/04/2802033.htmlhttp://www.cnblogs.com/uvsjoh/archive/2012/06/13/2547743.html(3) Baidu http://book.2cto.com/201212/11759.html

The eighth week summary of the Design foundation of information security system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.