Home  >  Article  >  Web Front-end  >  A brief analysis of the POSIX standard of Node.js api

A brief analysis of the POSIX standard of Node.js api

青灯夜游
青灯夜游forward
2021-09-02 10:06:062128browse

What is POSIX? What's included? The following article will take you to understand the POSIX standard of Node.js api and the characteristics of Node.js api. I hope it will be helpful to you!

A brief analysis of the POSIX standard of Node.js api

[Recommended study: "nodejs Tutorial"]

If you have used the API of Node.js, will you feel Strange, why the name of the api is like this:

For example, create a directory:

const fs = require('fs');

fs.mkdir('/a/b/c', { recursive: true }, (err) => {
  if (err) throw err;
});

Create a process:

const childProcess = require('child_process');

childProcess.fork('a/b/c.js');
childProcess.execFile('a/b/dddd');
childProcess.exec('"/path/to/test file/test.sh" arg1 arg2');
childProcess.spawn('ls', ['-lh', '/usr']);

mkdir, fork, exec, spawn, etc., what are these names? Did you get up?

If you have used the linux command or the c function library, you will find that these apis also have this name in the command and c function library.

Why is this so? Are these APIs standard?

Yes, this is the POSIX standard

What is POSIX

POSIX is the abbreviation of portable operating system interface (portable operating system interface) , x means unix, that is, it is inherited from unix.

Because if the functions and system calls provided by different operating systems are different, then the source code of the upper-layer application based on the operating system will be different. This results in the code written on one platform not being able to be used on another platform. Compile on.

How to do it?

What if the APIs provided by each operating system are the same? No matter how the underlying operating system implements these capabilities, it only needs to expose the same API to the application. In this way, the source code is cross-platform and can be run after compilation on different operating systems.

The standard for the APIs exposed by this unified operating system is POSIX.

This POSIX standard can be understood as an interface defined in ts. As long as the API of this interface is implemented, it is compatible with the POSIX standard.

POSIX was originally an extension of Unix. Linux implemented the POSIX standard. Later, Windows was forced to be compatible with the POSIX standard due to pressure. Otherwise, many Linux applications would not be able to run on Windows. . The same goes for our commonly used osx.

So, POSIX is some standard interfaces for the operating system to provide capabilities to upper-level applications, including system calls, c function libraries, and shell commands.

The so-called standards refer to those recognized by the ISO International Organization for Standardization. This is an international organization with members in various countries and is an organization that formulates various international standards. POSIX is the ISO/IEC 9945 standard (IEC is the standardization organization for electronics). In fact, POSIX was proposed by IEEE, which is an American standardization organization. The standards it proposed are recognized by ISO and will become international standards. For example, POSIX is the IEEE Std 1003 standard they proposed, which is now recognized by ISO and has become ISO/IEC 9945. standard.

What is included in POSIX

Let’s take a look at what system calls are provided by Linux that supports POSIX (system calls refer to those provided in the kernel code Program):

Process control:

  • fork creates a new process
  • execv runs the executable file
  • exit terminates the process

File reading and writing

  • open open the file
  • close close the file descriptor
  • write write the file
  • read read the file
  • truncate truncate the file
  • fsync writes the part of the file in memory to disk

File system related

  • access determines whether the file is available Access
  • chdir Change the current working directory
  • chown Change the file owner or user group
  • stat Get file status information
  • mkdir Create directory
  • symlink Create symbolic link
  • unlink Delete link

etc

Many of these system calls have APIs with the same name in Node.js, and the shell also has the same name Command:

For example:

fs.stats
fs.access
fs.chown
fs.mkdir

fs.open
fs.close
fs.read
fs.write

child_process.fork
child_process.exec
child_process.execFile

etc.

Features of Node.js api

Node.js is a js At runtime, many apis that provide operating system capabilities are injected into js based on v8, and many of these api designs directly use POSIX standard api names without much abstraction.

Java's JRE (java runtime) also provides an abstraction of operating system capabilities, but those APIs have little to do with the operating system POSIX APIs, and incorporate many design patterns, such as io stream decoration device mode.

The Node.js API is characterized by not many abstractions, and many API names are very similar to Linux commands, close to POSIX standards. Therefore, when learning Node.js, you still need to learn linux commands. The two have a certain relationship in design.

Summary

The POSIX standard is a standard for operating system capabilities. It defines what APIs the operating system should expose to applications, including shell commands, c function libraries, and system calls. etc. standards. The POSIX standard makes applications portable across platforms at the source code level, just by compiling them on different platforms.

POSIX is an international standard recognized by ISO. It was first proposed by IEEE, a standards association in the United States. ISO is an organization that specializes in customizing international standards, with members from many countries participating.

The API of Node.js does not do a lot of abstraction, and most of its names are more similar to the POSIX standard API. This is its characteristic. In contrast, the API exposed by JRE to Java does. A lot of abstraction.

Because many of the APIs of Node.js are close to the C function library and shell commands, learning Node.js combined with learning the shell commands, or those who know C can learn more about the system function library. reward.

Understanding POSIX is the prerequisite for understanding Node.js api design and learning Node.js well.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief analysis of the POSIX standard of Node.js api. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete