Programming Windows (一)_ Windows 的运行机制


Windows 是多任务的操作系统,

 

multitasking 可分为:

 

合作型多任务( cooperative multitasking

允许执行多个任务,但分享 CPU 是程序(而非操作系统)的责任。如果有一个程序决定咬住 CPU 不放,其它程序就停摆了。

 

抢先式多任务( preemptive multitasking

操作系统能够强迫应用程序把 CPU 分享给其他人,程序员不需要什么额外的努力。虽然这个减少了程序员的工作,也不会因为某个程序拒绝分享 CPU 而造成其他程序 hang 住,但是它使得各个线程之间的执行顺序不可预测,从而导致了 race condition 等一系列问题。

 

Earlier versions of Windows used a system of multitasking called "nonpreemptive." This meant that windows did not use the system timer to slice processing time between the various programs running under the system. The programs themselves had to voluntarily give up control so that other programs could run. Under windows NT and windows 98, multitasking is preemptive and programs themselves can split into multiple threads of execution that seem to run concurrently.

 

 

要了解 Windows 就必须了解 DLL(dynamic-link libraries) ,运行在 Windows 下的程序能够共享 DLL 中的 routines. 之所以称之为 DLL ,是因为 Windows 能在程序运行时, Link 程序和 DLL 中的 routines. Windows 本身就是一个 DLL 集合体

 

Windows DLL 被放在 Windows/System 目录下( windows 98 )或 WINNT/System WINNT/System32 目录下,这些 DLL 包含了我们应用程序中需要调用的各种 API

 

早期版本的 Windows 只包含三个 DLL ,这三个 DLL 组成了 Windows 最重要的子系统

Kernel (which is currently implemented by the 16-bit KRNL386.EXE and the 32-bit KERNEL32.DLL)

handles all the stuff that an operating system kernel traditionally handles—Memory management, file I/O, and tasking.

User (implemented in the 16-bit USER.EXE and the 32-bit USER32.DLL)

Refers to the user interface, and implements all the windowing logic.

GDI (implemented in the 16-bit USER.EXE and the 32-bit USER32.DLL)

Is the Graphics Device Interface, which allows a program to display text and graphics on the screen and printer

 

Windows 是一个图形界面, windows 能将图形或格式化的文字显示在屏幕上或打印字上, Windows 并不直接操作屏幕或打印机设备,在 Windows 与硬件实体之间有一个中间层 GDI(Graphics Device Interface), Windows 通过调用 GDI 虚拟化显示设备。程序员不用关心实际上连接在 PC 上的是何种显示或打印设备,可以通过同一接口操作不同的显卡或打印设备。在 DOS 时代,第三方硬件制造商提供的显示或打印设备经常会附带一些磁盘小文件用于驱动几种特殊的打印设备。 Windows 则不需要, because the support is part of windows

 

 

从程序员的角度看 Windows 程序的编译和链接

Windows 中调用 DLL 中的函数大致与调用 C library 中函数差不多,最大的区别是 C library 的机器会被静态链接到可执行文件中,而 Windows 包含的函数被放置在可执行程序之外, DLL 之中,等待程序运行时动态链接

当链接一个 Windows 程序,生成一个可执行文件时,程序员必须把程序环境所提供的 "import libraries" 链接进来,这些 import libraries 包含了 DLL 的名字及所有被调函数的参考信息, linker 会通过这些信息构建一张表放置在 .EXE 文件中,当程序加载时,就可使用这张表解析 DLL 中的 Windows 函数调用

 

 

从用户角度看 Windows 程序的运行

当运行一个 Windows 程序时,程序会通过 "dynamic linking" 的方式去与 Windows 系统进行交涉,一个 .EXE 文件包含了所有它要调用的 DLL DLL 中的函数的参考信息。当程序被加载到内存时,程序中的函数调用被解析成指向 DLL 函数的入口,并将这些入口信息加载到内存中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值