[Nodejs principle] core libraries Libuv Getting Started (Hello World article)

What Libuv that?

1 Introduction

Libuv is a high-performance, event-driven asynchronous I / O library, which itself is written in C, with high portability. libuv encapsulates the different platform underlying implementation model for asynchronous IO, so it itself has the ability to cross-platform Windows, Linux can be used.

Libuv designed specifically for Node.js, but it later because of this efficient model event-driven asynchronous IO gradually by many languages and projects adopted and used as their base library, like Luvit, Julia, pyuv, there are many based on its project [1]

2. Libuv history

Nodejs just came out, the bottom is not using libuv, but libev, libev itself is an asynchronous IO library, but it only works in the POSIX [2] to use the system. With more and more people use nodejs, because of the huge amount of windows users, so start thinking about cross-platform capabilities of Nodejs .
This time Nodejs provides libuv as an abstract encapsulation layer, on Unix systems, and by encapsulating libev libio calls of linux epoll or kqueue, on the Windows platform IOCP [3] encapsulation, and since then Nodejs with the cross-platform capabilities , the intermediate layer itself, as an abstract libuv provide cross-platform, it is determined using libev / libio or IOCP system according to later versions of the node-v0.9.0, libuv libev the contents removed.

Libuv features

全功能的事件循环基于epoll、kqueue、IOCP、event ports异步的TCP和UDP套接字异步的DNS解析异步的文件和文件系统操作文件系统事件ANSI转义代码控制的TTYPC包括套接字共享,使用Unix域套接字或有名管道(Windows)子进程线程池信号处理高分辨率时钟线程和同步原语

Libuv入门实战之Hello world

笔者是MacOS的系统,其他操作系统可以查看官方安装文档[4],接下下来介绍一下 libuv的安装与使用。

一、安装libuv

1.下载libuv的源码

git clone https://github.com/libuv/libuv.git

2.下载完成后进入项目,依次执行下面命令,进行编译安装,遇到问题 可以在libuv的ISSUE[5] 下寻找答案或者留言提问。

sh autogen.sh./configuremakemake checkmake install

  3.安装完成后,查看一下自己本地 /usr/local/include/ 下是否有uv.h等头文件,如果存在即安装完成。

    4.查看静态链接库文件/usr/local/lib/libuv.a 是否存在。

二、libuv的Hello word

下述示例默认有c语言环境,可以输入gcc验证一下,没有c环境的话请搜索安装gcc环境。这边的hello world参考的文档里推荐的入门示例[6]

1.新建文件 main.c。

vim main.c

     2.文件写入如下内容。

// main.c#include <stdlib.h>#include <stdio.h>#include <stdlib.h>#include <uv.h>
int main() { uv_loop_t *loop = malloc(sizeof(uv_loop_t)); uv_loop_init(loop);
printf("Now quitting.\n"); uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop); free(loop); return 0;}

  3.如果提示没有uv.h,可以试试下面的头替代。

#include </usr/local/include/uv.h>

4.执行编译链接命令。

gcc -o main main.c  -luv或者gcc -o main main.c  /usr/local/lib/libuv.a

5.执行可执行文件 ./main  进行验证。

  6.程序解释:首先uv_loop_init开启了一个loop,uv_run执行,但是loop是空的,没有要处理的事件,然后很快uv_loop_close退出。

目前一个使用libuv做的入门实例已完成,接下来笔者将会解析一下内部的Api,做一些实例分享出来,有兴趣的请关注笔者微信公众号哦 。

References

[1] Projects that use libuv: https://github.com/libuv/libuv/wiki/Projects-that-use-libuv
[2] 是一套操作系统 API 规范。一般而言,遵守 POSIX 规范的操作系统指的是 UNIX、Linux、Mac OS X 等: 
[3] Windows 平台上的内核事件通知相应的机制Input/Output Completion Port: 
[4] 安装文档: https://github.com/libuv/libuv#build-instructions
[5] ISSUE: https://github.com/libuv/libuv/issues
[6] 入门示例: https://github.com/luohaha/Chinese-uvbook/blob/master/source/basics_of_libuv.md#hello-world

如上内容均为自己总结,难免会有错误或者认识偏差,如有问题,希望大家留言指正,以免误人,若有什么问题请留言,会尽力回答之。如果对你有帮助不要忘了分享给你的朋友或者点击右下方的“在看”哦!也可以关注作者,查看历史文章并且关注最新动态,助你早日成为一名全栈工程师!

发布了3363 篇原创文章 · 获赞 36 · 访问量 14万+

Guess you like

Origin blog.csdn.net/cpongo9/article/details/103351148