File related system calls in Linux Environment

System call:
System calls are some functions provided by the operating system for programmers to call. These calls have been encapsulated in the form of C functions, but they are not part of standard C.

General applications run in user mode (using 0 to 3G memory), and system calls work in kernel mode (using 3 to 4G memory).

The commonly used standard library functions run in the user state most of the time, and the bottom layer occasionally calls the system call to enter the kernel state.

The code of system call is a part of the kernel, and its external interfaces are defined in the shared library (, LD Linux. So) with functions. The implementation of these interfaces uses soft interrupts to enter the kernel state and execute the real system call.

All documents:
UNIX/Linux abstracts services and devices into files for the operating system, and provides a set of simple and unified interfaces, which are file reading and writing.
In other words, any object in UNIX/Linux system can be treated as a special file and accessed in the form of file.

Document classification:
Directory file, device file, Socket file, pipeline file, ordinary file, link file

File related system calls:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags);

Function: open file
pathname: the path of the file
flags: how to open a file
O_RDONLY read only
O_WRONLY write only
O_RDWR read / write
O_APPEND: the file position pointer is at the end
O_ Create if the creat file does not exist
O_EXCL if the file exists, the creation fails
O_TRUNC empty if file exists
O_NDELAY is non blocking, and the operation after opening the file is carried out in non blocking mode.
O_SYNC synchronization: wait for the data to be written to the underlying hardware before returning.
O_ASYNC is asynchronous. When the file is read / written, it sends a signal SIGIO to the calling process.
Return value: FILE descriptor, similar to FILE * of standard library, represents an open FILE.

int open(const char *pathname, int flags, mode_t mode);

Function: creating files
flags: O_CREATE
S_IRWXU 00700 owner read / write execution permission
S_IRUSR 00400 owner read
S_ Written by iwusr 00200 owner
S_IXUSR 00100 owner execution
S_IRWXG 00070 same group read / write execution permission
S_IRGRP 00040 same group reading
S_IWGRP 00020 write in the same group
S_IXGRP 00010 executed in the same group
S_IRWXO 00007 other read / write execution permissions
S_IROTH 00004 other read
S_IWOTH 00002 other write
S_IXOTH 00001 other execution

int creat(const char *pathname, mode_t mode);

Function: creating files
mode: same as open

#include <unistd.h>

ssize_t write(int fd, const void *buf, size_t count);

Function: write the data in memory into the file
fd: file description, that is, the return value of open.
buf: first address of memory to be written
count: number of bytes to write
Return value: the number of bytes successfully written

ssize_t read(int fd, void *buf, size_t count);

Function: read data from file to memory
fd: file description, that is, the return value of open.
buf: the first memory address where data is stored
count: the number of bytes to read
Return value: the number of bytes actually read

int close(int fd);

Function: close file
Return value: 0 for success and - 1 for failure.

Exercise 3: use system IO to implement a cp command with coverage test.
./cp src dest
Target file exists, overwrite (y/n)?

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc,const char* argb[])
	if(3 != argc)
		printf("User:cp src dest\n");
		return -1;
	int fd = open(argb[1],O_RDONLY);
	if(0 > fd)
		printf("The source file does not exist!\n");
		return -1;
	int fd2 = open(argb[2],O_WRONLY|O_EXCL);
	if(0 < fd2)
		printf("File already exists, overwrite?y/n\n");
		int cmd;
		cmd = getchar();
		if('n' == cmd || 'N' == cmd) return 0;
	int fd3 = open(argb[2],O_WRONLY|O_CREAT,0644);
	if(0 > fd3)
		return -1;
	char buf[1024] = {};

usage method:
gcc cp.c -o cp is compiled into an executable named CP
. / cp src dest execute: copy src file to dest file

Tags: Linux Operating System

Posted by ghostdog74 on Sat, 14 May 2022 06:43:50 +0300