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

Detailed explanation of setting resource cache in nginx

I have always wanted to learn about caching. Afte...

Example of implementing colored progress bar animation using CSS3

Brief Tutorial This is a CSS3 color progress bar ...

Do you know the weird things in Javascript?

Our veteran predecessors have written countless c...

Before making a web page, let’s take a look at these so-called specifications

This article has compiled some so-called specific...

The specific use and difference between attribute and property in Vue

Table of contents As attribute and property value...

How to read the regional information of IP using Nginx and GeoIP module

Install GeoIP on Linux yum install nginx-module-g...

Summary of MySQL's commonly used database and table sharding solutions

Table of contents 1. Database bottleneck 2. Sub-l...

Detailed code for adding electron to the vue project

1. Add in package.json "main": "el...

Mysql: The user specified as a definer ('xxx@'%') does not exist solution

During the project optimization today, MySQL had ...

Appreciation of the low-key and elegant web design in black, white and gray

Among classic color combinations, probably no one...

Solution to overflow of html table

If the table is wide, it may overflow. For exampl...

Implementation of MySQL's MVCC multi-version concurrency control

1 What is MVCC The full name of MVCC is: Multiver...

Solution to MySQL root password error number 1045

Stop MySQL Service Windows can right-click My Com...