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

来源:互联网 发布:手机投影仪软件 编辑:程序博客网 时间:2024/05/02 02:25


Windows 是多任务的操作系统,

 

multitasking 可分为:

 

合作型多任务( cooperativemultitasking

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

 

抢先式多任务( preemptivemultitasking

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

 

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

 

 

要了解Windows就必须了解DLL(dynamic-linklibraries),运行在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-bitKERNEL32.DLL)

handles all the stuff that an operating system kernel traditionallyhandles—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 windowinglogic.

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

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

 

Windows 是一个图形界面,windows能将图形或格式化的文字显示在屏幕上或打印字上,Windows并不直接操作屏幕或打印机设备,在Windows与硬件实体之间有一个中间层GDI(Graphics Device Interface), Windows 通过调用GDI虚拟化显示设备。程序员不用关心实际上连接在PC上的是何种显示或打印设备,可以通过同一接口操作不同的显卡或打印设备。在DOS时代,第三方硬件制造商提供的显示或打印设备经常会附带一些磁盘小文件用于驱动几种特殊的打印设备。Windows则不需要,becausethe 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文件包含了所有它要调用的DLLDLL中的函数的参考信息。当程序被加载到内存时,程序中的函数调用被解析成指向DLL函数的入口,并将这些入口信息加载到内存中

原创粉丝点击