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 example of MySQL data storage process parameters

There are three types of MySQL stored procedure p...

Native JavaScript to implement random roll call table

This article example shares the specific code of ...

How to build lnmp environment in docker

Create a project directory mkdir php Create the f...

Docker container log analysis

View container logs First, use docker run -it --r...

Analysis of common usage examples of MySQL process functions

This article uses examples to illustrate the comm...

MySQL 8 new features: Invisible Indexes

background Indexes are a double-edged sword. Whil...

How to define data examples in Vue

Preface In the development process, defining vari...

A simple way to restart QT application in embedded Linux (based on QT4.8 qws)

Application software generally has such business ...

Does MySql need to commit?

Whether MySQL needs to commit when performing ope...

Detailed explanation of nginx proxy_cache cache configuration

Preface: Due to my work, I am involved in the fie...

HTML Tutorial: Unordered List

<br />Original text: http://andymao.com/andy...

MySQL scheduled task example tutorial

Preface Since MySQL 5.1.6, a very unique feature ...

SQL merge operation of query results of tables with different columns

To query two different tables, you need to merge ...