Organisation chart

Leave a Comment

Learning these days…

Hmm… After 2 setbacks recently..i decide to Kick start my learning again. As part of this i started learning How to design Software. I really felt bad when i could not design an Traffic Signal software. I also wanted to Give Microsoft Certification whose fate is hanging in air. Ending no where i decided to Scrap every thing and start from the begining. I should start with Apti by RS agarwal follwed by C , C++ course work. I also need to learn Win32 and COM. Let me do one step at a time.

Leave a Comment

Process Virtual Address space

A Process’s Virtual Address Space

Every process is given its very own virtual address space. For 32-bit processes, this address space is 4 GB, since a 32-bit pointer can have any value from 0×00000000 through 0xFFFFFFFF. This allows a pointer to have one of 4,294,967,296 values, which covers a process’s 4-GB range. For 64-bit processes, this address space is 16 EB (exabytes), since a 64-bit pointer can have any value from 0×00000000′00000000 through 0xFFFFFFFF’FFFFFFFF. This allows a pointer to have one of 18,446,744,073,709,551,616 values, which covers a process’s 16-EB range. This is quite a range!

Since every process receives its very own private address space, when a thread in a process is running, that thread can access memory that belongs only to its process. The memory that belongs to all other processes is hidden and inaccessible to the running thread.

In Windows 2000, the memory belonging to the operating system itself is also hidden from the running thread, which means that the thread cannot accidentally access the operating system’s data. In Windows 98, the memory belonging to the operating system is not hidden from the running thread. Therefore, the running thread could accidentally access the operating system’s data and corrupt the operating system (potentially causing it to crash). It is not possible in Windows 98 for one process’s thread to access memory belonging to another process.

As I said, every process has its own private address space. Process A can have a data structure stored in its address space at address 0×12345678, while Process B can have a totally different data structure stored in its address space—at address 0×12345678. When threads running in Process A access memory at address 0×12345678, these threads are accessing Process A’s data structure. When threads running in Process B access memory at address 0×12345678, these threads are accessing Process B’s data structure. Threads running in Process A cannot access the data structure in Process B’s address space, and vice versa.

Leave a Comment

Gmat 750 (Q50 V41 AWA6)

Hi all,
This is iday here. I’ve been a member of the forum for more than 6 months now. I gave CAT 2005 (scored 94) and XAT 2006 (scored 95). No calls, obviously, from any school. I decided to aim higher and booked my GMAT.

Started preps in Late March. Will do some 2 or 3 hrs of prep everyday. Used the weekends to maximum possible limit – would work for 8 or 10 hrs definitely.

The materials i used were the following:

  1. Kaplan Premier edition
  2. Kaplan 800
  3. Princeton
  4. OG 11th edition
  5. OG verbal review
  6. OG Math review

After more than 2 months of sincere preps, gave my GMAT today and scored a 750. I have made a detailed post abt the experience today in my blog – for those curious people who might be interested.

I did not follow any of the To-Dos mentioned in any of the books i have mentioned above, but made my own plans on attacking various question types. I would only say that we should settle with whatever we are comfortable with.

I am no big expert in GMAT preps, there are ppl who score better with lesser preps. But i’d be glad to help any of the souls out there, who are facing difficulty in GMAT preps, or have doubts. I know how difficult these times are and what difference a few good words can do 

Planning to apply for Fall 2007 with some big names under consideration. Will keep the forum posted about my progress for sure.

Iday

Leave a Comment

GMAT 770! – Now we are talking :)

Hi Puys

thanks for the wishes.

am in chandigarh right now taking some days off from office. shall be in delhi from monday onwards so will give a detailed reply to everybody later on.

to summarise:

I have already appeared for CAT twice, scored 98.66 and 98.50 both the times. needless to say it wasn’t a gratifying effort either time.
basically to say that i had some preparation for such an exam.

started off earnestly in mid-march.
took all my material except for Kaplan 800 from the net and friends
Two CDs for Kaplan (making a total of 6 tests and 2 diagnostics)
OG – English: last october when i wanted to prepare but had to leave it cause of office pressure.
OG – Maths: still haven’t finished it. think that it is not worth the time. really not boasting but to say that i thot i was above the OG
1000 SC: again did about 400 odd of the questions. But seriously guys, that is one exercise you should be serious about. no jokes attached. one helluva thing for practicing SC
Kaplan 800: The GOD of ALL. it has some serious questions but attempt the book only if you are fairly confident about general things. otherwise it might break your confidence in the short term. The book can be had in Delhi from Old Delhi (nai sadak i think) bought it for 650/-.
Loads of questions from Net:I dont know how many questions I attempted out of the stuff from the net. tons i wud say.

Summary: My prep was more tuned towards english than maths. SC was
the bete noire. more comments later on.

Results of Mock Tests that I took:

Kaplan 1/1 : 630
Kaplan 2/1 : 640
Kaplan 1/2 : 630
Kaplan 2/2 : 630
Kaplan Diagnostic : 700
Kaplan 3/2 : 640
Kaplan 4/2 : 630

I dont remember exactly but about four of them were in March (didn’t touch Kaplan or their reviews after the last test – kaplan can be confidence sapping!!)
Cambridge Test : 720-760 (it gave me a range! what can i say)
ETS 1/2 : 760 (2 weeks before the exam)
ETS 2/2 : 770 (6 days before the exam)
PowerPrep 1/2 : 780 (3 days before the exam)
PowerPrep 2/2 : zilch. didn’t have the time for this one. let it go.
Had Princeton Tests too but didn’t go thru them : lack of time and lack of erputtation for princeton to be a decent evaluator

Other than these simulation tests, i did give the paper tests from ETS (retired and found from their site) i gave about 6 of these tests on weekends january thru march : averaged 740 in those. mostly on account of english mistakes. I wud say these tests prompted me to think more seriously about English. Hence the recommendation from my side to go thru them.
Other than this I did give one more test from Scoretop (Set 17 i think) which is supposed to be a collection of recent JJ. again gud for people to think about and note the subtle change that has come about in GMAT since Pearson took charge. (more on this later)

Oh and i did eat a couple of 5 Stars in the 2 breaks that I got in the exam. ;-)

more later

Cheers

Kunal

Leave a Comment

how to prepare GMAT 30-10-2009

Hi guys, finished the GMAT yesterday and scored 750 (Q50, V42). I dont have plans of applying immediately, so there’s nothing more to do for now. This forum has been a lot of help to me, not least by providing a set of peers, so thanks PG!

I will post a more detailed description of my prep but in the meantime, here is a list of the books that I used:
OG, OG Verbal, OG Quant
Kaplan, Kaplan 800
Princeton
Manhattan SC
IMS CAT study material for Quant.

I studied for about 2 months. My score on the 1st GMAT Prep practice test was 720 (without any studying) and that on the 2nd was 760 (one week before my GMAT).

Please let me know any questions that you may have.

Leave a Comment

Disadavantage of sleep call

When you want something to execute every second, and your action takes 300 ms, you face the problem that with a Sleep(1000) after the action, the complete process takes more then 1300 milliseconds.

A further problem was the accuracy of a Sleep. This is not high. A Sleep of 300ms can take 310ms. a Sleep of 1ms takes at least 8ms. I did a check on different machines; it is always different and always bad.

To avoid this inaccuracy of Sleep, I only used Sleep() to approach the end time up to 15 milliseconds. In the last part, I used a dirty for() loop. Because this loop generates a big CPU usage, I only used this at the very end of the waiting time. Because of the small period, you cannot see any difference in the NT-performance meter.

When you don’t need to use this level of accuracy, only the remain sleep function, InitTimer(false), will avoid this for() loop, to the disadvantage of the accuracy.


And How Does It Work Under Water?

The best way for a good timer to operate is to use the QueryPerformanceCounter(). It gives clockpulses. Because this is hardware-dependent, we need to know the frequency (how many pulses per second). This is achieved by using QueryPerformanceFrequency(). QueryPerformanceCounter() works with a struct LARGE_INTEGER, which has some disadvantages when calculating. It’s no problem to use a LONGLONG (__int64) and cast it. This saves us from a lot of casting and time-consuming member accesses.

Because my app runs for long times, I looked to the maximum time it could run: We store in a __int64; its maximum val is 9.22337E+18 (=2^63). This is the maximum period we can sleep.

Leave a Comment

Message of the day

To be free from worry means to have the power to change negative into positive

Expression:

The ones who are free from worry change that which is bad into something good, because the state of mind is calm. The one with a
calm state of mind is able to think creatively and see very clearly even beyond the situation. So there is the ability to transform the seemingly negative situation into something very positive. So there is clear decision-making and quick action. There is also no

time wasted.

Experience:

When I am free from worry, I constantly remain satisfied for having seen the positive and finding the solutions immediately instead of
looking at the problem and worrying over it. This internal silence gives the feeling of power, which naturally enables transformation
in a second and only the goodness is absorbed.

Leave a Comment

Thread Synchronization for Beginners

Introduction

In my previous article, we discussed simple multithreaded programming in C, Win32 and MFC. Now, we see simple thread synchronization with Win32 API and MFC.

What is Thread Synchronization?

In a multithreaded environment, each thread has its own local thread stack and registers. If multiple threads access the same resource for read and write, the value may not be the correct value. For example, let’s say our application contains two threads, one thread for reading content from the file and another thread writing the content to the file. If the write thread tries to write and the read thread tries to read the same data, the data might become corrupted. In this situation, we want to lock the file access. The thread synchronization has two stages. Signaled and non-signaled.

The signaled state allows objects to access and modify data. The non-signaled state does allow accessing or modifying the data in the thread local stack.

Thread Synchronization methods:

Many of the thread synchronization methods are used to synchronize multiple threads. The following methods are used to synchronize between objects.

Thread Synchronization on different processes:

Event:

Event is a thread synchronization object used to set the signaled or non-signaled state. The signaled state may be manual or automatic depending on the event declaration.

Mutex:

Mutex is the thread synchronization object which allows to access the resource only one thread at a time. Only when a process goes to the signaled state are the other resources allowed to access.

Semaphore:

Semaphore is a thread synchronization object that allows zero to any number of threads access simultaneously.

Thread Synchronization in same process:

Critical Section

The critical section is a thread synchronization object. The other synchronization objects like semaphore, event, and mutex are used to synchronize the resource with different processes. But, the critical section allows synchronization within the same process.

The given difference is the main difference between the thread synchronization objects. The other differences between the thread synchronization are the following:

Win32 Wait Functions:

The Wait family of functions are used to wait the thread synchronization object while a process completes. The widely used functions areWaitForSingleObject and WaitForMultipleObjects functions. The WaitForSingleObject function is used for waiting on a single Thread synchronization object. This is signaled when the object is set to signal or the time out interval is finished. If the time interval is INFINITE, it waits infinitely.

The WaitForMultipleObjects is used to wait for multiple objects signaled. In the Semaphore thread synchronization object, when the counters go to zero the object is non-signaled. The Auto reset event and Mutex is non-signaled when it releases the object. The manual reset event does affect the wait functions’ state.

MFC Lock/Unlock Resource:

The MFC CMutex, CCriticalSection, CSemaphore, and CEvent classes are used to synchronize the threads in Microsoft Foundation Class library.

The CSingleLock and CMultiLock are used to control the access to the resources in multithread programming. The CSingleLock andCMultiLock classes have no base class. CSingleLock is used to lock the single synchronization object at a time. CMultiLock is used to control more than one thread synchronization objects with a particular time interval. The CSingleLock/CMultiLock Lock and Unlockmember functions are used to the lock or release the resource.

The CSingleLock and CMultiLock constructors use the CSyncObject object for locking and unlocking the resource. All the Thread Synchronization classes are derived from CSyncObject base class. So, the constructor has any one of the thread synchronization classes derived from CSyncObject. CSingleLock IsLocked member is use to find if the object is locked already or not.

The CMultiLock class is used to control the access to the resources in multiple objects. The CMultiLock constructor has an array ofCSyncObject objects and the total number of counts in the thread synchronization classes. The IsLocked member function is used to check the particular synchronization object state.

Thread Synchronization Objects:

Critical Section:

The Critical section object is same as the Mutex object. But, the Mutex object allows synchronizing objects across the process. But the Critical section object does not allow synchronization with different processes. The critical section is used to synchronize the threads within the process boundary.

It is possible to use Mutex instead of critical section. But, the critical section thread synchronization object is slightly faster compared to other synchronization objects. The critical section object synchronizes threads within the process. Critical section allows accessing only one thread at a time.

Win32 Critical Section Object:

The process allocates memory for the critical section using the CRITICAL_SECTION structure. The critical section structure declared in theWinnt.h is as follows:

minus.gif Collapse

 typedef struct _RTL_CRITICAL_SECTION { PRTL_CRITICAL_SECTION_DEBUG DebugInfo; // The following three fields control entering // and exiting the critical section for the resource LONG LockCount; LONG RecursionCount; HANDLE OwningThread; // from the thread's ClientId->UniqueThread HANDLE LockSemaphore; DWORD SpinCount; } RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;

In critical section, we allocate memory for CRITICAL_SECTION structure and initializes the critical section. TheIniailizeCriticalSection and InitializeCriticalSectionAndSpinCount are used to initialize the critical section. If we initialize the critical section, then only, we use any one of the EnterCriticalsection, TryEnterCriticalSection, orLeaveCriticalSection functions. The EnterCriticalsection function is used to enter the critical section, andTryEnterCriticalSection to enter the critical section without blocking. LeaveCricalSection is used to leave the critical section.

If any of the other synchronization object names is same as Critical section object, the Critical section object waits for the ownership infinitely. The Critical section object does not allow moving or copying the object. If we have to synchronize the thread on different processes, use Mutex object. DeleteCriticalSection function releases all the critical section objects. After calling theDeleteCriticalSection, it is not possible to call EnterCriticalsection or LeaveCriticalSection.

Example:

minus.gif Collapse

 CRITICAL_SECTION m_cs; //Initilize the critical section InitializeCriticalSection(&m_cs);

The two threads try to access the same variable. The global variable g_n tries to access two threads. The global m_csCRITICAL_SECTION structure is used to synchronize the two threads.

minus.gif Collapse

 UINT ThreadOne(LPVOID lParam) { // // Lock the Critical section EnterCriticalSection(&m_cs); // Some Process //Release the Critical section LeaveCriticalSection(&m_cs); return 0; }

Thread two:

minus.gif Collapse

 UINT ThreadTwo(LPVOID lParam) { // Lock the Critical section EnterCriticalSection(&m_cs); // Some Process //Release the Critical section LeaveCriticalSection(&m_cs); // return the thread return 0; }

MFC Critical Section object:

The CCriticalSection class provides the functionality of critical section synchronization object. The default constructor is used to construct the critical section object. The Lock and Unlock functions are used to control the resource access in the synchronization object.

The CRITICAL_SECTION’s m_sect data member allows initializing the CRITICAL_SECTION structure. The Lock function overloaded in two forms. The Lock function without any arguments is used to lock the resource. The other form of Lock function needs the number of milliseconds to wait. All the critical section members are declared in afxmt.inl file as inline functions.

Example:

minus.gif Collapse

 // Global Critical section CCriticalSection c_s; int g_C; //////////////////Thread One /////////////////////// UINT ThreadFunction1(LPVOID lParam) { // Create object for Single Lock CSingleLock lock(&c_s); // Lock lock.Lock(); // Process // Unlock lock.Unlock(); // return return 0; } ////////////Thraed 2//////////////////// UINT ThreadFunction2(LPVOID lParam) { // Single Lock Constract Critical Section CSingleLock lock(&c_s); // Lock lock.Lock(); // Process // Unlock lock.Unlock(); //return return 0; }

Event:

Event is the thread synchronization object to set signaled state or non-signaled state. The Event has two types. They are manual reset event and auto reset event.

The manual event has signaled user set to non-signaled state, uses ResetEvent function manually. The auto reset event automatically occurs when the object is raised to the non-signaled state. The event thread synchronization object is used to synchronize the particular event entered in the thread. The Event object is used to set the operating system kernel flag in the thread.

Win32 Event Object:

The CreateEvent function is used to create the event thread synchronization object. The manual or auto reset event choice is mentioned at the CreateEvent function parameter initialization. The Wait family functions (WaitForSingleObject, WaitForMultipleObjects) are use to wait when a particular event occurs. The group of objects waits for the events: the single object signaled or the entire events are signaled in the thread.

CreateEvent function is used to create manual or auto reset events. This function is used to create named and unnamed event objects.SetEvent function is used to set the event object to signal state. The ResetEvent function is used to set the event object to non-signaled state. If the function is successful, it returns the handle of that event. If the named event is already available, the GetLastErrorfunction returns the ERROR_ALREADY_EXISTS flag. If the named event is already available, the OpenEvent function is used to access the event previously created by the CreateEvent function.

Example:

minus.gif Collapse

 // Handle for Event HANDLE g_Event; // Create a manual-reset event object with no security attributes g_Event = CreateEvent( NULL, TRUE, TRUE, "Event" ); ResetEvent(g_Event); // Do Process SetEvent(g_Event);

MFC Event Object:

Event is useful for waiting if something happens (or an event occurs). CEvent class is used for event object functionality. The CEventclass is derived from the abstract CSyncObject Class. The CSyncObject is derived from the CObject mother class. The CSingleLockor CMultiLock constructor use one of the CSyncObject derived classes. The CEvent constructor specifies the manual or auto reset event options.

MFC CEvent class provides the constructor with default arguments. If we want to control our needs, we set the Ownership for the event object, manual or auto reset event flag, the name of the event object (if named event), and the security attributes. If the name matches with that of an existing event object, check the type of the object. If that object is an event, the CEvent constructor simply replies the handle of the previous event. If the name exists for any of the other synchronization object, the CEvent constructor gives an error message.

The Manual event object signaled/non-signaled uses SetEvent or ResetEvent function .The Auto Reset event occurs when it releases any one of threads in the event object. We don’t use CSyncObject directly. Because, the CSyncObject is an abstract base class. All the event members are declared in the afxmt.inl file as inline functions.

Mutex Synchronization Object:

Mutex is the synchronization object used to synchronize the threads with more than one process. The Mutex is as the name tells, mutually exclusive. The Mutex object allows accessing the resource single thread at a time.

Win32 Mutex Object:

The CreateMutex function is used to create the Mutex object. In the CreateMutex function, we initialize the named Mutex or unnamed Mutex, and set the ownership to true or false arguments.

If we create two-named Mutex using CreateMutex function within the same process, the second CreateMutex function returns error. If we create two or more Mutex objects on different processes, with the same name, when we call first time, the CreateMutex function creates the Mutex. The other CreateMutex function returns the handle of the previous Mutex object.

The OpenMutex function is used to open an existing Mutex using the supplied Mutex name. The ReleaseMutex is used to release a Mutex object. The threads wait in first in first out order for taking the ownership for the waiting threads. If we try to take the ownership twice, deadlock occurs. We the call ReleaseMutex, and then try to take the ownership.

Example

minus.gif Collapse

 HANDLE g_Mutex; DWORD dwWaitResult; // Create a mutex with initial owner. g_Mutex = CreateMutex( NULL, TRUE, "MutexToProtectDatabase"); // Wait for ownership dwWaitResult = WaitForSingleObject( g_Mutex, 5000L); // Check takes ownership // Release Mutex ReleaseMutex(g_Mutex))

MFC Mutex Object:

The MFC CMutex class is used to control the Mutex objects. The CMutex constructor has three parameters. We can specify the name of the Mutex as a parameter. If this is null, an unnamed Mutex is created. The Security attributes are used to set the security attributes for the Mutex object. If the name already exists for a Mutex object, the constructor simply returns the existing Mutex object. If the name exists for some object, the constructor fails.

To control resource access for single Mutex object, use CSingleLock class. If you wish to control multiple Mutex objects, theCMultiLock is used to control the access to resources in multithreaded programming.

minus.gif Collapse

 // Global Mutex Object CMutex g_m; int g_C; UINT ThreadFunction1(LPVOID lParam) { // Create object for Single Lock CSingleLock lock(&g_m); lock.Lock(); // Process lock.Unlock(); // return return 0; } UINT ThreadFunction2(LPVOID lParam) { // Single Lock Construct Mutex CSingleLock lock(&g_m); lock.Lock(); // Process lock.Unlock(); //return return 0; }

Semaphore Thread Synchronization Object:

Semaphore is a thread synchronization object that allows accessing the resource for a count between zero and maximum number of threads. If the Thread enters the semaphore, the count is incremented. If the thread completed the work and is removed from the thread queue, the count is decremented. When the thread count goes to zero, the synchronization object is non-signaled. Otherwise, the thread is signaled.

Win32 Semaphore Synchronization Object:

The CreateSemaphore function is used to create a named or unnamed semaphore thread synchronization object. The initial count and maximum count is mentioned in the CreateSemaphore function. The count is never negative and less then the total count value. TheWaitForSingleObject waits for more than one object in semaphore object.

The WaitForMultipleObjects function is non-signaled when all the objects are returned. The OpenSemaphore function is used to open an existing handle to a semaphore object created within the process or another process. The Releasesemaphore function is used to release the semaphore from the Thread synchronization queue. If the CreateSemaphore function has created the same named Thread synchronization object within the process, the CreateSemaphore returns 0. The GetLastError function is used to retrieve the reason for the failure.

Example:

minus.gif Collapse

 HANDLE g_Semaphore; // Create a semaphore with initial and max. g_Semaphore = CreateSemaphore( NULL, 4, 4, NULL); DWORD dwWaitResult; //take ownership dwWaitResult = WaitForSingleObject( g_Semaphore, 0L); // Check the ownership // Release Semaphore ReleaseSemaphore( g_Semaphore, 1, NULL) ;

MFC Semaphore Synchronization Object:

The CSemaphore class allows us to create the CSemaphore object. The CSemaphore class is derived from the abstract CSyncObjectbase class. The CSemaphore class constructor is used to specify the count and maximum number of resource access.

The virtual destructor is used to delete the CSemaphore class object without affecting the CSyncObject base class. The Unlockfunction is used to unlock the resources.

Conclusion:

Thread Synchronization is used to access the shared resources in a multithread environment. The programmer decides the situation for when to use the synchronization object efficiently. The MFC Thread Synchronization classes internally call the Win32 API functions. The MFC Thread Synchronization classes wrap many of the functionalities form the Windows environment.

Link:- http://www.codeproject.com/KB/threads/Synchronization.aspx

Leave a Comment

WaitforSingleObject

Synchronization is a very hot topic in all threading subjects. Almost all the programs using threading are in some way will be implementing a synchronization object in their source.

Windows provides a number of synchronization objects and objects that are wait able on the synchronization objects. WaitForSingleObject is one such wait able object. This article explains how to use the WaitForSingleObject in a windows c++ program.

This WaitForSingleObject is a single object wait function. It requires a handle to be supplied as a parameter for it to wait on. The handle can be from objects like a thread, event, mutex, semaphore etc., The function WaitForSingleObject waits till the objects are in non-signaled state. Once these objects are signaled, then WaitForSingleObject function returns immediately.

The WaitForSingleObject function can also be made to wait for either a specified amount of milli -seconds or INFINITE amount of time. When this is set to a specified amount of time, the function WaitForSingleObject returns either when it is signaled or when the time specified has elapsed. If it is made to wait for INFINITE amount of time, then it waits till the object becomes signaled.

The biggest advantage or use of WaitForSingleObject is when there are a lot of threads used and there is a need to track if all of them are complete. For example there might be a program while closing, might need to exit all the threads, close all pipes, sockets etc., legally without aborting them. In such cases, we can put a wait using WaitForSingleObject and exit once all of them give a green signal and then close the program.

A small sample is provided using a thread, an event object on which the WaitForSingleObject function waits and exits after it becomes signaled.

Leave a Comment

Older Posts »