The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: P1 on January 06, 2006, 04:42:37 PM

Title: Mutex Problem
Post by: P1 on January 06, 2006, 04:42:37 PM
I have a piece of code I am limiting to a single instance on a W2K3 server with multi-processors and multiple terminal sessions ( With Remote Desktop ).

The mutex does not work across the multiple terminal sessions.  So in effect, I can create the same mutex in all terminal sessions including the current deskstop window.  So I am getting multiple instances of the same code running.

I have been doing research on MSDN with no results as of yet.

Has anyone ran into this problem before?

Does anyone have a single instance code technique that does work for a server with multi-processors and multiple terminal sessions?

Regards,  P1  :8)
Title: Re: Mutex Problem
Post by: Tedd on January 06, 2006, 06:59:36 PM
Synchronize on a file?

Open the file with an exclusive lock, run the code, and then release it when you've finished. Anyone else trying to open the file will fail and can't (shouldn't) run the code. The file itself would be on the server, and zero length.
Title: Re: Mutex Problem
Post by: P1 on January 10, 2006, 11:02:03 PM
Exclusive File Locking solved one problem, But ....

I have a new problem.   :tdown

I can not distinguish which VM session is going to get "there" first.  This piece of software should only run in the physical server console desktop and not in a remote desktop session.

Any ideas ?  I will run it as a service, if I have too.

More research.   :eek

Regards,  P1  :8)
Title: Re: Mutex Problem
Post by: Tedd on January 11, 2006, 01:15:44 PM
Shouldn't the server program be different from the others?
And if only the server vm should be running the code, then why should the others even attempt to?
Unless it should be the server first, and then any others?
I suppose you would want some kind of identifier for each session and limit based on that.
Title: Re: Mutex Problem
Post by: P1 on January 11, 2006, 02:25:01 PM
I have MacroScheduler Running on the server for timing of automated tasks.  If I ( or another Admin ) remote desktop into the server, the remote session is running a copy of MacroScheduler too.  So the same task gets started twice.  But the seperate VM sessions are not aware of the each other.

It's boiling down to managing the VM sessions and/or a re-write to a service.

Regards,  P1  :8)
Title: Re: Mutex Problem
Post by: Tedd on January 12, 2006, 12:32:58 PM
All I can think is that each VM should check where it is actually running and then only run if it's really on the server.
If the server has a file containing its hostname, then each session could check its own hostname against the contents of that file. But this assumes getting your 'own' hostname will return the local hostname and not the one of the remote session.
Title: Re: Mutex Problem
Post by: P1 on January 12, 2006, 02:49:40 PM
Quote from: Tedd on January 12, 2006, 12:32:58 PMAll I can think is that each VM should check where it is actually running and then only run if it's really on the server.
That's today research project, to find some code to ID the VM sessions.

Thanks for your input so far !!!

Regards,  P1  :8)
Title: Re: Mutex Problem
Post by: P1 on February 01, 2006, 07:09:51 PM
Here is the code snippet to limit code to either Console Only Mode or Execute for a Remote session.
Note:  For Windows Terminal Servers, W2K and Up.

invoke GetCurrentProcessId
invoke ProcessIdToSessionId,eax,addr dwSessionID
mov eax,dwSessionID
or eax,eax
jz Console
;invoke MessageBox,0,ADDR szRemote,ADDR szMsgTitle,MB_OK
jmp The_Exit
Console:
;invoke MessageBox,0,ADDR szConsole,ADDR szMsgTitle,MB_OK


Regards,  P1  :8)
Title: Re: Mutex Problem
Post by: lingo on February 01, 2006, 07:41:21 PM
some ideas here:
http://www.developerfusion.co.uk/show/1716/8/

Regards,
Liingo
Title: Re: Mutex Problem
Post by: P1 on February 01, 2006, 09:02:08 PM
QuoteHe points out that in the Terminal Server edition of NT (which is built into Windows 2000), the kernel no longer has a single "global" namespace, but in fact each Terminal Server session has a private namespace. System services share a common namespace for what is called the "console session". He points out that "this all results in consuming much more memory and making some programming tasks quite tricky, but the result is that every user logged into the Terminal Server is able to start its E-Mail client".
Thanks for making my point.

So this is why I/you need the code above.

Regards,  P1  :8)
Title: Re: Mutex Problem
Post by: sluggy on February 09, 2006, 08:24:50 PM
Quote from: P1 on February 01, 2006, 07:09:51 PM
Here is the code snippet to limit code to either Console Only Mode or Execute for a Remote session.
Note:  For Windows Terminal Servers, W2K and Up.
Excellent :) I've had to code for terminal sessions before, but never to restrict the app to only one instance. Personally i was thinking you needed a service, but kinowing an alternate option is great  :U
Title: Re: Mutex Problem
Post by: P1 on February 10, 2006, 02:35:47 PM
Quote from: sluggy on February 09, 2006, 08:24:50 PM... Personally i was thinking you needed a service, but kinowing an alternate option is great  :U
Well, this is a special case these days.  Most rack servers don't have a physical console.  But we maintain a a rack with a KVM to all servers.  I have found, in a pinch, you need to be at the server with a phyiscal connection.  When they get a RDP that works with a BIOS setup screen, we can drop it then.

I deal with the physical and spiritual issues of computing.

Regards,  P1  :8)