I. Opening and creating a file
Open the function to open or create a file and return the file descriptor.
# Include <fcntl. h>
Int open (const char * filename, int Oflag,.../* [model_t mode] */);
Parameter description:
Path Name of the file opened or created by filename
Oflag integer Oflag specifies the file opening method. The definition is as follows:
Flag |
Description |
O_rdonly |
Open a file in read-only mode |
O_wronly |
Open a file in write-only mode |
O_rdwr |
Open a file in read/write mode |
O_append |
Open an object in append Mode |
O_creat |
Create a file |
O_exec |
If o_creat is used and the file already exists, an error occurs. |
O_noblock |
Open a file in non-blocking mode |
O_trunc |
If the file already exists, delete the file content. |
O_rdonly, o_wronly, and o_rdwr can only use any one of them.
If the o_create flag is used, the int open (const char * pathname, int flags, mode_t mode) function is used. In this case, we need to specify the mode flag, indicates the object access permission. Mode can be a combination of the following conditions:
Flag |
Description |
S_irusr |
Users can read |
S_iwusr |
Users can write |
S_ixusr |
Users can execute |
S_irwxu |
Users can read, write, and execute |
S_irgrp |
Group readable |
S_iwgrp |
Group Writable |
S_ixgrp |
Group executable |
S_irwxg |
The Group can be read and written for execution. |
S_iroth |
Others can read |
S_iwoth |
Others can write |
S_ixoth |
Others can execute |
S_irwxo |
Others can read, write, and execute |
S_isuid |
Set User execution ID |
S_isgid |
Set the execution ID of the Group |
In addition to the above macro "or" logic to generate a flag, we can also use numbers to represent the various permissions of the file. Linux uses a total of five numbers to represent the permissions of the file: the first digit indicates the user ID, the second digit indicates the group ID, the third digit indicates the user's own permission, the fourth digit indicates the Group permission, and the last digit indicates the permissions of others. Each number can be 1 (Execution permission), 2 (write permission), 4 (read permission), 0 (none), or the sum of these values.
Ii. Close and delete files
The function closes an open file for close, and the function unlink deletes the file.
# Include <unistd. h>
Int close (INT filedes );
Int unlink (char * pathname );
Close closes the file descriptor filedes. If the file is successfully closed, 0 is returned. If the file fails,-1 is returned;
Unlink deletes a file. the space occupied by the file is released only when the number of links to the file is 0 and is not opened by other processes. If the function is successful, 0 is returned. Otherwise,-1 is returned.
Iii. file read/write
# Include <unistd. h>
Size_t read (int fd, void * Buf, size_t nbytes );
Size_t write (int fd, const void * Buf, size_t nbytes );
The parameter Buf is the pointer to the buffer, and nbytes is the buffer size (in bytes ). The read () function reads nbytes bytes from the file specified by the file descriptor FD to the buffer zone pointed to by the BUF. The returned value is the actual number of bytes read. Function write writes the nbyte bytes from the buffer to which the Buf points to the file pointed by the file descriptor FD. The returned value is the actual number of bytes written.
Iv. File locating
# Include <unistd. h>
Int lseek (int fd, offset_t offset, int whence );
Lseek moves the file read/write pointer to the offset byte relative to whence. When the operation is successful, the position of the returned file pointer relative to the file header. The whence parameter can use the following values:
Seek_set |
Start with relative file |
Seek_cur |
Current position of the relative file read/write pointer |
Seek_end |
Relative to the end of the file |
The offset value can be a negative value. For example, the following call can move the file pointer 5 bytes forward from the current position:
Lseek (FD,-5, seek_cur );
Because the return value of the lseek function is the position of the file pointer relative to the file header, the following returned values are the file length:
Lseek (FD, 0, seek_end );
5. File Buffering
# Include <unistd. h>
Int fsync (int fd );
The system calls fsync to write all data written into the file descriptor FD to a disk or another device. Similar to fflush in the standard file library programming, the system returns 0 when the system call is successful, otherwise,-1 is returned.
6. copy the file descriptor
# Include <unistd. h>
Int DUP (int fd );
Int dup2 (INT fdsrc, int fddes );
DUP copies the file descriptor FD to the currently unused minimum available File description.
Dup2 copies the file description fdsrc to fddes. If fddes is enabled, disable it. If fddes and fdsrc are equal, return directly. When both functions are successful, a new file description is returned; otherwise,-1 is returned.
VII. File Control
1. fcntl
# Include <fcntl. h>
Int fcntl (int fd, int cmd );
Int fcntl (int fd, int cmd, int Arg );
Int fcntl (int fd, int cmd, struct flock * Arg );
Parameter description:
FD file description.
CMD Operation Command.
Arg parameters used by the command.
The same as lock.
Fcntl executes various control commands on the file descriptor. The cmd parameter determines the specific command to be executed and whether the parameter Arg is required. The cmd option is as follows:
F_dupfd |
Copy file description |
F_setfd |
Set the description word mark of a file |
F_getfd |
Mark of File Reading status |
F_setfl |
Set File status flag |
F_getfl |
Mark of File Reading status |
F_getown |
Obtains the ID of the process or process group that receives the sigio or sigurg event signal on the file description FD. |
F_setown |
Set the process or process group ID that will receive sigio or sigurg event signals on the file description FD |
F_getlk |
Used for Lock Processing |
F_setlk |
Used for Lock Processing |
F_setlkw |
Used for Lock Processing |
When fcbtl is used for locking, its prototype is int fcntl (int fd, int cmd, struct flock * Arg). The structure struct flock is used to describe the lock information. The definition is as follows:
Struct flock {
Short l_type; // lock type, f_rdlck (applied) read lock, f_wrlck (applied) Write lock, f_unlck unlock
Short l_whence; // the relative location of the Start address of the lock area. The value is one of seek_set, seek_cur, and seek_end, which is similar to the whence in lseek;
Long l_start; // The start address offset of the lock area.
Long l_len; // The length of the lock area. 0 indicates the end of the file.
Short l_pid; // the ID of the process;
}
When fcntl is used for locking, the CMD has three values:
F_getlck
F_setlck
F_setlkw
2. file lock operation
In the use of the lock mechanism, the most common lock application, release testing, and so on.
A. Test the lock
/* View the object lock in len bytes starting from start */
Void seelock (int fd, int start, int Len)
{
Struct flock ARG;
Arg. l_type = f_wrlck;
Arg. l_whence = seek_set;
Arg. l_start = start;
Arg. l_len = Len;
If (fcntl (FD, f_getlk, & Arg) =-1) fprintf (stderr, "See lock failed./N ");
Else if (Arg. l_type = f_unlck) fprintf (stderr, "No lock from % d to % d/N", start, Len );
Else if (Arg. l_type = f_wrlck) fprintf (stderr, "Write lock from % d to % d, id = % d/N", start, Len, Arg. rochelle PID );
Else if (Arg. l_type = f_rdlck) fprintf (stderr, "Read lock from % d to % d, id = % d/N", start, Len, Arg. rochelle PID );
}
B. Apply for a read lock
/* Apply for a read lock in The LEN byte starting from start, blocking mode */
Void getreadlock (int fd, int start, int Len)
{
Struct flock ARG;
Arg. l_type = f_rdlck;
Arg. l_whence = seek_set;
Arg. l_start = start;
Arg. l_len = Len;
If (fcntl (FD, f_setlkw, & Arg) =-1) fprintf (stderr, "[% d] Set read lock failed./N", getpid ());
Else fprintf (stderr, "[% d] Set read lock from % d to % d/N", getpid (), start, Len );
}
C. Apply for a write lock
/* Apply for a write lock in The LEN byte starting from start, blocking mode */
Void getwritelock (int fd, int start, int Len)
{
Struct flock ARG;
Arg. l_type = f_wrlck;
Arg. l_whence = seek_set;
Arg. l_start = start;
Arg. l_len = Len;
If (fcntl (FD, f_setlkw, & Arg) =-1) fprintf (stderr, "[% d] Set write lock failed./N", getpid ());
Else fprintf (stderr, "[% d] Set write lock from % d to % d/N", getpid (), start, Len );
}
D. Release the lock.
/* Release the lock in len bytes starting from start */
Void releaselock (int fd, int start, int Len)
{
Struct flock ARG;
Arg. l_type = f_unlck;
Arg. l_whence = seek_set;
Arg. l_start = start;
Arg. l_len = Len;
If (fcntl (FD, f_setlkw, & Arg) =-1) fprintf (stderr, "[% d] Unlock failed./N", getpid ());
Else fprintf (stderr, "[% d] Unlock from % d to % d/N", getpid (), start, Len );
}