惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
V
Visual Studio Blog
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
IT之家
IT之家
博客园_首页
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - Franky
雷峰网
雷峰网
罗磊的独立博客
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
T
Tailwind CSS Blog
B
Blog RSS Feed
H
Help Net Security
T
The Blog of Author Tim Ferriss
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
C
CERT Recently Published Vulnerability Notes
博客园 - 三生石上(FineUI控件)
P
Palo Alto Networks Blog
I
Intezer
G
GRAHAM CLULEY
Engineering at Meta
Engineering at Meta
S
Securelist
J
Java Code Geeks
V
V2EX
Y
Y Combinator Blog
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog
Forbes - Security
Forbes - Security
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
N
News and Events Feed by Topic

博客园 - 八爻老骥

Gradle模块化项目中使用了非模块化库的编译方法 系统架构一一前端技术 系统架构一一ORM的应用 WPF下的RibbonApplicationMenu控件自定义 系统架构——多线程的应用 系统架构——前后端分离 为什么要学DirectX? D3D实战-在窗口中画一个三角形 - 八爻老骥 - 博客园 DirectX 入门1-初识DirectX Tool Kit DirectX 12 版 俄罗斯方块 WPF俄罗斯方块改进版 - 八爻老骥 - 博客园 【编程漫谈】用JAVA画多边形 【编程漫谈】单机与联机 往Angular应用程序中添加DevExtreme 【编程漫谈】程序的运行环境 创建DevExtreme应用程序 【编程漫谈】程序的运行流程 【编程漫谈】Hello world! WPF实战俄罗斯方块
【编程漫谈】程序的进入与退出
八爻老骥 · 2019-08-12 · via 博客园 - 八爻老骥

就像汽油发动机,需要进行一次人工点火才能一直自行运转下去。应用程序也是如此,应用程序的入口就是一个程序启动时最先执行到的地方,操作系统通过这个入口来启动你的应用程序。正常情况程序从开始的一行执行到最后一行,程序结束。

观察一段C语言写的程序代码:

#include <stdio.h>
#include <string.h>
void main()
{
    printf("Application Starting...\n");//程序启动提示
    while (1)//保持程序一直处于运行状态
    {
        printf("Waiting for command, you can input 'quit' to exit.\n");
        char c[10];
        scanf("%s", &c);
        if (strcmp(c, "quit") == 0)
        {
            break;
        }
        printf("Invalid command:%s\n", c);
    }
    printf("Application exited. \nBye bye!!");//程序退出提示
}

运行之后的输出结果:

PS E:\dev\my\demo> ./a
Application Starting...
Waiting for command, you can input 'quit' to exit.
ljkk
Invalid command:ljkk
Waiting for command, you can input 'quit' to exit.
exit
Invalid command:exit
Waiting for command, you can input 'quit' to exit.
quit
Application exited.
Bye bye!!
PS E:\dev\my\demo>

在这段C的代码中,我们只定义了一个函数main,然后启动程序后,就按顺序执行main里边的东西了。main就是C写的程序的入口,当你告诉操作系统说,我要运行这个a.exe的时候,操作系统先读取a.exe的所有放在磁盘上的静态内容加载到内存中,然后就在内存中找a.exe那个代表main的指针,然后将这个指针交给CPU,那这个程序就处于运行的状态了。

Java虽然不是独立运行的程序,但它跟C有着相同的机制,都要定义一个入口函数main,只不过,C是直接交给CPU来处理的,而Java的main是交给java虚拟机的。Java的代码和C的大同小异,只不过java是面向对象的,所有函数都要写在一个class里边。

public class Hello {

    public static void main(String[] args) throws Throwable {
        System.out.println("Application Starting..."); //程序启动提示
        while (true) { //保持程序一直处于运行状态
            System.out.println("Waiting for command, you can input 'quit' to exit."); //输入提示
            byte[] input = new byte[10];
            var r = System.in.read(input); //读取命令
            if (r == 0)
                continue;
            var cmd = new String(input);
            if (cmd.trim().equals("quit")) {
                break;
            }
            System.out.printf("Invalid command:%s\n", cmd); //提示错误
        }
        System.out.println("Application exited. \nBye bye!!"); //程序退出提示
    }
}

JAVA的输出

PS E:\dev\my\demo> java Hello
Application Starting...
Waiting for command, you can input 'quit' to exit.
quit
Application exited.
Bye bye!!

程序的逻辑是一样的,只是语言风格上有些不同,调用的函数略有差别。总C跟JAVA目前来讲,还没有太大的差别。

我们再一个js版的。代码如下:

WScript.StdOut.WriteLine("Application Starting...");//程序启动提示
while (1)//保持程序一直处于运行状态
{
    WScript.StdOut.WriteLine("Waiting for command, you can input 'quit' to exit.");
    var cmd = WScript.StdIn.ReadLine();

    if (cmd == "quit") {
        break;
    }
    WScript.StdOut.WriteLine("Invalid command:"+cmd);
}
WScript.StdOut.WriteLine("Application exited. \nBye bye!!");//程序退出提示

运行输出:

PS E:\dev\my\demo> cscript hello.js
Microsoft (R) Windows Script Host Version 5.812
版权所有(C) Microsoft Corporation。保留所有权利。

Application Starting...
Waiting for command, you can input 'quit' to exit.
hello
Invalid command:hello
Waiting for command, you can input 'quit' to exit.
quit
Application exited.
Bye bye!!

JS的代码和前边两个基于编译的程序的差别是,JS中不需要定义入口函数,js的文件的开头就是入口,程序文件的结尾就是程序结束。

再来个Python的,跟JS的差不多。

print("Application Starting...");#程序启动提示
while (1):#保持程序一直处于运行状态
    print("Waiting for command, you can input 'quit' to exit.");
    cmd = input();
    if (cmd == "quit") :
        break;    
    print("Invalid command:"+cmd);
print("Application exited. \nBye bye!!");#程序退出提示

输入输出:

PS E:\dev\my\demo> py hello.py
Application Starting...
Waiting for command, you can input 'quit' to exit.
hello
Invalid command:hello
Waiting for command, you can input 'quit' to exit.
quit
Application exited.
Bye bye!!

还有PHP,PERL什么的大同小异,这里不列举了。