Fileio
1.open () system call
Header file
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
Prototype
int open (const char * name,int flags);
int open (const char * name,int flags,mode_t mode);
Flags o_rdonly o_wronly O_RDWR
O_append each write operation to the end of the file
O_creat If the specified file does not exist, the file is created
O_EXCL returns 1 if the file to be created already exists, and modifies the value of errno
O_trunc if the file exists and is allowed to write, the entire contents of the file are emptied (the length truncated to 0)
O_noctty if the path name is pointing to the end device, do not use it as a control terminal.
O_nonblock if the path name points to a fifo/block file/character file, the open and subsequent I/O of the file are set to non-blocking mode
The following three constants are also selected for synchronous input and output
O_dsync wait for physical I/O to finish before write. Does not wait for the file property to be updated without affecting the reading of the newly written data.
O_rsyncread waits for all writes to the same region to complete before
O_sync wait for physical I/O to finish before write, including I/O to update file properties
Attention:
O_creat must fill in the mode parameter after selecting
Mode parameter s_i + r+w+x + U
U indicates that the setting owner sets a separate fill usr
G indicates that the Setup group user sets a single item to fill the GRP
O means setting up another person to set up a separate fill oth
Mode can also be set to octal values such as 0644
2.creat ()
Header file
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
Prototype
Int creat (const char* name,mode_t mode);
Equivalent
Open (Name, o_rdonly| O_creat| O_trunc, Mode)
3.Read ()
Header file
#include <unistd.h>
Function prototypes
Ssize_tread (int fd, void buf,size_t len);
Whether read will block is determined by the device being read, set in functions such as open
size_t Maximum value is Size_max
ssize_t Maximum value is Ssize_max
4.Write ()
Header file
#include <unistd.h>
Prototype
Ssize_twrite (int fd, const void * buf,size_t count);
Read and write are actually interacting with the buffer data, not directly exchanging data with the disk
Detailed https://www.cnblogs.com/JohnABC/p/5821660.html
5. Synchronous IO
Fsync () and Fdatasync ()
Header file
#include <unistd.h>
int fsync (int fd);
It is not returned until the data of FD is written to the disk buffer, and metadata such as timestamp and inode information is also established
int fdatasync (int fd);
The FD data is not returned until it is written to the disk buffer
The problem that may be caused is file data update, but the directory is not updated, then the data is written successfully, but the directory entry is not updated and the file is inaccessible.
Workaround: The directory should also call Fsync ();
Sync ()
void sync (void);
Sync so cache.
Sync Flag O_sync
At open (), with this flag, all IO on the file will be synchronized
O_dsync sync only normal data
O_rsync
Direct IO
With 0_direct open (), the system minimizes IO management, initializes the user buffers and devices directly, all the IO is synchronized, and the operation does not return until it is complete.
4. Closing file Close ()
Header file
#include <unistd.h>
int close (int fd);
Disassociate file descriptor for FD and file is not valid
The same program is bound to FD if close, and then open, before the FD and the following refers to the same, is can be used to perform file operations
5.Lseek ()
Header file
#include <sys/types.h>
#include <unistd.h>
Function prototypes
Off_tlseek (int fd,off_t pos,int origin);
Origin can be set to
Seek_cur move the POS bit from the current location of the file
Seek_end moving the POS bit from the end of the file
Sek_set moving the POS bit from file start
Returns the new file location
Ret =-1 indicates an error
Positioning read/write
Pread ()
Header file
#include <unistd.h>
Other words:
#define _xopen_source
is to be able to use 5. The X/openportability guide features.
Function prototypes
Ssize_tpread (int fd,void *buf,size_t count,off_t pos);
Pwrite
Function prototypes
Ssize_tpwrite (int fd,const void * buf,size_t count,off_t POS);
Read and write at POS
The position of the FD will not change, and the read write mix will result in overwriting
File truncation
Header file
#include <unistd>
#include <sys/types.h>
Function Prototypes:
intftruncate (int fd,off_t len);
Inttruncate (char * path,off_t len);
IO multiplexing
6.Select
Header file
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
Function Prototypes:
intSELECT (int n,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval*timeout);
n the maximum number of file descriptors in all collections plus one
Timeout struct body
#include <sys/time.h>
struct{
Longtv_sec;//seconds
Longtv_usec; microseconds
};
When select returns, the struct state is undefined, preferably reinitialized, and the newer version will initialize him to the remaining time.
If the time for 0,select returns immediately, the report file descriptor is not available and is not waiting for subsequent operations.
Manage File Description Macros definition
FD_CLR (Intfd,fd_set *set);
Fd_isset (Intfd,fd_set *set); Tests whether a file descriptor is within the collection
Fd_set (int fd,fd_set *set); Adds a file descriptor to the specified collection
Fd_zero (Fd_set *set); Removes all descriptors from the specified collection, calling the Select () before each call
The set of file descriptors is statically established and the maximum value is limited to fd_setsize; Linux is 1024
By setting the select three collection to 0, the time-out is set to non-null to implement sleep;
7.Pselect
POSIX version of Select
Header file
#include <sys/select.h>
#define _XOPEN_SOURCE 600
Prototype
Intpselect (int n,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,
Const struct Timespec *timeout,
Const sigset_t * sigmask);
and select ratio Difference
The time-out parameter type changes, the Pselect uses the Timespec precision is second and nanosecond, is higher than the second and the millisecond precision, the fact is all in seconds after the unreliable
Pselect do not modify the timeout parameter
Select does not have the Sigmask parameter, Pselect is added to resolve the race condition between the signal and the file descriptor
Generally choose to use Select
Attention:
Select is returned when there is a change in the state of the file descriptor, the file descriptor collection is modified when returned, and only the state-changed FD in the returned collection is re-assigned each time it is used.
8.Poll
I/O multiplexing of System V
Header file:
#include <sys/poll.h>
Function Prototypes:
int poll (struct POLLFD *fds,unsigned intnfds, int timout);
POLLFD structure
Header file
#include <sys/poll.h>
Structpollfd
{int FD; File descriptor
The event bit mask on the file descriptor that the short event;//monitors
Short revent;//Event bit mask that occurs on a file description
}
Constant |
Description |
Pollin |
Normal or priority with data-readable |
Pollrdnorm |
Plain Data readable |
Pollrdband |
Priority with data-readable |
Pollpri |
High-priority data readable |
Pollout |
Plain data can be written |
Pollwrnorm |
Plain data can be written |
Pollwrband |
Priority with data writable |
Pollerr |
Error occurred |
Pollhup |
Occurs pending |
Pollnval |
The description word is not an open file |
Attention:
Poll only modified the Revents field of FDS after returning, so it can be used repeatedly.
differs from SELECT.
9.Ppoll
Similar to the Pselect
Standard I/O
Open File
10.fopen ()
Header file
#include <stdio.h>
Function Prototypes:
File*fopen (const char* path,const char *mode);
File is opened and associated to a new stream
Open mode: R r+ W w+ A +
The pointer returned successfully, and the failure returned null.
11.fdopen
Header file
#include <stdio.h>
Function Prototypes:
file* Fdopen (intfd,const char *mode);
Tell the descriptor of an open file into a stream, and the mode will be the same when converted
12.fclose
Header file
#include <stdio.h>
Function Prototypes:
Intfclose (FILE * stream);
Successfully returns 0 and writes no write data to the buffer, failing to return EOF
Fcloseall.
#include <stdio.h>
#define _gun_source
Int Fcloseall ();
14.FGETC ()
#include <stdio.h>
Int fgetc (File*stream);
The function result must be saved with an int, EOF will be returned, and the output will be checked for c== EOF
15.UNGETC ()
#include <stdio.h>
int ungetc (int c,file*stream);
Successfully returned 0, failed to return EOF;
16.fgets
#include <stdio.h>
char * fgets (char *str,int size,file*stream);
Read Size-1 bytes to STR, successful return STR failed to return NULL.
Ends when the EOF or newline character is read, and is written to Str if a newline character is read.
Maximum input length is line_max defined in Limits.h
17.fread
18.FPUTC ()
#include <stdio.h>
int FPUTC (int c,file *stream);
19fputs ()
#include <stdio.h>
Int fputs (const char *str,file *stream);
Fwrite21.fseek ()
#include <stdio.h>
Int fseek (FILE *stream,long offset,int whence);
The whence has the following values:
Seek_cur file location is current plus offset
Seek_end file location for end of file plus offset
Seek_set file location is 0+offset
Error returned-1; 0 returned correctly
22.fsetpos ()
#include <stdio.h>
Int fsetpos (FILE * stream,fpos_t *pos);
23.rewind
#include <stdio.h>
Void Rewind (file* stream); Place flow in initial position
Ftell ()
#include <stdio.h>
Long Ftell (file* stream);
Returns the current stream location of the file. Error returned-1.
25fgetpos
#include <stdio.h>
int Fgetpos (file* stream,fpos_t *pos);
26.fflush
#include <stdio.h>
Int fflush (file* stream); Write data back to file in stream
Successful return 0;
Int ferror Check for errors
Int feof Check if end of file
Void clearer empty failed and file tail flag, unrecoverable
27. Fileno
#include <stdio.h>
Int Fileno (FILE *stream);
Return file descriptor failed return-1;
28.Setvbuf
#include <stdio.h>
Int setvbuf (FILE * stream, char * buf, Intmode, size_t size);
_ionbf
_iolbf
_iofbf
function In addition to no cache mode, BUF can point to a size buffer, and if buf is empty, Glic is assigned
BufSize in Stdio.h defines the default buffer size
29.readv and Writev
#include <sys/uio.h>
ssize_t readv (int fd, const struct iovec*iov,int count);
ssize_t Writev (int fd, const struct iovec*iov,int count);
Reads from the buffer described by Iov or writes count of segment data to FD.
The buffer described by Struct Iovec becomes segment
#include <sys/uio.h>
Struct Iovec
{
Void *iov_base;
size_t Iov_len;
}
30.epoll
#include <sys/epoll.h>
int epoll_create (int size);
Returns the file descriptor associated with this instance, size is the number of file descriptors to monitor. Error return-1
EINVAL size is not a positive number
Enfile system open files up to the maximum number
Enomen Not enough memory
The returned file descriptor needs to be closed with close.
#include <sys/epoll.h>
int epoll_ctl (int epfd,int op,int fd,structepoll_event *event);
#include <sys/epoll.h>
Struct epoll_event{
_U32 events;
union{
Void *ptr;
INT FD;
_u32 u32;
_64 U64;
} data;};
The EPOLL_CTL call associates the Epoll instance with the EPFD, op specifies the action to be performed, and the event describes the more specific behavior
OP's value
Epool_ctl_add to add FD to the monitoring list
Epool_ctl_del Delete FD to monitor list
Epool_ctl_mod using event to modify the FD in the monitoring list
Event Value:
Epollin: Indicates that the corresponding file descriptor can be read (including a graceful shutdown of the peer socket);
Epollout: Indicates that the corresponding file descriptor can be written;
Epollpri: Indicates that the corresponding file descriptor has an urgent data readable (this should indicate the arrival of out-of-band data);
Epollerr: Indicates an error occurred in the corresponding file descriptor;
Epollhup: Indicates that the corresponding file descriptor is hung up;
Epollet: Set Epoll to edge triggered mode, which is relative to the horizontal trigger (level triggered).
Epolloneshot: Listen to only one event, when the event is monitored, if you still need to continue to listen to the socket, you need to use Epoll_ctl_mod to modify the FD
#include <sys/epoll.h>
int epoll_wait (int epfd,structepoll_event * Events,int maxevents,int timeout);
Timeout milliseconds
Return success events point to Epoll_event struct, where up to maxevents return value is the number of events, error returns-1
Edge triggering and horizontal triggering
Sets the Edge trigger when events is set to Rpolle in Epoll_ctl.
Example:
- Producer writes data to pipe 1KB
- The consumer pipeline calls Epoll_wait to wait for the data.
In the Edge trigger, the epoll_wait is not returned until the 1 data is written, and the horizontal trigger epoll_wait immediately returns
The horizontal trigger is the default form, and select and poll are horizontal modes. The horizontal trigger concerns only the state, and the edge triggers the relationship event.
Memory mapping
31.Mmap
#include <sys/mman.h>
Void*mmap (void* addr,size_t len,int prot,int flags,int fd,off_t offset);
The addr parameter tells the best address of the kernel mapping file, except that the hint is not mandatory. The general setting is 0; The call returns the start address of the memory map.
Prot setting access rights protnone inaccessible, generally this setting is meaningless.
Prot_read readable
Prot_write can write
Prot_exec Executable
The access permission cannot conflict with the access mode of the open file.
The flag parameter describes the type of mapping and some behavior
Map_fixed//Using the specified mapping start address, if the memory area specified by the start and Len parameters overlap the existing mapping space, the overlapping portions will be discarded. If the specified start address is not available, the operation will fail. And the start address must fall on the boundary of the page.
Map_shared//share the mapping space with all other processes that map this object. Writes to the shared area are equivalent to outputting to a file. The file is not actually updated until Msync () or Munmap () is called.
Map_private//Create a private mapping of a write-time copy. The write to the memory area does not affect the original file. This flag is mutually exclusive to the above logo and can only be used with one.
Map_denywrite//This flag is ignored.
Map_executable//Ibid.
Map_noreserve//Do not reserve swap space for this mapping. When the swap space is preserved, modifications to the map area may be guaranteed. When the swap space is not preserved and the memory is low, modifications to the mapping area can cause a segment violation signal.
map_locked//Locks the page of the map area, thus preventing the page from being swapped out of memory.
Map_growsdown//For the stack, telling the kernel VM system that the mapping area can be scaled down.
Map_anonymous//Anonymous mapping, the map area is not associated with any files.
Map_anon//map_anonymous's nickname, no longer used.
Map_file//compatible flag, ignored.
Map_32bit//The mapping area is ignored when the low 2gb,map_fixed of the process address space is specified. Currently this flag is only supported on the X86-64 platform.
Map_populate//Prepare the page table for file mapping by pre-reading. Subsequent access to the map area is not blocked by page violations.
Map_nonblock//is only meaningful when used with map_populate. Does not perform a read-ahead, only creates a page table entry for a page that already exists in memory.
FD: A valid file description word. It is generally returned by the open () function, and its value can also be set to-1, at which point the Map_anon in the flags parameter needs to be specified to indicate that an anonymous mapping is in progress.
Off_toffset: The starting point for the content of the mapped object.
Successfully returns the address of the mapped area
Sigbus//process access Invalid mappings to die from
The SIGSEGV//process attempts to write data in the read-only map area.
32.Sysconf
#include <unistd.h>
Long sysconf (int name);
The value returned by the Name property failed to return-1.
Example: Long pagesize = sysconf (sc_pagr_size); Get page size
#include <unistd.h>
Int getpagesize (void);
#include <asm/pages.h>
Int page_size = page_size;
33.munmap
#include <sys/mman.h>
int Munmap (void*addr,size_t len);
Removes a mapping of Len byte lengths starting from addr. The SIGSEGV signal is generated when the access is removed.
Successful return 0, failed to return-1;
34.mremap ()
#include <sys/mman.h>
#include <unistd.h>
#define _gnu_source
void * Mremap (void *addr,size_told_size,size_t new_size,unsigned long flags);
Modifying file sizes size from old to new
35.mprotect
#include <sys/mman.h>
Int mprotect (const void *addr,size_tlen,int prot);
Modify Map Zone Permissions
In some systems mprotect can only modify mmap allocated areas, and in Linux you can modify all
36.msync
#include <sys/mman.h>
Int msync (void *addr,size_t len,int flags);
Flag value
Ms_async synchronization operation A occurs, the system is scheduled to update, MYSNC immediately return.
Ms_invalidate all copies of this block map are invalidated and future operations on the map are written directly to the hard disk
Ms_sync all synchronous synchronizations, Msync returns after write execution is complete.
#include <sys/mman.h>
Int madvise (void *addr,size_t len,int len);
#include <fcntl.h>
int posix_fadvise (int fd, odd_toffset,off_t len,int advise);
#include <fcntl.h>
ssize_t readahead (int fd, off64_toffset,size_t count);
Synchronous synchronized, synchronous synchronous asynchronous asynchronous
Synchronization is not returned until the read-write operation has finished executing.
Asynchronous no read and write operations can be done to return
Synchronized ensure that events occur, are deeper, Nosynchronized writes are returned to the queue, and are only guaranteed to the kernel cache area.
Linux system programming File IO