/*----------------------------------------------------- Script for AdSense -----------------------------------------------------*/ /* */ /* Footer ----------------------------------------------- */ #footer { clear: both; text-align: center; color: #333333; } #footer .widget { margin:.5em; padding-top: 20px; font-size: 85%; line-height: 1.5em; text-align: left; } /** Page structure tweaks for layout editor wireframe */ body#layout #header { width: 750px; } -->

Wednesday, April 7, 2010

Process

Long back after graduation, when I started learning programming, I learnt OS concepts. Here just sharing few important points from my diary about process.

Introduction
A process is usually defined as an instance of a running program and consists of two components:
1) A kernel object that the operating system uses to manage the process. The kernel object is also where the system keeps statistical information about the process.
2) An address space that contains all the executable or DLL module's code and data. It also contains dynamic memory allocations such as thread stacks and heap allocations.

How Process and Thread work for any Application
For a process to accomplish anything, it must have a thread that runs in its context; this thread is responsible for executing the code contained in the process's address space. In fact, a single process might contain several threads, all of them executing code "simultaneously" in the process's address space. To do this, each thread has its own set of CPU registers and its own stack.

Each process has at least one thread that executes code in the process's address space. If there were no threads executing code in the process's address space, there would be no reason for the process to continue to exist, and the system would automatically destroy the process and its address space.
For all of these threads to run, the operating system schedules some CPU time for each thread.

How OS load any application or run GUI or CUI application?
Notepad, Calculator , Wordpad are GUI-based application.
CUI-based applications are contained within a window on the screen, the window contains only text. The command shell - cmd.exe is a CUI based application.

When there is any application project has been created, that time the integrated environment sets up various linker switches so that the linker embeds the proper type of subsystem in the resulting executable. This linker switch is /SUBSYSTEM:CONSOLE for CUI application and
/SUBSYSTEM:WINDOWS for GUI application.
When user runs an application, the OS header looks inside the executable image's header and grabs this subsystem value.
If the value indicates a CUI-based application, the loader automatically ensures that a text console window is created for the application. If the value indicates a GUI-based application, the loader doesn't create the console window and just loads the application. Once the application starts running, the operating system doesn't care what type of UI your application has.

How Any Window Application Starts running?
Windows application must have an entry-point function that is called when the application starts running. There are four possible entry-point functions:
WinMain, wWinMain, main, wmain.
The operating system doesn't actually call the entry-point function you write. Instead, it calls a C/C++ run-time startup function. This function initializes the C/C++ run-time library so that you can call functions such as malloc and free. It also ensures that any global and static C++ objects that you have declared are constructed properly before your code executes.
The linker is responsible for choosing the proper C/C++ run-time startup function when it links your executable. If the /SUBSYSTEM:WINDOWS linker switch is specified, the linker expects to find either a WinMain or wWinMain function.it chooses either the WinMainCRTStartup or wWinMainCRTStartup function (Startup fn embedded in your executable), respectively.
Likewise, if the /SUBSYSTEM:CONSOLE linker switch is specified, the linker expects to find either a main or wmain function and chooses either the mainCRTStartup or wmainCRTStartup function (Startup fn embedded in your executable), respectively.

Project settings

New Windows/Visual C++ developers commonly make is to accidentally select the wrong project type when they create a new project. For example, a developer might create a new Win32 Application project but create an entry-point function of main. When building the application, the developer will get a linker error because a Win32 Application project sets the /SUBSYSTEM:WINDOWS linker switch but no WinMain or wWinMain function exists.
to solve this Click on the Link tab of the Project Settings dialog box and change the /SUBSYSTEM:WINDOWS switch to /SUBSYSTEM :CONSOLE. This is an easy way to fix the problem;
C/C++ run-time startup functions do basically the same thing. for that code for the four startup functions in the CRt0.c file.

Process Instance:
Every executable or DLL file loaded into a process's address space is assigned a unique instance handle. Your executable file's instance is passed as (w)WinMain's first parameter, hinstExe. The handle's value is typically needed for calls that load resources. For example, to load an icon resource from the executable file's image, you need to call this function:
HICON LoadIcon(
HINSTANCE hinst,
PCTSTR pszIcon);

Similarly we can always get and set, Process Instance Handle, Process's Previous Instance Handle, Process's Command Line, Process Environment variables, Process's Error Mode, Process's current drive and directory, Process's current directories, System Version etc. by using set of function calls


Source: Programming Application for Microsoft Window (by Jeffrey Richter), OS basics book (Engg. Syllabus)

No comments: