Solved

Semaphore

Posted on 1999-01-16
2
214 Views
Last Modified: 2010-04-15
When do we have to use OpenSemaphore()???  And what will happens if my application does not use UnmapFile Viewing() and CloseHandle() atfer the application had finished? My application was able to access the Shared memory many times... but to a certain limit of continuous accessing the shared memory(about 15 mins) without using the closhandle and unmapFile Viewing(). What could be the main cause? Thanx!
0
Comment
Question by:Meng
2 Comments
 

Author Comment

by:Meng
ID: 1258155
Please send your answers to my office account at ho_cm@gitpl.com.sg   Thanx a billion!
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1258156
>> When do we have to use OpenSemaphore()?

The OpenSemaphore function enables multiple processes to open handles of the same semaphore object.  The function succeeds only if some process has already created the semaphore by using the CreateSemaphore function.

>> what will happens if my application does not use UnmapFileViewing() and CloseHandle() atfer the application had finished?

UnmapViewOfFile() is needed because although an application may close the file handle used to create a file mapping object, the system holds the corresponding file open until the last view of the file is unmapped.

When a process exits, all of the object handles opened by the process are closed.  Thus, technically it is not strictly necessary to use CloseHandle() but it is a good programming practice to do it anyway.

From KB article Q125713:

Necessity of Unmapping All Views of a Mapped File
-------------------------------------------------
 
Windows maintains an internal handle to a file mapping object for each view of that object, whether created by MapViewOfFile() or MapViewOfFileEx().  This internal handle is kept in addition to the handle returned by CreateFileMapping().  The internal handle is not closed until the view associated with the handle is unmapped by calling UnmapViewOfFile(). To completely close a file mapping object requires that all handles for the object, including internal handles, be closed.  Thus, to close a file mapping object, all views of that object must be unmapped, and the handle returned by CreateFileMapping() must be closed.
 
Extant unmapped views of a file mapping object will NOT cause a CloseHandle() on the object's handle to fail.  In other words, when your handle to the object is closed successfully, it is not necessarily true that all views have been unmapped, so the file mapping object has not necessarily been freed.
 
Failure to properly unmap all views of the object and to close the handle to the object will cause leaks in the application's paged pool, nonpaged pool, virtual bytes, and also in the system wide committed bytes.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now