Chp 16: C Under Windows Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Microsoft categorizes the diff versions under 2 major heads. they?

A

Consumer Window:
For home, small offices.
Like: Windoes 95,98,ME.

Windows NT family:
For Business users.
Like: Windows NT, 2000, XP, Server 2003

Book refers to Windows NT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Integers under 16 bit environment and 32 bit environment:

A

In 16 bit: Int is 2 bytes

32 bit envio : Int is 4 bytes.
range -2 147 483 648 to +2 147 483 647

If we want to store age, which doesn’t need 4 bytes at all then short int [2 bytes] used. Here Int and long int is the same.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

typedefs USE:

A
  1. A windows program is required to perform plenty of complex tasks, perform I/O, send mails, play sound files…etc. Using normal int data type to represent variables that hold different entities we would soon lose track of what that integer value actually represents hence typedefine the integer as COLORREF, HANDLE, BOOL,..etc
  2. Typedefining long structure names.

Microsoft has done several typedefs for commonly used entities, all are stored in header files. These header files are provided as a part of 32-bit compiler like Visual C++.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Pointers in 32 bit World:

comment on how many tasks are allowed and how are allowed tasks managed?

A

In 16 bit envio only one task could be performed at a time so single-tasking envio, so all the resources of machine, like memory and hardware devices were accessible to this program.

In 32 bit envio several program reside and work in memory at same time, multi tasking environment.
To avoid the conflict of 2 programs, windows does not permit any application direct access to any machine resource. study in “Memory Management and Device Access”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Memory Management:

Users have become more demanding…

and windows run several applications simultaneously…

How does 16-bit envio do and how does 32 bit envio do?

A

16-bit envio allows max 1 MB which is too small for above. Hence windows evolved.
Windows run on 32-bit microprocessors so each CPU register is 32 bit long.
When we store a value at memory location the address of this location has to be stored in the register at some point of time. Thus 32 bit address can be stored in these registers and it leaves us with 2 raise 32 unique address choices. Which enables us to allow 4 GB of memory, and pointers who store these address become 4 byte entity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If we decide to install 4 gb, it would cost a lot. So how does windows manage?

A

Windows uses a memory model which makes use of as much of physical memory(128 MB ish..) as has been installed and simulates the balanced amount of memory on the hard disk when the need to do so arises. This memory management is demand-based.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what do you think of the following sentence:

“ Programs are executed straight away from hard disk”

A

Programs are first brought into physical memory before they can get executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Page Out operation and Page In Operation:

A

Suppose there are multiple programs in memory and new one starts executing, if this needs more memory than available then some of existing programs(or their parts) are transferred to the disk in order to free the physical memory to accommodate new program.
This operation is called page out.
When the part of pragram which was paged out is needed is brought back to memory its called page in. These both keep on happening.
Page as in a block of memory ( 4096 bytes).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

some rules sorta about page operations:

A
  1. Part of the program that is being executed can be paged out to disk.
  2. When it would be paged in again on need, its not necessary that it would page in at the same physical location.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Now, how do these paging operations affect our programming:

A

Pointer hold virtual address of location.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a virtual address?

Whats needed to get to an actual physical address.

A

this has 3 parts, these parts in conjunction with a CPU register (CR3) and contents of 2 tables Page directory and page table leads to an actual physical address.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how can we get to an actual physical address?

A

CR3 holds physical location of page directory table.

Left part of 32 bit virtual address holds index to page directory table, the value at index is starting address of page table.

Middle part is index into the page table, value at this index is starting address of the physical page in memory.

The right part holds byte offset(from start of the page) of the physical memory location to be accessed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can the actual address be reached by an application?

A

CR3 register is not accessible from an application, So no.

Also, as paging activity is going on the OS would suitably keep updating the values in the two tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Device Access:

A

All devices are shared among all running programs, so no program can access it directly.
Access is routed through device driver program, which finally accesses the device. more in chp 17 bout the std way.
These device drivers are to make sure no conflicts occur as many programs will want to access the device.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

DOS programming Model:

A

16 bit program envio like DOS use sequential programming model. The path along which program flows from start to finish may vary during each execution depending on the inputs received or conditions under which it is run. However path remains predictable. Assume inputs to walk through program start to end.

C programs written in this model begin execution with main().

In this model the program and not the OS that decides which function will get called and when.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Under DOS model when does program ask help from OS?

A

TO carry out jobs like console I/o, file I/o, printing..etc.

17
Q

Under Dos Model
Who performs foll operations:
Generating Graphics
Carrying out serial Communication..etc

A

The program calls another set of functions called ROM BIOS functions.

18
Q

How to call DOS and BIOS functions:

A

Unfortunately these functions do not have any names, so a mechanism called interrupts is used to call them.
Ugly: remember interrupt numbers for calling diff funcs.
And communication to these function has to be done using CPU registers, this leads to difficulties as different funcs use different registers for communication.

Providing library functions reduce these difficulties to an extent. But lib doesn’t have parallel function for every DOS/BIOS function. DOS func either call BIOS func or directly access the hardware.

Sometimes the programs are needed to directly interact with the hardware. cuz either there are no DOS/BIOS func to do so, or if their reach is limited.

19
Q

Limitations Of the DOS Model:

A
  1. No true Reuse: called lib funcs become part of .exe, same functions get replicated in several EXE files, wasting disk space.
  2. Inconsistent Look and feel: DOS/Bios doesn’t provide any functions for creating user interface elements like menus. So every DOS program is diff and user takes time to get comfy in using it.
  3. Messy Calling Mechanism:
    There is always a chance of error while using interrupt number and registers.
  4. Hardware Dependency :
    DOS programs are required to bother bout the details of the hardware, as every new hardware has it’s own interrupt number and new register details. So lot of writing to detect the hardware, this leads to large program and the programmer has to learn technical details of hardware.
20
Q

Windows Programming Model:

what new changes are observed?

A
  1. Change from text interface to graphical user interface.
  2. Windows OS can simultaneously execute several programs.
  3. Eliminate Messy calling.
  4. Permit true reuse.
  5. Provide a consistent look and feel.
  6. Eliminate hardware dependency.
21
Q

Windows Programming Model Calling Mechanism:

A

Instead of using Interrupt Number and registers Windows provides functions within itself which can be called using their names. These functions are called API ( Application Program Interface) functions. They help an application to perform various tasks such as creating a window, drawing a line, performing file Input/Output,..etc

22
Q

Windows Programming Model:

True reuse?

A

API files are stored in special files with extension .DLL.
DLL : Dynamic Link Libraries, its a binary file that provides a library of functions. These can be linked during execution. And can also be shared between several applications running in Windows. Since linking is done dynamically so they don’t become a part of .exe and hence size stays in hand.

23
Q

Why would you want to create your own DLL?

A
  1. Sharing a common code between different executable files.

2. Breaking an application into component parts to provide a way easily to upgrade applications components.

24
Q

3 DLLs that windows API come in?

A
  1. USER32.DLL : Contains function responsible for windows management like menu, cursor, communication,timer..etc
  2. GDI32.DLL : Graphics. Drawing and painting.
  3. KERNAL32.DLL : handle memory, management, threading, etc.
25
Q

Windows Programming Model:

Consistent Look and feel:

A

Every program has similar user interface, user doesn’t spend long time mastering a new program.
Every program occupies a rectangular area, title bar, info too large to fit on screen can be accessed using a scroll bar.
Most program functions are initiated through the program’s menus.

In almost every program there is a same dialogue box to open a file, invoked from same menu option.

26
Q

How is this consistent look possible?

A

Cuz windows rather than program handles it.

It results from using the windows API functions for constructing menus and dialog boxes.

27
Q

Windows Programming Model:

Hardware independent programming:

A

Windows program can call Windows API functions. Thus an application can communicate with
The new thing in windows is that OS can also communicate with an application.

When an event occurs, device driver informs windows, windows notifies application [ this notification is called message]. On receiving the messages the app communicates back with the OS by calling a windows API to perform the func and API will communicate with device driver requires and perform function.

Now replacing the keyboard/mouse would not affect the application. As there’s no direct communication between app and hardware.
Device driver may require changes but never in the application.

28
Q

Event?

A

The key press, mouse click are examples of event.

29
Q

Event Driven Model:

A

The order of calling the functions in the program is dictated by the order of occurrence of events in ‘Event Driven Programming Model’.

We can only anticipate what users are likely to do with app’s user interface, we can not expect what user can really say.

30
Q

Windows Programming a closer look:

How does the application process many messages ?

A

The messages are put in a queue. The messages in this queue are processed in First In First Out (FIFO) order.

31
Q

More on the queue’s maintained by OS:

A

System Message Queue: Common for all apps.
device driver sends a message to this on the occurrence of an event. After which OS finds out regarding which app and sends a message into
Application Message Queue (There is one queue per app.)
Recall the image in text. page 563.

32
Q

The first Windows Program:

A
#include 
int \_\_stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdline, int nCmdShow)
{
MessageBox(0, "Hello!", "Title",0);
return (0);
}
33
Q

Explain the Program:

A

C under windows program begins execution with WinMain().
__stdcall before WinMain indicates standard calling convention. __cdecl is another. Both pass arguments right to left. Std call stack cleaned by called function, cdecl stack cleaned by calling func.
cdecl is assumed.

34
Q

WinMain() function explanation:

A

It receives 4 params.
HINSTANCE(unsigned int) and LPSTR (pointer to a char) are typedefs.
Parameters:
hInstance : This is the ‘Instance Handle’ for the running application. Windows creates this ID number when the application starts. Used in windows func to identify data. handle is 32-bit number.
There is a unique handle for each entity and we can refer and reach them using this handle.
entity : app, window, icon, brush, cursor, bitmap, file, device.

hPrevInstance: This always contain 0, remnant of earlier version, used only for backward compatibility.

lpszCmdLine : Similar to argc, argv. Pointer to char string containing cmd line args passed to program.

nCmdShow : int passed to func,tells whether window created should appear minimized, as an icon, normal, or maximized when displayed first time.

35
Q

MessageBox() :

A

pops message box with title as Title and message “Hello!”.
MessageBos( 0, lpszCmdline, “Title”,0);
to print comndline args.they can be supplied from Start–>Run.
C:\ appname.exe abc def qwe
lpszCmdline points to “ abc def awe”

36
Q

GetCommandLine() :

A

To retrieve the entire command line including filename.

37
Q

Hungarian Notation:

A

Its a variable naming convention.
So called in the honor of Charles Simonyi.
It says:
Variable name begins with lower case letter or letters that denote the data type of the variable.
sz : string terminated by xero.
h : handle
n : int
lpsz : long pointer to a zero terminated string.

38
Q

Usage of Hungarian Notation:

A

It’s discouraged. Code when ported from 16 bit envio to 32 or 32 to 64 many changes req.
16 bit will have 2 byte and 4 byte int as diff datatypes
whereas in 32 there will be int of 4 byte.