Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / August 2003

Tip: Looking for answers? Try searching our database.

Part of the code to explain Mutex problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Leonardo Lanni - 29 Aug 2003 00:05 GMT
Ok, this is a scheme that shows my project

Process A : Creates a file-mapping empty (with no file in it) in order to be
used as a shared memory, then it creates a mutex ; both the operations have
controls to check their correct working.
Both the structure (file-mapping and mutex) have got a name, in order to be
opened by other processes, the mutex name is mtx and the filemapping name is
my_mapping.

Process B : Opens the file-mapping genereated in A by OpenFileMapping
instruction (using my_mapping name), attaches it to the memory of the
process by MapViewOfFile and gets the address of this attach.
Then it opens the Mutex (using mtx name). Both the operations have controls
to check their correct working.
Now the function of B is to allow users to manage a collection of messages
written in the shared memory installed before. It is made with 3 different
operations :
Add : adds a new message in queue to the other messages (if there is enough
space, due to collection-limits)
Remove : removes the last message in the shared memory (the last message put
in memory)
View : views all the messages in the shared memory.

Now the use of a mutex is necessary to avoid data-inconsistence : if 2 users
work on the program, they can use add function at the same time so that both
writes in the same position of memory, so that it will contain only the
informations of the last who wrote on the memory, giving data-inconsistence.
So if one wants to use those functions, immediately before the access to
shared memory there is a WaitForSingleObject on the mutex, and immediately
after the end of the operations there is a ReleaseMutex.

The problem is this : sometimes even if only one single user uses the
program function like add-remove, the program blocks as if there was some
other user working on shared memory (as if the mutex is working improperly),
and there is no way to unblock it but closing the window and restarting both
the A-B processes.
This fact happens casually, not if I do a precise procedure or activate
something.
Maybe, according to me, the cause is in the desidered mode of access to the
mutex specified in the OpenMutex operation : msdn says to use
MUTEX_MODIFY_STATE if ReleaseMutex function is needed (as in my case)
using this choose, I obtain this : the program never blocks, but the mutex
does not work (If I open 2 instances, the first is doing "add", and do "add"
also in the second, the second is not blocked by the mutex as it should be!)
if I use MUTEX_ALL_ACCESSES I obtain the situation I've described.

I've also thought it can depend on de boolean regulating the inherit rights
of the mutex, (in my case is set on FALSE).

I write now an explicative part of the code (Written in C language with
<windows.h> library)

PROCESS A:
#include <windows.h>
#define errore(a) {printf(a);ExitProcess(2);}
HANDLE shmem1,sem1;

int main(int argc, char *argv[]) {
shmem1 =
CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,500000,"my_mapp
ing");
if (shmem1 == NULL) errore("Error : Creating ShMem!\n");
printf("ShMem Installed!\n");

sem1 = CreateMutex(0,FALSE,"mtx");
if (sem1==NULL) errore("Errore : Creating Mutex");
printf("Mutex Installed!\n");

}

PROCESS B:
#include <windows.h>
#define errore(a) {printf(a);ExitProcess(2);}

HANDLE shmem,sem; char *attach;

void add/remove/view() {<FUNCTION CODE>;
WaitForSingleObject(mtx,INFINITE);
<OPERATIONS ON SHARED MEMORY>;
ReleaseMutex(sem);
}

void console() {<ALLOWS THE USER TO SELECT  THE FUNCTIONS, AFTER THE
FUNCTION IT IS RELOADED UNTIL USER CHOOSES FOR "quit">

int main (int argc, char *argv[]) {

shmem = OpenFileMapping(FILE_MAP_ALL_ACCESS,TRUE,"my_mapping");
if (shmem == NULL) errore("Error : Opening ShMem!\n");
printf("ShMem Opened!\n");

attach =(char*)MapViewOfFile(shmem,FILE_MAP_WRITE,0,0,0);

if (attacco == NULL) errore("Error : attaching ShMem to memory process!\n");
printf("ShMem Attached to memory process!\n");

sem = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mtx");
if (sem==NULL) errore("Error : Opening Mutex!\n");
printf("Mutex Opened!\n");

<LAUNCH CONSOLE> --do until "quit"--

ExitProcess(0);}

NB : First I execute A, then I execute B (while A is still working)

Thank you again!!!!!!
Leonardo
Jip from Paris - 29 Aug 2003 10:14 GMT
I would track the pb in add/remove/view function. The WaitForSingleObject
call is strange. It uses an unknown variable mtx. I assume this is a typo
and you actually do

WaitForSingleObject(sem, INFINITE);

Now, i would closely look at <OPERATIONS ON SHARED MEMORY>; seeking for an
execution path leading to returning from the function without executing the
ReleaseMutex function call.

Moreover, assuming that SEH is something you can consider, I would wrap the
<OPERATIONS ON SHARED MEMORY>; in a __try / __finally block.

> Ok, this is a scheme that shows my project
>
[quoted text clipped - 55 lines]
> int main(int argc, char *argv[]) {
> shmem1 =

CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,500000,"my_mapp
> ing");
> if (shmem1 == NULL) errore("Error : Creating ShMem!\n");
[quoted text clipped - 44 lines]
> Thank you again!!!!!!
> Leonardo

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.