A blog describing computer internals and electronics, from a student's viewpoint

"Paperbacks"

Monday, July 12, 2010

Multi-process software: boon or bane?

If ever you've used Google Chrome, you'll notice that if it has multiple tabs, multiple copies of the executable (chrome.exe under Windows) will show up in the operating system's process manager (Task Manager under Windows), even though there is only one window open. How does this happen, I'll write later. For now, let's focus on its strengths and weaknesses.

Before I proceed, some terminology round-up:
  • Process - a running instance of a program / executable file
  • Thread - A function running as a "mini" process within a normal process
  • Mutex - An object used by operating systems to ensure exclusive access to a resource, such as memory and CPU time
  • Context switch - The process of telling the CPU to execute/resume execution of another function
One of the good points of the multi-process concept is that a crash in one process does not affect other processes, thereby making the system more stable and resistant to software bugs that are sufficiently fatal to bring down a computer (bluescreen in Windows, kernel panic in Linux/Unix.). This is in contrast with threaded software, where a crash in one thread will cause all other threads in a process to be killed as well without an opportunity to clean up, thereby leaving dangling objects like mutexes which can choke up other processes and cause system crashes. Also, multi-process software can be designed so that each copy of itself may run as a different user with the same, greater, or less privileges, compared to threaded software where each thread is tied to the privileges of its container.

With the good comes the bad. Processes require more resources like memory to maintain, and it is costlier to do context switches between processes than between threads, since a process context switch involves saving/restoring more data than thread context switching; that's why it is easier to run out of memory with multi-process applications. This consideration especially applies with embedded-system operating systems, such as those found in the iPhone and iPad. Another disadvantage is that, implementation-wise, a multi-process executable is harder to code.

So, is this concept good or bad? The answer is that it depends on the program's purpose. For Web browsers, this is good, because, from a security standpoint, you have no control over the code in the Web pages served to you; it may be containing malware. For games, the very nature of its coding makes it impossible to spawn (branch out) its operations across multiple copies of itself, since process switches are not deterministic.

Sunday, July 11, 2010

So nice user interfaces are better...

...but not if it restricts you too much. Besides, a nice user interface (UI) for a badly-written piece of software is useless and vice-versa; although the former is worse than the latter, the latter is more pronounced in the use of normal software.

The Ribbon UI that comes with Microsoft Office 2007 and beyond has become very "popular" to people. I place the word "popular" in quotes because, in reality, it is quite misleading, and many companies who use this UI design in their programs do so without first seeing how many features their programs have, and what their programs do; as a result, it's the end-users who suffer digging through tabs just to find the features they really need.

On one hand, it does a good job of placing the most frequently-used items at the front and in "big" size, while less-used items appear in smaller icons or in their dialog boxes; on the other hand, it will be a pain for the user who is accustomed to the classic design of toolbars and menus; such a user will have to find which category a particular command falls under, and it is not a suitable UI paradigm if your program does not contain a ton-load of features, or if keyboard shortcuts are an extensive requirement, such as in programs for troubleshooting programmable-logic controllers.

It may be apparent that the UI design of the Ribbon was overly emphasized for novice users; in fact, the MS Office user base has more people very proficient at it (the old ones with menus, that is) than novice users; hence, widespread backlash ensued on the new UI of MS Office. Besides, the glow on the Ribbon and its large icons are a no-no for graphics editing and computers with a limited color palette - say, 16bpp or low screen resolution.

Anyway, in any case, whether a program's UI will live on or die off is a matter of personal opinion and preference.

Sites:

Sunday, March 14, 2010

Kernel-mode Applications under Windows XP

Recently, I have been making a dynamic-link library consisting of two main types of code: one that I actively learn through both trainings and algorithm books, and the other that are rewrites of old code I found in my data archives dating ~3 years back. For performance reasons, I decided to rewrite the code for operation as a device driver (which kernel-mode software are more commonly called by.) In the course of porting to kernel space, I came across this scenario: The well-beloved C runtime library of Windows, MSVCRT.DLL, has an almost non-existent equivalent for applications written to operate in kernel-mode. Of course, things like localization are not important for kernel-mode software, for their purpose is interfacing the operating system kernel to various hardware peripherals like CD-ROMs and USB host controllers. But C functions such as malloc(), free(), rand() and the str* series of functions, are very commonly used. The question is why Microsoft did not implement a kernel-mode equivalent of MSVCRT.DLL that contains even the most basic of C functions.

Because of this, I was forced to re-implement some C functions such as sin(), cos(), sqrt(), memcpy() and memset() . Being just a hobbyist, do not expect me to make a kernel-mode dynamic-link library without causing dreaded blue screens, in a short span of time; expect years. Some of the C functions now have a good-enough implementation, but many others are just "do nothing" stubs. I'll post a copy soon.

Upcoming hobby project for kernel-mode programming : an audio filter that is chained to a hardware driver to have dancing lights. :P

P.S. Do not ever expect me to make drivers for Vista and 7. The digital signatures required are way beyond my reach.

Thursday, January 14, 2010

Kernel-mode software: Part I

First of all, sorry for keeping this blog dormant for too long. :(

As of now, I am writing a compilation of algorithms whose implementation is designed in kernel-mode to make them faster. My goal: make a standard c/c++ library that can operate correctly under kernel-mode the same way as they do in user-mode - minus standard I/O (device drivers are designed to do I/O directly to their hardware, right? xD). And port common libraries to kernel-mode.

P.S. I'm currently doing my work under Windows XP. Will do for Linux afterwards.

As programmers know, kernel-mode software development is much, much harder than normal, user-mode programming. For one, while an application bug will just crash your application with familiar message boxes, in kernel-mode the same thing causes the entire system to issue a bugcheck (a.k.a. BSOD in Windows). For another, you'd have to do everything that the runtime would have done for you by hand; for example, the familiar C++ exception handling is absent in kernel-mode (C++ does not work properly under kernel-mode in the first place!) There are more issues than what I've written here, but these two are some of the most important, aside from security issues (kernel-mode software, from their name, are capable of fully controlling your system. So... don't, don't, DON'T INSTALL DEVICE DRIVERS FROM UNKNOWN PEOPLE!)

I think that the only advantage of normal applications ported to kernel-mode comes in terms of speed.

--- I shall add to this page. This is all for now... ---

Saturday, September 12, 2009

Visual Studio or wxDev-C++?

P.S. Sorry for the long time I've left this blog dormant: I'm only a college student, subject to the "heavyweight" requirements of my subjects.

Microsoft Visual Studio or wxDev-C++?
These are only two of the most popular compilers in the world when it comes to making Win32 programs. The commercial companies use Visual Studio because it is an optimizing compiler (case in point: Adobe Flash Player); open-source projects (most, not all) use wxDev-C++ to compile their software projects (for example, VideoLan's VLC player). But which compiler is really better?

Vendors of software modules such as ActiveX components and skinning frameworks use Microsoft's compiler because MFC is used to encapsulate the nosebleed details of COM and the like. As many know, there are already a lot of GUI skinning frameworks out there that use MFC, such as those that provide a look-and-feel of Office 2007. However, MFC is well-known in the software world for being bloatware, so many application developers don't want to use MFC.

wxDev-C++, on the other hand, has a GUI library bundled with it, wxWidgets. Intended as a replacement for MFC? Well, not quite: it is entirely different than the coding style you have to use with MFC. But rest assured it will remain far simpler to use than MFC. :)

Well, I don't develop GUI applications myself; I more often develop library back-ends for other people to use. In this respect, I use wxDev-C++, since it is more standards-compliant and flexible than VS.

Wednesday, June 10, 2009

C++ for native NT user-mode programs?

C++ is a powerful language, with features like templates, exception handling, native machine-code generation, and classes. These are some of its attractive features. But almost anyone who tried to use C++ in native NT application development may have wondered why their applications fail to start (it's obvious already you cannot start one directly from Win32, right?) even though there are no unresolved DLL references (these can be seen using a program such as Dependency Walker.)

That same program can answer why, if your program uses the dynamic CRT in a DLL (e.g. msvcr90.dll): the runtime DLL requires the graphical-user interface (GUI) subsystem, which is in the server process (Csrss.exe), which is executed usually after any other native applications are done. So, by the time your native NT application runs, the required subsystem (GUI) has not been started yet, thus explaining the reason why the DLL fails to load.

The answer isn't so obvious when the CRT is statically linked (e.g. using libcmt.lib). Looking at the 'Subsystem' column for both Ntdll.dll and Kernel32.dll, we see that they are both using the 'Console' subsystem. The Console subsystem is already loaded very early in the boot process, because Ntdll.dll sits directly above the kernel. So why it can't start? Well, even I had to wonder until people working on Wine (many thanks to them! :D) made their source available on the Net. I pilled a file from the set belonging to Kernel32.dll, and I saw a code that communicates with the server process, in its DllMain function. No wonder the Kernel32 module cannot load...

Monday, May 25, 2009

The Microsoft Visual C Runtimes and Wine

Through the years, the Wine development team has been striving to ensure full compatibility of Windows programs in Linux. It is known to them that kernel-mode software, and software that does direct hardware accesses, will not be supported ever, across different operating systems due to fundamental differences in the kernel architecture between OS'es.

But there's one aspect of many high-end Windows programs that makes cross-platform execution harder to attain. This has something to do with the versioning of the Microsoft Visual C runtime libraries, especially the MSVC++ 2005 CRT's (e.g. msvcr80.dll) and later (due to the use of the side-by-side architecture of Windows XP and later.) To complicate matters further, many high-end apps also make use of MFC, either directly or indirectly (e.g. using a skinning component.) MFC is a well-known bloater in the size of software distributions, so many companies hate products using the library, be it their own apps or some skinning component.

Examining the C runtime libraries reveals something surprising: much of the core routines ( e.g. sprintf() ) haven't changed over the years, yet each succeeding version of Visual C++ requires an entirely different set of runtimes (even MFC's no exception.) To make matters worse, msvcr80.dll and later force all applications to have a manifest or not load at all - this info can be dissected by a hacker with the proper resources, and he can tell what compiler was used in making the program. I wonder what Microsoft's up to in terms of code redundancy, given the fact that the core C library exists with all Windows versions since Win98 and hasn't changed (this file is named Msvcrt.dll in the system32 directory, typically C:\Windows\System32 .) Folks there could just release the differences in exception handling with a separate DLL, and all others will forward to the system's C library.

I'm still writing a DLL that does what I've said above: just have the differences in exception handling included, and the rest is forwarded to the system C library.

If Microsoft keeps tying software vendors into their Windows operating system, there will come one day that the big software company will terribly suffer for all of what they've done, given the disturbing fact (from leaked internal docs) that it plans to dominate the computer world (in effect, control our everyday businesses, often against our will). Windows may be the mainstream OS, but it's nothing if it's from the hands of the company which can be considered the computing world's "Adolf Hitler."

P.S. Many thanks to the folks at the Wine project; I'd try donate and help one day.

Saturday, May 23, 2009

Linux and "WinDevices"

The hardware compatibility arena is one of those places in the computer world where issues are at their hottest.

Take my own Neo Laptop as an example. It would have been easy for me to use Linux out of the box, weren't it for the SiS video chipset and the C&E OSD keyboard filter... Although I've found the solution to my SiS problem in the form of a generic 2D driver (many thanks to the posters at Ubuntu Forums! ), not all of my "Fn" keys work, especially the WiFi switch, which is one of what I need most when doing online research from my laptop while on the move, and I have not found a driver for those special "Fn" keys. And even though I'm happy with the SiS fix, how about other computer users who want to use Linux but are forced to use Windows because of hardware? $^&$@#@&*$!

Another good example is the PCI card sector. These are cards that plug into the motherboard, such as sound cards, LAN/Ethernet cards, video cards, and so on. Because of their nature, there is no single driver that fits all cards, even generic ones. A good exception, though, is video cards in VGA mode [640x480, 16 color] - the BIOS requires support for that at start-up. In virtually all other respects, things differ wildly between devices such as USB PCI cards, WiFi adapters, and on and on. This incompatibility problem gives the hardware vendors an opportunity to force the user to use the card only on the OS(es) of their choice, which in many cases is Windows Vista and later (mine's Windows XP + Ubuntu dual-boot.)

I know that computer hardware vendors are constantly striving to bring to the end-user more sophisticated, better, and more fashionable peripherals, such as faster video cards and sound cards delivering more realistic sound. But to the same vendors, really, if you cannot provide full functionality on even the "Big Three" operating systems (Windows, Linux, Mac OS), your innovations are essentially useless, for many of the same people wanting better hardware dislike Windows and/or Mac.

P.S. I may be one who would give a try on making PC peripherals, but I'd be as generic as possible - or if not possible, at least provide native drivers for the "Big Three."

Monday, May 11, 2009

Ubuntu 8.0.4 and Windows executables and devices

I recently began using Ubuntu Hardy Heron (8.0.4). Now, I was faced with making my devices and programs work as they did in Windows.

I installed Wine to make my programs work. But, as would be expected, not all programs work, and, I tell you, don't ever hope that Wine will support kernel-mode Windows software, for the kernel-mode architecture of Windows and the Linux kernel are way too different to be ever compatible in any respect. Also, one problem is emulating Ntdll.dll - this DLL, though exports many functions (1000+), what it simply does is "barrel" it down to the kernel-mode libraries, which actually do the work. I guess Wine's emulation of Ntdll.dll just maps the call to native Linux functions.

If ever, I'll simply get native Linux versions - that will be good. But for some software (especially "high-end" software like Adobe Flash) which are "Windows-only," I have to use Wine to run them. Still, I know at least they will perform reasonably good under Wine.

A good example of Windows-only software that's of practical use to me is Novarm, Ltd.'s DipTrace. Even with the limitation that it was compiled to run on Windows, still, I've been able to make it work under Ubuntu - and many other "simple" Windows binaries I have at my computer.

A harder problem I have is making my SiS graphics card work as it did in Windows XP. I know, and you may also know, that this company writes drivers solely for Windows. It's sad enough that they, a hardware vendor, are very picky on the OS they want their hardware to run in (in this case, Windows). In my opinion, device manufacturers are supposed to design their hardware such that any OS (Windows, MAC, Linux) can use its capabilities. I understand if they made their drivers for only the three operating systems I've mentioned - they're the mainstream operating systems out there in the market.

Anyway. I hope device vendors will not concentrate on Windows in writing their device drivers - it's not just they have to pay Microsoft to make the drivers "certified," many organizations in my opinion hate Windows. I know of news to obstruct the motive of free - as in libre - software. But for them, here's just one thing I have to say:

"No one can monopolize the software world. Ever."

Even I am planning to make some PC hardware, but I have to use commands that are as generic as possible, so I in turn don't get into the "selective device-OS combination" problem.

Tuesday, April 14, 2009

C++: The new and delete operators

If you have been accustomed to C, you may encounter the way heap memory is allocated:

char* c = (char*)malloc(sizeof(char) * x);

In C++, due to the existence of classes and constructors/destructors, malloc and free alone won't suffice; it won't be able to call the constructor and destructor, respectively. So, the keyword new was made to allocate the memory and call the constructor. The previous item will now look like this:

char* c = new char[x];

Notice that sizeof(char) is gone. Why? Because new will, in order to return the correct pointer type, need to know the type of data you are allocating, and the operator determines the size of an element from the type. The general syntax of the new keyword is:

(data type)* x = new (data type)[y];

where x is the pointer to store the address of the allocated memory, and y is the number of elements. When y = 1, the [y] part can be omitted:

(data type)* x = new (data type);

Notice new's two forms: the former, with the square brackets, is called vector new; the one without the brackets is called scalar new.

On the other hand, delete does the reverse: it calls the destructor before putting the memory back to the heap. The delete operator has two forms:

delete - for single-element allocations (scalar)

delete[] - for multi-element (array) allocations (vector)

To avoid being too technical, as I've done in some posts, the versions with the square brackets are for array (vector) allocations/deallocations, while the versions without the brackets are for one-element (scalar) allocations/deallocations.