The kernel is the most trusted part of the operating system. Multiple rings of protection were among the most revolutionary concepts introduced by the Multics operating system, most general-purpose systems use only two rings, even if the hardware they run on provides more CPU modes than that. For example, Windows 7 and Windows Server 2008 (and their predecessors) use only two rings, with ring 0 corresponding to kernel mode and ring 3 to user mode, because earlier versions of Windows ran on processors that supported only two protection levels.
Many modern CPU architectures (including the popular Intel x86 architecture) include some form of ring protection, although the Windows NT operating system, like Unix, does not fully utilize this feature. Under DOS, the kernel, drivers and applications typically run on ring 3 (however, this is exclusive to the case where protected-mode drivers and/or DOS extenders are used; as a real-mode OS, the system runs with effectively no protection), whereas 386 memory managers such as EMM386 run at ring 0.
Windows Memory
Each process started on x86 version of Windows uses a flat memory model that ranges from 0x00000000 – 0xFFFFFFFF. The lower half of the memory, 0x00000000 – 0x7FFFFFFF, is reserved for user space code. While the upper half of the memory, 0x80000000 – 0xFFFFFFFF, is reserved for the kernel code. The Windows operating system also doesn’t use the segmentation (well actually it does, because it has to), but the segment table contains segment descriptors that use the entire linear address space. There are four segments, two for user and two for kernel mode, which describe the data and code for each of the modes. But all of the descriptors actually contain the same linear address space. This means they all point to the same segment in memory that is 0xFFFFFFFF bits long, proving that there is no segmentation on Windows systems.
The segmentation is actually not used by the Windows system. Therefore we can use the terms “virtual address space” and “linear address space” interchangeably, because they are the same in this particular case. Because of this, when talking about user space code being loaded in the virtual address space from 0x00000000 to 0x7FFFFFFF, we’re actually talking about linear addresses. Those addresses are then sent into the paging unit to be translated into physical addresses. We’ve just determined that even though each process uses a flat memory model that spans the entire 4GB linear address space, it can only use half of it. This is because the other half is reserved for kernel code: the program can thus use, at most, 2GB of memory.
Every process has its own unique value in the CR3 register that points to the process’ page directory table. Because each process has its own page directory table that is used to translate the linear address to physical address, two processes can use the same linear address, while their physical address is different. Okay, so each program has its own ad
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Computer and multimedia Week 1 Windows Architecture.pptx
1. Computer System
and Multimedia (Week 1):
Windows Architecture and Intro to
Programming using Visual Studio
Textbook: Programming Windows / Charles Petzold. -- 5th ed.
Chapter 1. Getting Started
1
2. Pre-requisite
• Familiar With Windows
• Familiarity with the C language, particularly
with C structures and pointers.
• A computer system with Windows XP/Vista/7
installed with Visual Studio 2010.
• Open-minded and ready to enjoy writing
programs on one sophisticated operating
system.
2
3. Aspects of Windows
• Preemptive multitasking and multithreading
graphical operating systems.
• Programs themselves can split into multiple
threads of execution that seem to run
concurrently
3
4. User Interface
• Interaction between humans and machines occurs.
• Input: allowing the users to manipulate a system
• Output: allowing the system to indicate the effects of the
users' manipulation
• Type of User Interface
– Text
• command need to be typed-in from a keyboard) and user must
have knowledge on the valid command entries and their functions.
– Graphics (command selected by a mouse operation or
typed-in
• the user directly interacts with the objects on the display which
give the information of what the selection will be.
4
5. Advantage of GUI
• Users no longer expect to spend long periods of time learning how
to use the computer or mastering a new program.
• Windows helps because all applications have the same fundamental
look and feel.
• The program occupies a window—usually a rectangular area on the
screen.
• Each window is identified by a caption bar. Most program functions
are initiated through the program's menus.
• A user can view the display of information too large to fit on a
single screen by using scroll bars.
• Some menu items invoke dialog boxes, into which the user enters
additional information.
• One dialog box in particular, that used to open a file, can be found
in almost every large Windows program.
• This dialog box looks the same (or nearly the same) in all of these
Windows programs, and it is almost always invoked from the same
menu option.
5
6. Programmer's Perspective
• The consistent user interface results from
using the routines built into Windows for
constructing menus and dialog boxes.
• All menus have the same keyboard and mouse
interface because Windows—rather than the
application program—handles this job
6
7. Dynamic-link Libraries
• Programs running in Windows can share routines that
are located in other files called "dynamic-link libraries."
• Windows includes a mechanism to link the program
with the routines in the dynamic-link libraries at run
time.
• Windows itself is basically a set of dynamic-link
libraries.
• These are files with the extension .DLL or sometimes
.EXE, and they are mostly located in the
WINDOWSSYSTEM subdirectory or
WINDOWSSYSTEM32
7
8. Graphics Device Interface
• Programs written for Windows do not directly access the
hardware of graphics display devices such as the screen and
printer.
• Instead, Windows includes a graphics programming
language (called the Graphics Device Interface, or GDI) that
allows the easy display of graphics and formatted text.
• Windows virtualizes display hardware.
• A program written for Windows will run with any video
board or any printer for which a Windows device driver is
available.
• The program does not need to determine what type of
device is attached to the system.
8
9. Dynamic Linking
• Windows provides a wealth of function calls that an application, mostly to
implement its user interface and display text and graphics on the video display.
– Kernel (the 32-bit KERNEL32.DLL) handles all the stuff that an operating system kernel
traditionally handles—memory management, file I/O, and tasking.
– User (USER32.DLL) refers to the user interface, and implements all the windowing logic.
– GDI (GDI32.DLL) is the Graphics Device Interface, which allows a program to display text and
graphics on the screen and printer.
• When you run a Windows program, it interfaces to Windows through a process
called "dynamic linking."
– A Windows .EXE file contains references to the various dynamic-link libraries it uses and the
functions therein.
– When a Windows program is loaded into memory, the calls in the program are resolved to
point to the entries of the DLL functions, which are also loaded into memory if not already
there.
• When you link a Windows program (handled by Visual Studio programming
environment) to produce an executable file, special "import libraries" that contain
the dynamic-link library names and reference information for all the Windows
function calls in a table that Windows uses to resolve calls to Windows functions
when loading the program.
• When you run a Windows program, it interfaces to Windows through a process
called "dynamic linking" the calls in the program are resolved to point to the
entries of the DLL functions, which are also loaded into memory if not already
there. 9
10. Function Calls
• Windows supports several thousand function
calls that applications can use. Each function has
a descriptive name, such as CreateWindow.
• All the Windows functions that an application
may use are declared in header files (windows.h).
• Windows function calls is generally the same as C
library functions.
– Primary difference
• the machine code for C library functions is linked into your
program code
• the code for Windows functions is located outside of your
program in the DLLs.
10
11. APIs
• To a programmer, an operating system is defined by its API.
• An API encompasses all the function calls that an
application program can make of an operating system, as
well as definitions of associated data types and structures.
• In Windows, the API also implies a particular program
architecture
• The API for the 16-bit versions of Windows (Windows 1.0
through Windows 3.1) is now known as Win16. The API for
the 32-bit versions of Windows (Windows 95, Windows 98,
and all versions of Windows NT) is now known as Win32.
• We will deal with Win32 API in this course.
11
12. About Compiler
• A compiler is a program that reads code and produces a stand-alone executable
code that the CPU can understand directly.
• Once your code has been turned into an executable, you do not need the compiler
to run the program.
• Although it may intuitively seem like high-level languages would be significantly
less efficient than assembly languages, modern compiTo write a prog lers do an
excellent job of converting high-level languages into fast executables.
• Sometimes, they even do a better job than human coders can do in assembly
language!
• Here is a simplified representation of the compiling process:
2/20/2024 12
13. About Intepreter
• An interpreter is a program that reads code and essentially compiles and executes
(interprets) your program as it is run.
• One advantage of interpreters is that they are much easier to write than compilers,
because they can be written in a high-level language themselves.
• However, they tend to be less efficient when running programs because the
compiling needs to be done every time the program is run.
• Furthermore, the interpreter is needed every time the program is run.
• Here is a simplified representation of the interpretation process:
2/20/2024 13
14. Why High Level language
• Any language can be compiled or interpreted, however, traditionally
languages like C, C++, and Pascal are compiled, whereas “scripting”
languages like Perl and Javascript are interpreted. Some languages, like
Java, use a mix of the two.
• High level languages have several desirable properties. First, high level
languages are much easier to read and write.
• Here is the same instruction as above in C/C++: a = 97;
• Second, they require less instructions to perform the same task as lower
level languages. In C++ you can do something like a = b * 2 + 5; in one line.
In assembly language, this would take 5 or 6 different instructions.
• Third, you don’t have to concern yourself with details such as loading
variables into CPU registers. The compiler or interpreter takes care of all
those details for you.
• And fourth, they are portable to different architectures, with one major
exception, which we will discuss in next slide.
2/20/2024 14
15. Portability
• The exception to portability is that many platforms, such as Microsoft Windows,
contain platform-specific functions that you can use in your code.
• These can make it much easier to write a program for a specific platform, but at
the expense of portability. In these tutorials, we will explicitly point out whenever
we show you anything that is platform specific.
2/20/2024 15
17. Compiling
• In order to compile a program, we need a compiler. The job of the compiler is
twofold:
1) To check your program and make sure it follows the syntactical rules of the
C++ language:
2) To take your source code as input and produce a machine language object
file as output. Object files are typically named name.o or name.obj, where
name is the same name as the .cpp file it was produced from. If your
program had 5 .cpp files, the compiler would generate 5 object files.
2/20/2024 17
18. Linking
• Linking is the process of taking all the object files for a program and combining them into a single
executable.
• In addition to the object files for a program, the linker includes files from the
runtime support library.
• The C++ language itself is fairly small and simple.
• However, it comes with a large library of optional components that may be
utilized by your program, and these components live in the runtime support
library.
• For example, if you wanted to output something to the screen, your program
would include a special command to tell the compiler that you wanted to use
the I/O (input/output) routines from the runtime support library.
2/20/2024 18
19. One Of The Solution Available
To Build A Program
The Visual Studio Integrated Development Environment
(IDE) offers a set of tools that help you write and
modify code, and also detect and correct errors.
In these topics, you create a new standard C++ program
and test its functionality by using features available in
Visual Studio for the C++ developer.
2/20/2024 19
20. Language Options
• Using C and the native APIs is not the only way to write
programs for Windows.
• However, this approach offers you the best
performance, the most power, and the greatest
versatility in exploiting the features of Windows.
• Executables are relatively small and don't require
external libraries to run (except for the Windows DLLs
themselves, of course).
• Most importantly, becoming familiar with the API
provides you with a deeper understanding of Windows
internals, regardless of how you eventually write
applications for Windows.
20
21. Windows Programming
• Writing C or C++ program using native
Windows APIs a one of techniques of
Windows programming
21
22. Programming Tutorial
• Get a copy of Visual Studio 2010
• Refer to the following site on Installing Visual
Studio 2010
– http://msdn.microsoft.com/en-
us/library/e2h7fzkw.aspx#installing
22
23. Setting up Visual Studio for a console project(1)
• Start Visual Studio.net.
Select 'File', then 'New Project‘
• You will see the following screen.
23
Choose 'Win32 Console Application' and choose a name for the project. Then
select OK.
24. Setting up Visual Studio for a console project(2)
• You will see the following screen. Select 'next'
24
25. Setting up Visual Studio for a console project(3)
• You will see the following screen.
25
Select 'Empty project' and click 'Finish'
26. Creating the Source File (1)
• After the Visual Studio IDE opens, in the Solution Explorer window, Right
click on 'Source files' and select 'Add -> New Item'
26
27. Creating the Source File (2)
• Choose 'C++ File' and enter 'main' into the name field and click Add button.
27
28. Writing the program
• Type in the following program in the “main.cpp” edit tab area.
28
29. Running the program without debug
• Press Ctrl-F5 and you will see the following display
29
• “hello, world” is the output of the program
• “Press any key to continue . . .” is the interaction required by the Visual Studio
IDE which will wait for a response from you before closing the window so that
you can see the output of the program on the display.
30. If The is Error During Compilation
1. If there is error during compiling, an error message dialog box will
appear. Select <No>
2. The Output window displays information about the compilation
progress, for example, the location of the build log and a message
that states the build status.
3. Double click the line which has the words “error cXXXX”. The line
with where the compiler fond the error will be pointed in the .CPP
window. It is not necessary that the real error is there. You need
to have knowledge on C++ programming which you will learn in
this class and develop experiences to solve this. So get the
knowledge and experience after this.
4. Whatever you must remove all errors before an executable file
will be created.
30
31. Finding the Executable file
By Default:
• For Vista/Window 7 system the file is located at the following folder
…DocumentsVisual Studio 2008Projects<Projectname>Debug
• For XP system the file is located at the following folder
…My DocumentsVisual Studio
2008Projects<Projectname>Debug
• You can execute the program by clicking the application icon from the
relevant directory
• You can also execute the program from the Command Prompt Window
• To open a command prompt
– In Windows XP select Start|Run and enter “cmd” in the entry field.
– In Windows Vista /Window 7 select Start and enter “cmd” in the entry field.
– Change to the default directory where the executable file is located
– The you can run the program by entering the file name on the command
prompt of the Command Prompt Window
– This method is “hectic” since to change directory you hate to a long string of
characters and prones to re-entry of strings.
• The next slide shows how to open command prompt at the folder which
you find using windows explorer. 31
32. Right Click to Open Command Prompt
in Selected Folder in Windows XP:
A shortcut in the right click context menu of Windows Explorer to open a
command prompt in the current directory. When you need to run scripts in a
very long directory name, this is very handy.
So here's how you set it up:
• Open up windows explorer
• Tools -> Folder Options.
• File Types Tab
• Select the Folder file type
• Click Advanced
• Click New
• For the Action type what ever you want the context menu to display, I
used Command Prompt.
• For the Application used to perform the action use
c:windowssystem32cmd.exe (note on win2k you will want to specify
the winnt directory instead of the windows directory)
32
33. To open a Command Prompt on a
folder in Windows Vista
This very useful shortcut to the DEFAULT installation inside
Windows Vista Explorer's context menu!
• Open a Windows Explorer windows, browse to the required
folder.
• Right-click that folder in the right pane of the Windows
Explorer window. Note that you do NOT have the "Open
Command Prompt Here" option.
• Now, hold the SHIFT key while you right-click the folder.
Behold!
• Note: As in most cases, Microsoft has only gone part of the
way with this cool feature. It's silly but this context menu
add-on in only available when you right-click on the folder
in the right pane, and not in the left pane or in a My
Computer window...
33
34. To Customize Command Prompt in
Windows XP/2000/2003/Vista
To configure the command prompt in Windows XP, Windows 2000 and Windows Server 2003:
• Open Command Prompt.
• Click the upper-left corner of the Command Prompt window, and then click Properties.
• Click the Options tab.
• In Command History, type or select 999 in Buffer Size, and then type or select 5 in Number of Buffers.
• In Edit Options, select the Quick Edit Mode and Insert Mode check boxes.
• Click the Layout tab.
• In Screen Buffer Size, type or select 9999 in Height.
• Do any of the following optional tasks:
– In Screen Buffer Size, increase Width.
– In Window Size, increase Height.
– In Window Size, increase Width.
– Clear the Let system position window check box, and then, in Window Position, change the values in Left and Top.
• In the Apply Properties dialog box, click Save properties for future windows with same title.
• Note:
• To open Command Prompt Properties from the keyboard, press ALT+SPACEBAR+P.
• By selecting the Quick Edit Mode check box, you enable copy and paste from the Command Prompt
window. To copy, select the text in the Command Prompt window with your left mouse button, and then
right-click. To paste, either at the command prompt or in a text file, right-click.
• By increasing the screen buffer size to 999, you enable scrolling through the Command Prompt window.
• By increasing the number of buffers to five, you increase the number of lines in the Command Prompt
window to 5000. 34
35. Conventional Way Running The
Program
By double-clicking the program executable icon.
• In windows Explorer, open the window where the
ex1-1.exe is located.
• Run the Program and the exit.
From the Command Prompt.
• Open a command prompt window from the
where the ex1-1.exe is located.
• Execute the program by entering “ex1-1” at the
DOS prompt.
2/20/2024 35
36. Debugging
1. Single-stepping
a. Press F10 to Step Over a process (eg. a function or a Control structure like a
while loop).
b. Press F11 to Step Into a process (eg. a function or a Control structure). This
method allow you to debug a function or a control structure.
c. If you Press F10 of F11 before a Project is being build, IDE will compile and
create the executable file if there is no error.
d. IDE will stop the flow of program at the entry into the program.
e. Continue debugging by ether pressing F10 or F11 depending on situation.
f. You may not need to debug Step Into a process if you know that the process
in bug-free.
2. Using breakpoints
a. Setting Breakpoints will let you stop your program at any location in the
program
b. You can also just select the Start Debugging button or press F5 and if there is
no error, the IDE will execute the program and stop if the flow of the
program finds a breakpoint or an exit process.
c. If you press F5 again, IDE will continue the flow of the program until a
breakpoint or an exit process.
d. If you need to single step, use the appropriate single-step key.
36
37. Watching Variables
• There are a number of important windows to monitor objects as the code
executes, these are located in the bottom left by default but if not visible they can
be opened from Debug>Windows.
• The Watch window allow monitoring of any object which can simply be
highlighted in the code window and dragged to the Watch window.
– The Watch window monitor objects regardless of whether they are in scope or now.
• The Locals window cannot have objects dragged into it and shows all objects that
are currently in scope.
– A powerful feature of the locals window is that it allow the objects to by modified. In this
example, the value of the vsTutor variable can be modified by clicking and changing the
number in the Value column.
• The Autos window shows the objects used in the execution of the current
statement.
• The Immediate window allows for statements to be written and executed as the
program’s execution is paused.
• The Breakpoints window allows the breakpoints for the project to be managed by
enabling, disabling or deleting them.
• At all times the yellow arrow shows the line of code that will be executed next.
This arrow can be dragged down to execute subsequent lines of code without
executing the code that it is dragged over.
• When in debug mode the Debug toolbar will either appear in the main tool bar of
Visual Studio or a floating toolbar as show below.
37
39. Review
• Write the program REVIEW.CPP and with the
help of debugging technique available in
Visual Studio, answer the Questions in the
comment field, identified by the prefix Q.
• For Lecturer run this Week 1 Visual Studio
Project
39
Editor's Notes
Pre-emptive multitasking involves the use of an interrupt mechanism which suspends the currently executing process and invokes a scheduler (time-shared scheduling, or time-sharing)to determine which process should execute next. Therefore all processes will get some amount of CPU time at any given time.