There are a lot of system calls in Linux, but there are a couple of things that are immutable: Create, open, write, read, close, delete, and so on the most basic operation, as people often say, Linux everything we can as a file to deal with, since it is a file, then the above several operations is necessary, There are some advanced, I will be introduced in succession.
Think of this function, think of our open does not have the ability to create files before, then the creat is how brilliant, now open like Tencent's high-handed, what all do, is simply an encyclopedia. Don't say much nonsense, introduce under creat ()
Located in #include <fcntl.h>
int creat (const char *pathname, mode_t mode);
Successfully returned as a write-only open file descriptor, and returns 1 if an error occurs.
Note: This function is equivalent to:
Open (Pathname, o_wronly | O_creat | O_trunc, mode);
Here's an example
It is strange that I did not include fcntl.h header file, the program did not error, I hope to know the comrades leave a message, let me also long under the experience.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main (void)
{
int FD =-1;
FD = creat ("Tmp.lock", 00644);
if (0 > FD)
{
printf ("errno:%s\n", Strerror (errno));
}
else
{close
(FD);
}
printf ("Execute OK:%d\n", FD); After the shutdown and then print, the value of FD should not change (test only know). return
0;
The purpose of writing this function is to determine whether the file created by Creat has been created and then called again if there will be a eexist error, and the result is disappointing.
The same program executes several times, the result is not changed, fd=3.
But learn something, take it slow, start all over again, No.
Open is not.
Need fcntl.h------{Little brother really confused, is not the standard library contains creat, why open need to refer to this head file. }
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
int main (void)
{
int fd =-1;
FD = open ("Tmp.lock", O_wronly | O_creat | O_trunc, 00644);
printf ("fd:%d\n", FD);
if (0 > FD)
{
printf ("errno:%s\n", Strerror (errno));
}
else
{close
(FD);
}
printf ("Execute OK");
return 0;
}
The results of the implementation are ibid.
I used the next O_EXCL flag.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h >
int main (void)
{
int fd =-1;
FD = open ("Tmp.lock", O_wronly | O_creat | O_EXCL, 00644);
printf ("fd:%d\n", FD);
if (0 > FD)
{
printf ("errno:%s\n", Strerror (errno));
}
else
{close
(FD);
}
printf ("Execute OK");
return 0;
This time I returned to eexist, FD =-1, which is just what I meant.
About the flag of some columns of open, commonly used also on those several, listed to everybody a reference:
Http://os.51cto.com/art/201108/286554.htm
Description (DESCRIPTION)
Open () is typically used to convert a pathname to a file descriptor (a non-negative small integer that will be used in I/O operations like Read, write, and so on). When the Open () call succeeds, it returns a new file descriptor (the minimum value for which the unused descriptor is always taken). This call creates a new open file that assigns a new unique file descriptor that is not shared with any other program running (but can be shared through fork (2) system calls). This new file descriptor is used later in the function that opens the file operation. (Reference Fcntl (2)). File read and write pointers are placed on file headers
The parameter flags are obtained by o_rdonly, o_wronly, or O_rdwr (which indicates that the file is opened in read-only, write-only, or read-write mode) and the following 0 or more optional modes are bitwise--OR operations:
O_creat
A new file will be created if the file does not exist. The owner (user ID) of the new file is set to the ID of the valid user for this program. The same file-owning grouping is also set to the ID of the valid group for this program or the group ID of the upper directory (this depends on the file system type, mount options and the schema of the upper directory, reference, the Ext2 file system described in Mount (8) Load Options bsdgroups and Sysvgroups)
O_excl
By O_creat, the file is generated, and if the file already exists, the open error occurs and the call fails. If a symbolic join exists, the pointing file of its join pointer is ignored. O_EXCL is broken on NFS file systems, programs which rely in it for performing locking tasks would contain a race condition . The solution for performing atomic file locking using a-lockfile are to create a unique file on the same fs (e.g., Incorpor ating hostname and PID), Uselink (2) to make a link to the lockfile. If link () returns 0, the lock is successful. Otherwise, use STAT (2) on the unique file to check if it link count has increased to 2, in which case the lock is also su Ccessful.
O_noctty
If pathname references a terminal device---reference TTY (4)---even if the process has no control terminal, the terminal will not become the control terminal of the process.
O_trunc
If the file already exists and is a normal file, open mode is writable (that is, the file is opened in O_RDWR or o_wronly mode), the length of the file is set to zero, and the existing content is discarded. If the file is a FI FO or terminal equipment files, O_trunc flags are ignored. The role of other o_trunc is not specified (in many Linux versions, it is usually ignored, and some other versions will return an error)
O_append
The file opens in Append mode. Before writing, the file read and write pointer is placed at the end of the file. As if with Lseek. O_append may leads to corrupted files in NFS file systems if more than one process appends the data to a file at once. This are because NFS does not support appending to a file, so the client kernel has to simulate it, which can ' t was done wit Hout a race condition.
O_nonblock or O_ndelay
Open files can be opened in non block (non-blocking) mode. The file is not opened, and the returned file descriptor cannot be used for subsequent operations, but rather to cause the caller to wait. This mode is for FIFO (named pipe) processing, reference FIFO (4). This mode has no effect except FIFO.
O_sync
Open file for I/O synchronization. Any write to a file through a file descriptor interrupts the calling process until the data is actually written to the hardware. Other, reference restrictions.
O_nofollow
If pathname is a symbolic join, the open fails. This is an extension of FreeBSD, which has been introduced into Linux since version 2.1.126. The definition of this parameter is included in the header file since the GLIBC2.0.100 library;
Kernel 2.1.126 has previously ignored its use.
O_directory
If pathname is not a directory, it fails to open. This parameter is unique to Linux and is added in kernel 2.1.126 to avoid denial-of-service problems when calling FIFO or tape devices, but should not be used outside of execution opendir.
O_largefile
Support for large file systems in 32-bit systems allows opening of large files with 31 bits that cannot represent their length.
After the file is opened, these optional parameters can be changed by FCNTL.
When a new file is created, parameter mode specifies the permission to use. He is also usually modified by umask. Therefore, the general permissions for new files are (mode & ~umask). Note mode is only applied to future use of this new file; The open call creates a new read-only file, but still returns a read-write file descriptor.
The following are some of the specific parameters of mode:
S_irwxu
00700 allow the owner to read, write and execute files
S_IRUSR (S_iread)
00400 allow the owner of the file to read the file
S_IWUSR (S_iwrite)
00200 allow the owner of the file to write the file
S_IXUSR (s_iexec)
00100 the owner of the file is allowed to execute the file
S_irwxg
00070 allow files to be read, written and executed in groups
S_irgrp
00040 allow files to be in the grouped read file
S_iwgrp
00020 allow files in the group to write
S_ixgrp
00010 allow files in the grouped execution file
S_irwxo
00007 allow other users to read, write and execute files
S_iroth
00004 allow other users to read files
S_iwoth
00002 allow other users to write files
S_ixoth
00001 allow other users to execute files
Mode is only valid when using o_creat in flags, otherwise it is ignored.
creat equal to the open argument flags equals o_creat| o_wronly| O_trunc.
return value
Both the open and creat return a new file descriptor (1 If an error occurs, and the error message is set in errno). Note Open opens a device-specific file, but creat cannot be created and needs to be replaced with Mknod (2).
On the NFS file systems with UID mapping enabled, the open may return a file descriptor but e.g. read (2) requests are denied with Eacces. This is because the client performs open by checking the permissions, but UID mapping are performed by the server upon read and write requests.
If the file is newly established, his atime (last access Time), CTime (creation time), Mtime (modification time) are modified to the current time, the atime of the upper directory, and CTime are also modified. Other, if the file is modified by the O_trunc parameter, its ctime, mtime field is also set to the current time.
ERRORS error message
Eexist
The parameter o_creat and O_EXCL are used, but the file (pathname) already exists.
Eisdir
The filename (pathname) is a directory, and it involves a write operation.
Eacces
Access requests are not allowed (insufficient permissions), there is a directory in the file name (pathname) that does not allow searching (no Execute permissions), or the file does not exist and write operations to the upper directory are not allowed.
Enametoolong
FileName (pathname) Too long
Enoent
The directory (pathname) does not exist or is a dangling symbolic join.
Enotdir
Pathname not a subdirectory
Enxio
Using O_nonblock | O_wronly, named file is FIFO, read file has not opened file, or, open a device private file and the corresponding device does not exist
Enodev
The file (pathname) refers to a device-specific file, and the corresponding device does not exist. (This is a bug-enxio of Linux kernel will definitely be returned.)
Erofs
The file (pathname) is a read-only file, and a write operation is requested.
Etxtbsy
A file (pathname) is an executable file that is being executed, and a write operation is requested.
Efault
Pathname in an address space that you cannot access.
Eloop
Too many symbolic joins are encountered or indicate o_nofollow when the pathname is decomposed but pathname is a symbolic join
Enospc
Pathname will be created, but the device has no space to store pathname files
Enomem
The available core memory (kernel memory) is not enough
Emfile
The number of files opened by the program has reached its maximum value.
Enfile
The total number of files opened by the system has reached its limit