Table of Contents
File copy
Process Creation
Summary
Home Web Front-end JS Tutorial A brief discussion on Node.js+COW technology for process creation and file copying

A brief discussion on Node.js+COW technology for process creation and file copying

Sep 17, 2021 am 10:07 AM
node.js File copy

This article will take you to understand COW (Copy-On-Write) technology and introduce the process creation and file copy application of COW technology Node.js. I hope it will be helpful to everyone!

A brief discussion on Node.js+COW technology for process creation and file copying

COW is not a cow, it is the abbreviation of Copy-On-Write, which is a technology that is copying but not completely copying.

Generally speaking, copying is to create two identical copies. The two copies are independent:

A brief discussion on Node.js+COW technology for process creation and file copying

However, sometimes copying does not work. No matter how necessary, you can reuse the previous one. At this time, you can just quote the previous one and copy the corresponding part of the content when writing the content. In this way, if the content is used for reading, copying is eliminated, but if writing is required, part of the content will actually be copied for modification.

A brief discussion on Node.js+COW technology for process creation and file copying

This is called "copy-on-write", also known as Copy-On-Write.

The principle is very simple, but it is very common in the memory management and file system of the operating system. Node.js has also become "lazy" because of this technology.

In this article, we will explore the application of Copy-On-Write in process creation and file copying in Node.js. [Recommended learning: "nodejs tutorial"]

File copy

The most common idea for file copying is to write a completely identical copy file content to another location, but there are two problems with this:

  • Write exactly the same content. If the same file is copied hundreds of times, then the same content will be created hundreds of times. Times? What a waste of hard disk space
  • What if the power goes off halfway through writing? How to restore overwritten content?

How to do it? At this time, operating system designers thought of COW technology.

Using COW technology to realize file copying perfectly solves the above two problems:

  • Copying just adds a reference to the previous content. If it is not modified, it will not actually be copied, only The corresponding data blocks are not actually copied until the content is modified for the first time, thus avoiding the waste of a large amount of hard disk space.
  • When writing a file, modifications will be made to another free disk block first, and then copied to the target location after the modification is completed, so that there will be no problem of being unable to roll back after a power outage

You can use the Copy-On-Write mode in the fs.copyFile api of Node.js:

By default, copyFile will write to the target file, overwriting the original content

const fsPromises = require('fs').promises;

(async function() {
  try {
    await fsPromises.copyFile('source.txt', 'destination.txt');
  } catch(e) {
    console.log(e.message);
  }
})();
Copy after login

But You can specify the copy strategy through the third parameter:

const fs = require('fs');
const fsPromises = fs.promises;
const { COPYFILE_EXCL, COPYFILE_FICLONE, COPYFILE_FICLONE_FORCE} = fs.constants;

(async function() {
  try {
    await fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_FICLONE);
  } catch(e) {
    console.log(e.message);
  }
})();
Copy after login

There are 3 supported flags:

  • COPYFILE_EXCL: If the target file already exists, an error will be reported (the default is to overwrite)
  • COPYFILE_FICLONE: Copy in copy-on-write mode. If the operating system does not support it, it will switch to real copy (the default is direct copy)
  • COPYFILE_FICLONE_FORCE: Copy in copy-on-write mode. If the operating system does not support it, an error will be reported.

These three constants are 1, 2, and 4. They can be combined by bitwise OR and passed in:

const flags = COPYFILE_FICLONE | COPYFILE_EXCL;
fsPromises.copyFile('source.txt', 'destination.txt', flags);
Copy after login

Node.js Supports the copy-on-write technology of the operating system, which can improve performance in some scenarios. It is recommended to use the COPYFILE_FICLONE method, which is better than the default method.

Process Creation

Fork is a common way to create a process, and its implementation is a copy-on-write technology.

We know that the process is divided into three parts in the memory: code segment, data segment, and stack segment:

  • Code segment: stores the code to be executed
  • Data segment: stores some global data
  • Stack segment: stores execution status

If a new process is created based on this process, these three parts of memory must be copied. And if these three parts of memory have the same content, then memory space is wasted.

So fork does not actually copy the memory, but creates a new process and references the memory of the parent process. When the data is modified, this part of the memory will actually be copied.

A brief discussion on Node.js+COW technology for process creation and file copying

This is why process creation is called fork, which is a fork, because it is not completely independent, but a certain part is forked into two parts, but Most of it is still the same.

But what if the code to be executed is different? At this time, you need to use exec, which will create a new code segment, data segment, stack segment, and execute new code.

The fork and exec APIs can also be used in Node.js:

fork:

const cluster = require('cluster');

if (cluster.isMaster) {
  console.log('I am master');
  cluster.fork();
  cluster.fork();
} else if (cluster.isWorker) {
  console.log(`I am worker #${cluster.worker.id}`);
}
Copy after login

exec:

const { exec } = require('child_process');
exec('my.bat', (err, stdout, stderr) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(stdout);
});
Copy after login

fork is the basis for Linux process creation , which shows how important copy-on-write technology is.

Summary

Copying multiple copies of the same content is undoubtedly a waste of space, so the operating system uses Copy-On- when copying files and copying memory during process creation. Write technology will only copy when it is actually modified.

Node.js supports the setting of flags of fs.copyFile. You can specify COPYFILE_FICLONE to use Copy-On-Write to copy files. It is also recommended that you use this method to save hard disk space and improve file copying. performance.

The fork of a process is also an implementation of Copy-On-Write. It does not directly copy the code segment, data segment, and stack segment of the process to new content, but refers to the previous ones and only when modifying them. Will do a real memory copy.

In addition, Copy-On-Write has many applications in the implementation of Immutable and in distributed read-write separation and other fields.

COW makes Node.js "lazy" but performs better.

Original address: https://juejin.cn/post/6999497362255118366

Author: zxg_God said there must be light

More programming For related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of A brief discussion on Node.js+COW technology for process creation and file copying. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

How does Node.js do GC (garbage collection)? The following article will take you through it.

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

See all articles