linux file_systm

file_operation

open

#include <fcntl.h>
int open(const char path, int oflag, … / mode_t mode */ );
int openat(int fd, const char path, int oflag, … / mode_t mode */ );
Both return: file descriptor if OK, −1 on error

oflag (five flag, must choose one ,and only choose one):
O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing.
O_EXEC Open for execute only.
O_SEARCH Open for search only (applies to directories)., None of the versions of the operating systems covered in this book support O_SEARCH yet.

oflag(can add )
O_APPEND Append to the end of file on each write. We describe this option in
detail in Section 3.11.
O_CLOEXEC Set the FD_CLOEXEC file descriptor flag. We discuss file descriptor
flags in Section 3.14.
O_CREAT Create the file if it doesn’t exist. This option requires a third argument
to the open function (a fourth argument to the openat function) — the
mode, which specifies the access permission bits of the new file. (When
we describe a file’s access permission bits in Section 4.5, we’ll see how
to specify the mode and how it can be modified by the umask value of a
process.)
O_DIRECTORY Generate an error if path doesn’t refer to a directory.
O_EXCL Generate an error if O_CREAT is also specified and the file already
exists. This test for whether the file already exists and the creation of
the file if it doesn’t exist is an atomic operation. We describe atomic
operations in more detail in Section 3.11.
O_NOCTTY If path refers to a terminal device, do not allocate the device as the
controlling terminal for this process. We talk about controlling
terminals in Section 9.6.
O_NOFOLLOW Generate an error if path refers to a symbolic link. We discuss symbolic
links in Section 4.17.
O_NONBLOCK If path refers to a FIFO, a block special file, or a character special file,
this option sets the nonblocking mode for both the opening of the file
and subsequent I/O. We describe this mode in Section 14.2.
In earlier releases of System V, the O_NDELAY (no delay) flag was introduced. This
option is similar to the O_NONBLOCK (nonblocking) option, but an ambiguity was
introduced in the return value from a read operation. The no-delay option causes a
read operation to return 0 if there is no data to be read from a pipe, FIFO, or device,
but this conflicts with a return value of 0, indicating an end of file. SVR4-based
systems still support the no-delay option, with the old semantics, but new
applications should use the nonblocking option instead.
O_SYNC Have each write wait for physical I/O to complete, including I/O
necessary to update file attributes modified as a result of the write.
We use this option in Section 3.14.
O_TRUNC If the file exists and if it is successfully opened for either write-only or
read–write, truncate its length to 0.
ptg10805159
64 File I/O Chapter 3
O_TTY_INIT When opening a terminal device that is not already open, set the
nonstandard termios parameters to values that result in behavior that
conforms to the Single UNIX Specification. We discuss the termios
structure when we discuss terminal I/O in Chapter 18.
The following two flags are also optional. They are part of the synchronized input and
output option of the Single UNIX Specification (and thus POSIX.1).
O_DSYNC Have each write wait for physical I/O to complete, but don’t wait for
file attributes to be updated if they don’t affect the ability to read the
data just written.
The O_DSYNC and O_SYNC flags are similar, but subtly different. The O_DSYNC flag
affects a file’s attributes only when they need to be updated to reflect a change in the
file’s data (for example, update the file’s size to reflect more data). With the O_SYNC
flag, data and attributes are always updated synchronously. When overwriting an
existing part of a file opened with the O_DSYNC flag, the file times wouldn’t be
updated synchronously. In contrast, if we had opened the file with the O_SYNC flag,
every write to the file would update the file’s times before the write returns,
regardless of whether we were writing over existing bytes or appending to the file.
O_RSYNC Have each read operation on the file descriptor wait until any pending
writes for the same portion of the file are complete.
Solaris 10 supports all three synchronization flags. Historically, FreeBSD (and thus
Mac OS X) have used the O_FSYNC flag, which has the same behavior as O_SYNC.
Because the two flags are equivalent, they define the flags to have the same value.
FreeBSD 8.0 doesn’t support the O_DSYNC or O_RSYNC flags. Mac OS X doesn’t
support the O_RSYNC flag, but defines the O_DSYNC flag, treating it the same as the
O_SYNC flag. Linux 3.2.0 su


	{
   
		//ensure file no exist
		string file_name="1.txt";
		remove(file_name.data());
		//open file 
		int file=open("1.txt",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
		if(file==-1)return;
		//write file 
		string file_data="hello 2022";
		auto write_size=write(file,file_data.data(),file_data.size());
		LOGXA("write size:", write_size);
		close(file);
	}
create

#include <fcntl.h>
int creat(const char *path, mode_t mode);
Returns: file descriptor opened for write-only if OK, −1 on error

create = open(path, O_RDWR | O_CREAT | O_TRUNC, mode);
close

#include <unistd.h>
int close(int fd);
Returns: 0 if OK, −1 on error

lseek

#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
Returns: new file offset if OK, −1 on error

//whence == SEEK_CUR | SEEK_END |SEEK_SET
off_t currpos;
currpos = lseek(fd, 0, SEEK_CUR);//get now location 
read

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t nbytes);
Returns: number of bytes read, 0 if end of file, −1 on error

write

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t nbytes);
Returns: number of bytes written if OK, −1 on error

文件的read or write BUFFSIZE 一般是4K,效率最好,在文件的stat结构里面会发现其值

dup和dup2

先看file_sharing,再回来

#include <unistd.h>
/* Duplicate FD, returning a new file descriptor on the same file. */
int dup(int fd);

/* Duplicate FD to FD2, closing FD2 and making it open on the same file.
return fd2 */
int dup2(int fd, int fd2);

Both return: new file descriptor if OK, −1 on error

dup和dup2相当于在同一个进程中不同的fd指向了相同的file table entry

在这里插入图片描述
当有指针指向file table entry 时候,file table 并不会释放,close(fd)只是关闭其中一个指针.

//dup close(fd) 
void tl::testDup(){
   
	string file_name="dup.txt";
	int file_fd=open(file_name.data(),O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
	if(file_fd==-1)return;
	string file_data="hello world\n";
	int file_dup=dup(file_fd);
	auto write_size=write(file_fd, file_data.data(), file_data.size());
	LOGXA(file_fd,"write_size:", write_size,lseek(file_fd,0,SEEK_CUR));
	write_size=write(file_dup, file_data.data(), file_data.size());
	LOGXA(file_dup, "write_size:", write_size, lseek(file_dup, 0, SEEK_CUR));
	close(file_fd);


	write_size = write(file_fd, file_data.data(), file_data.size());
	LOGXA(file_fd, "write_size:", write_size, lseek(file_fd, 0, SEEK_CUR));

	write_size = write(file_dup, file_data.data(), file_data.size());
	LOGXA(file_dup, "write_size:", write_size, lseek(file_dup, 0, SEEK_CUR));
	close(file_dup);


}


result:
00:12:41 <A> [140648170555200] [testDup] 3  write_size:  12  12   (tl.cpp:21)
00:12:41 <A> [140648170555200] [testDup] 4  write_size:  12  24   (tl.cpp:23)
00:12:41 <A> [140648170555200] [testDup] 3  write_size:  -1  -1   (tl.cpp:28)
00:12:41 <A
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值