C Programming Language Notes (eight) UNIX system interface

Source: Internet
Author: User

8.1 File Descriptor
The UNIX operating system provides services through a series of system calls, which are actually functions within the operating system ANSI C standard libraries are built on UNIX systems and all peripherals in a UNIX system are considered files in the file system, so All input and output must be read and written through the file. That is, all communication between peripherals and programs can be handled by a single interface before reading and writing the file, the intent is to inform the system that the process is called open file if you write a file, You may need to create the file operating system before you want the program to return a small, nonnegative integer called a file descriptor. Any time the input and output of a file is identified by a file descriptor, rather than by a file name, the filesystem is responsible for maintaining all information about the open file, and the user program can only refer to the file through a file descriptor because most of the input and output is done by keyboard and monitor for convenience, Unix made a special arrangement for this. When the command interpreter runs a program, it will open 3 files corresponding to the file descriptor is 012 in turn identify standard input standard output standard error

  

8.2 Low-level IO--read and write

The input output is implemented through the read and write system calls in the C language, which can be accessed through the function read and write access to both systems called read (int fd,char *buf,int n); write (int fd,char *buf,int n); The first parameter is the file descriptor the second parameter is the character array in the program that holds the read and write data. The third parameter is the number of bytes to be transferred. When a file is read, the return value of the function may be less than the number of bytes requested if the return value is 0. Identifies the end of the file that has been reached if 1 indicates that an error occurred while writing the file The return value is the actual number of bytes written if the return value is not equal to the number of bytes written, then an error occurred in one call, the data read and write can be the most common value of any size 1, that is, 1 bytes per read/write or a value similar to the physical block size of a peripheral device, such as 1024 or 4096, calls the function with a larger value to achieve higher efficiency, because the number of system calls is reduced by # include "Syscalls.h" main () {   char Buf[bufsiz ];   int n;   while ((n = read (0,buf,bufsiz)) > 0)      write (1,buf,bufsiz);   return 0;} The function prototype of the system call is placed in a syscalls.h parameter Bufsiz also contains the corresponding operating system used in the header file, which is a more appropriate value if the file size is not a multiple of bufsiz, then a call to read will return a smaller number of bytes// GetChar implementation of int getchar (void) {   char C;   Return (read (0,&c,1) ==1)? (unsigned char) c:eof;} Where c must be a variable of type char, because the read function requires a character pointer type parameter in the return statement to convert C to unsigned char type can eliminate the sign extension problem if you want to include the header file <stdio.h> In the case of compiling the GetChar function you must use the #undef preprocessor directive to cancel the GetChar macro definition because in the header file, GetChar is implemented as a macro

  

8.3 Open creat close and unlink
In addition to the default standard input standard output and standard error files, other files must be explicitly opened before reading and writing system calls open and creat used to implement the function open and fopen very similar, different is open very a file descriptor int type numeric value fopen returns a file pointer # Include <fcntl.h>int fd;fd = open (char *name,int flags,int perms); parameter name filename flags is a value of type int that describes how the file is opened including         O_ Rdonly  Read-only    o_wronly write-  only    o_rdwr    read/write current open parameter perms value is always 0 If an open file does not exist, it will cause an error to create a new file using the creat system call or overwrite an existing file int fd;fd = creat (char *name,int perms), or return 1 if the file descriptor is created successfully if this file already exists. Emptying the original data using creat to create an existing file does not cause an error if the file to be created does not exist, the file specified with the parameter perms is created, each file corresponds to a 9-bit permission information owner owner group other members a program open at the same time the number of files is limited (usually 20) If a program needs to process many files at the same time, then he must reuse the file descriptor function close with the Disconnect file descriptor and open the file directly to the connection, and release the file descriptor close function and the standard library of the fclose function want to correspond, but it does not need to clean (flush) Buffer if the program exits through the Exit function or returns from the main program, all open files will be closed function unlink Delete the file from the file system, which corresponds to the standard library function remove

  

8.4 Random Access Lseek
The input output is usually sequential every time the call to read and write is placed immediately after the location of the previous operation, however, sometimes the file needs to be accessed in any order, and the system call Lseek can move anywhere in the file without actually reading or writing any data long lseek (int fd, Long Offset,int orign); Setting the current position of the file descriptor FD to Offsetoffset is the subsequent read and write operation from the location specified by origin, which will be used when the Lseek system call is made from this location, and the file can be treated as a large array. The cost is slow access some standard library functions fseek and system call Lseek are similar, the first parameter of the former is the file * type and a non-0 value is returned when an error occurs

  

8.5 Examples--implementation of fopen and GETC functions
A file in a standard library is not described by a file descriptor, but rather a file pointer that is described using a file pointer is a pointer to a structure that contains various information about a file that contains the following information: A pointer to a buffer, It can read a chunk of the file one at a time. A counter to the number of characters remaining in a record buffer a pointer to the next character in the buffer file descriptor describes the read-write pattern of flags describing the error state of the flag description file data structure contained in the <stdio.h> Only the names used by other functions in the standard library begin with an underscore, and therefore generally do not conflict with names in the user program

  

8.6 Examples--directory list
A directory in UNIX is a file ls just read this file to get all the filenames but if you need to get additional information about the file, you need to call the system

  

8.7 Instance--Storage allocation program

  

C Programming Language Notes (eight) UNIX system interface

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.