Reading Notes for advanced programming in UNIX environment-file I/O

Source: Internet
Author: User
1. Open or create a file # include <fcntl. h>
Int open (const char * pathname, int Oflag,.../* mode_t mode */);
Returned value: If successful, the system returns the file descriptor. If an error occurs, the system returns-1.

Oflag options:
O_rdonly
O_wronly
O_rdwr

One of the preceding three constants must be specified and only one can be specified.
The following constants are optional:
The o_apped is appended to the end of the file each time it is written.
O_creat is created if the file does not exist.
If o_excl is specified at the same time and the file already exists, an error occurs. Otherwise, the file is created.
O_trunc if the file exists and the file is successfully written or read/write, the truncation of the file is 0.
O_noctty
O_nonblock if pathname specifies a FIFO, a special file, or a special character file, this option sets the non-group mode for this open operation of the file and subsequent I/O operations.

O_dsync waits for the physical I/O to complete each write operation, but does not wait for the file attribute to be updated.
O_rsync
Every write operation of o_sync waits for the completion of physical I/O, including the I/o Required for file attribute update caused by write.



# Include <fcntl. h>
Int create (const char * pathname, mode_t mode );
Returned value: if the operation succeeds, the system returns the file descriptor that is opened only. If an error occurs, the system returns-1.

This function is equivalent:
Open (pathname, o_wronly | o_creat | o_trunc, Mode)


2. Close an opened file # include <unistd. h>
Int close (INT filedes );
Returned value: if the request is successful, 0 is returned. If the request is incorrect,-1 is returned.

When a file is closed, all locks of the process on the file will be released.
When a process ends, the kernel automatically closes all open files.


3. Set the offset for an opened file # include <fcntl. h>
Off_t lseek (INT filedes, off_t offset, int whence );
Return Value: If successful, the system returns the new file offset. If an error occurs, the system returns-1.

Whence value:
Seek_set sets the file offset to offset bytes from the start of the file.
Seek_cur sets the file offset to the current value plus offset, which can be positive or negative.
Seek_end sets the file offset to the file length plus offset, which can be positive or negative.

The offset cannot be set for pipelines, FIFO, and network sockets.


Iv. file read/write
# Include <unistd. h>
Ssize_t read (INT filedes, void * Buf, size_t nbytes );
Return Value: if the result is successful, the number of bytes read is returned. If the result is reached the end of the file, 0 is returned. If an error occurs,-1 is returned.

# Include <unistd. h>
Ssize_t write (INT filedes, void * Buf, size_t nbytes );
Return Value: If successful, the number of written bytes is returned. If an error occurs, the value-1 is returned.


V. File Sharing


6. copy the file descriptor # include <unistd. h>
Int DUP (INT filedes );
Int dup2 (INT filedes, filedes2 );
Returned value: if a new file descriptor is returned successfully,-1 is returned if a file descriptor fails.

DUP returns the currently available minimum file descriptor.
Dup2 returns the file descriptor specified by filedes2. If filedes2 already exists, disable it first.


VII. file synchronization
# Include <unistd. h>
Int fsync (INT filedes );
Int fdatasync (INT filedes );
Void sync ();

Sync this function only adds all modified block caches to the write queue. It does not return after the real write disk ends. The system update daemon periodically calls this function (30 seconds ).
Fsync takes effect only for the filedes Descriptor and waits until the disk write operation ends and returns
Fdatasync is similar to fsync, but it only affects the data part of the file and does not synchronously update the file attributes.

# Include <fcntl. h>
Int fcntl (INT filedes, int cmd,.../* Arg */);
Returned value: if the call succeeds, it depends on CMD. If the call fails,-1 is returned.

8. File Control

Fcntl features:

Copy an existing descriptor cmd = f_dupfd
GET/set file descriptor mark cmd = f_getfd/f_setfd
There is currently only one flag: fd_cloexec
GET/set the File status mark cmd = f_getfl/f_setfl
File status flag: o_rdonly read-only
O_wronly write-only open
O_rdwr read/write Enabled
O_append * append each write.
O_nonblock * non-blocking mode
O_sync * Wait for the write to complete (data and attributes)
O_dsync * Wait for the write to complete (only data)
O_rsync * synchronous read and write
O_fsync *
O_async *
Which can be modified if f_setfl is added with the * sign?
Obtain/set asynchronous I/O ownership cmd = f_getown/f_setown
Obtain/set the record lock cmd = f_getlk/f_setlk/f_setlkw

# Include <unistd. h>
Int IOCTL (INT filedes, int request ,...);
Return Value: if an error is returned,-1 is returned, and other return values are returned successfully.

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.