Linux system calls for operating files

Linux system calls for operating files

Header files that need to be imported:

#include <unistd.h>

1. Open the file

Open an existing file

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

Create a new file and create permissions

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

Parameter Introduction

pathname: The path and name of the file to be opened

flags: Open flag

Logo Introduction:

The argument flags must include one of the following access modes:
O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-
only, write-only, or read/write, respectively.

O_RDONLY Open for read-only

O_RDWR Open for reading and writing

O_CREAT Create the file if it does not exist

O_APPEND Append to the end of the file

O_TRUNC Clear the file and rewrite the mode

The following symbolic constants are provided for mode:

S_IRWXU 00700 user (file owner) has read, write, and execute permission
                       

S_IRUSR 00400 user has read permission

S_IWUSR 00200 user has write permission

S_IXUSR 00100 user has execute permission

S_IRWXG 00070 group has read, write, and execute permission

S_IRGRP 00040 group has read permission

S_IWGRP 00020 group has write permission

S_IXGRP 00010 group has execute permission

S_IRWXO 00007 others have read, write, and execute permission

S_IROTH 00004 others have read permission

S_IWOTH 00002 others have write permission

S_IXOTH 00001 others have execute permission

Return value: file descriptor

2. Reading Files

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

Parameter Introduction

fd: the corresponding open file descriptor buf: the space for storing data count: the number of bytes of data planned to be read from the file at a time return value: the actual number of bytes read

3. Write a file

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

Parameter introduction:

fd: corresponding to the opened file descriptor buf: stores the data to be written count: how much data is planned to be written to the file at a time

4. Close

int close(int fd);

fd: the corresponding file descriptor

Analysis Questions

If the parent process opens a file first, can the child process share it after forking?

File Contents

insert image description here

Code

#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include<stdlib.h>

int main()
{
    char buff[128] = {0};

    int fd = open("myfile.txt", O_RDONLY);

    pid_t pid = fork();
    assert(pid != -1);

    if (pid == 0)
    {
        read(fd, buff, 1);
        printf("child buff = %s\n", buff);

        sleep(1);
        read(fd, buff, 1);
        printf("child buff = %s\n", buff);

    }
    else
    {
        read(fd, buff, 1);
        printf("parent buff = %s\n", buff);

        sleep(1);
        read(fd, buff, 1);
        printf("parent buff = %s\n", buff);
    }

    close(fd);

    exit(0);
}

Running results:

insert image description here

in conclusion :

Since the PCB of the child process created by fork is a copy of the parent process , the pointer to the open file in the file table in the child process PCB just copies the value in the parent process PCB, so the parent and child processes share all the file descriptors opened before the parent process fork.

insert image description here

Exercises

Complete the copy of a file (similar to command: cp)

The original file content is:

insert image description here

Code:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include<stdlib.h>
#include <assert.h>

int main(void)
{
    char buff[128] = {0};

    int fdr = open("myfile.txt", O_RDONLY);
    assert(fdr != -1);

    int fdw = open("newfile.txt", O_WRONLY | O_CREAT, 0600);
    assert(fdw != -1);

    int n = 0;
    while (n = read(fdr, buff, 128) > 0)
    {
        write(fdw, buff, n);
    }

    close(fdr);
    close(fdw);
    
    exit(0);
}

Running the example:

You can see that newfile.txt was created successfully

insert image description here

The difference between system calls and library functions

Difference: The implementation of system calls is in the kernel and belongs to kernel space, while the implementation of library functions is in the function library and belongs to user space.

System call execution process:

insert image description here

This is the end of this article about Linux system calls for operating files. For more information about Linux file system calls, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the Linux system call principle
  • Three ways to implement Linux system calls

<<:  Detailed explanation of angular two-way binding

>>:  Detailed explanation of using MySQL where

Recommend

The process of installing MySQL 8.0.26 on CentOS7

1. First, download the corresponding database fro...

An article to understand the advanced features of K8S

Table of contents K8S Advanced Features Advanced ...

An article to understand the use of proxies in JavaScript

Table of contents What is an agent Basic knowledg...

Two usages of iFrame tags in HTML

I have been working on a project recently - Budou...

How to implement horizontal bar chart with percentage in echarts

Table of contents Example Code Rendering Code Ana...

How to set the height of the autosize textarea in Element UI

After setting textarea input in Element UI to aut...

Installing the ping tool in a container built by Docker

Because the Base images pulled by Docker, such as...

How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

Today, I logged into the server and prepared to m...

How to reset the root password in Linux mysql-5.6

1. Check whether the MySQL service is started. If...

How to install ZSH terminal in CentOS 7.x

1. Install basic components First, execute the yu...

Vue: Detailed explanation of memory leaks

What is a memory leak? A memory leak means that a...

How to write the introduction content of the About page of the website

All websites, whether official, e-commerce, socia...

How to convert a string into a number in JavaScript

Table of contents 1.parseInt(string, radix) 2. Nu...

CentOS uses expect to remotely execute scripts and commands in batches

Sometimes we may need to operate servers in batch...