Tenth Week study report

Source: Internet
Author: User
Tags posix

10.7 I/O redirection

The Unix shell provides an I/O redirection operator that allows users to connect disk files to standard input and output, for example:

Unix > ls > foo.txt

dup2 function:

The Copy Descriptor table entry OLDFD to the Descriptor table entry NEWFD, overwriting the previous contents of the Descriptor NEWFD table entry.

10.8 Standard I/O

Standard I/O library (LIBC): Advanced input-Output functions

          fopen/fclose:打开和关闭文件          fread/fwrite:读和写字节          fgets/fputs:读和写字符串          scanf/printf:复杂格式化的I/O函数

The standard I/O library models an open file as a stream, and a stream is a struct pointer to the file type.

Each program starts with three open streams:

                                         stdin:标准输入                                         stdout:标准输出                                         stderr:标准错误

A stream of type file is an abstraction of the document descriptor and stream buffer, the purpose of the stream buffer: to make the number of high-overhead UNIX I/O system calls as small as possible.

10.9 Synthesis: I should use those I/O functions

Standard I/O flow, full duplex, so in order to prevent collisions:

    • Limit one: The input function followed by the output function. --use to flush buffers before each input operation
    • Limit two: The output function followed by the input function. --opens two streams for the same open set of characters, one read and one write.

Most cases use standard I/O, but do not use on network sockets, and use the robust Rio function.

Formatted output

Use the sprintf function to format a string in memory and then send it to the socket with Rio_writen.

Format input

Use Rio_readlineb to read a complete line of text, and then use SSCANF to extract different fields from the line of text.

Appendix A error handling

Error handling Wrapper function:

Given a basic system-level function Foo, define a wrapper function foo with the same parameters. The wrapper function calls the basic function and checks for errors. If an error is found, print a message and terminate the process, otherwise the caller is returned. That is, if there is no error, the wrapper function behaves exactly like the basic function.

The wrapper function is encapsulated in a source file (CSAPP.C) that is compiled and linked to each program.

error handling in A.1 Unix system1, UNIX-style error handling

The function return value includes both the error code and useful results. If an error is encountered, return-1 and set the global variable errno to the error code that indicates the cause of the error. If successful, returns useful results.

2. POSIX-style error handling

Only the return value is used to indicate success (0) or failure (not 0). Any useful results are returned in the function arguments passed in by reference.

3. DNS-style error handling

Returns a null pointer on failure, and sets the global variable H_errno.

A.2 Error handling wrapper function
    • UNIX-style error handling wrapper function

    • POSIX-style error handling wrapper function

    • DNS-style error handling wrapper function

Practice part:

Code:

ls can do:

ls-l
Ls-a
Ls-lu: Last Access time
Ls-s: File size in blocks
Ls-t: Sort by Time
Ls-f: Show File types

LS1:  

Display the contents of the current folder

LS2:

features like LS1, and Print permission size time ...

Who

The WHO also has two code, running the same result, the function of code implementation is the same

The main is to read the information from the Utmp_file file to the memory, and then use the standard output function to print the information: Name,line,time,host, and then close the file

 

Cp:

The CP command is used to copy files or directories

Setecho:

is a function that sets the Echo value

Echostate:

This function is used to check whether the keyboard type command is visible

Since Setecho input yes, the keyboard type command is visible, and when no is entered, the keyboard type command is invisible, combining the two

Tenth Week study report

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.