{"id":440,"date":"2013-01-04T10:58:01","date_gmt":"2013-01-04T09:58:01","guid":{"rendered":"http:\/\/www.martyndavis.com\/?p=440"},"modified":"2013-03-29T16:51:04","modified_gmt":"2013-03-29T15:51:04","slug":"mono-and-system-wide-mutexes-on-linux","status":"publish","type":"post","link":"https:\/\/www.martyndavis.com\/?p=440","title":{"rendered":"C#, Mono, and System-Wide Mutexes on Linux"},"content":{"rendered":"<p>Here&#8217;s another little &#8220;gotcha&#8221; from my experimentations in running C# apps cross-platform. The following code implements a system-wide mutex to ensure that only one instance of the program is running at any one time:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static void Main(string&#x5B;] args){\r\n\tMutex mutex = null;\r\n\ttry{\r\n\t\tmutex = Mutex.OpenExisting(&quot;test.martyndavis.com&quot;);\r\n\t\t\/\/ if we *can* open it we're an ADDITIONAL instance\r\n\t\tConsole.WriteLine(&quot;Application is already running&quot;);\r\n\t\treturn;\r\n\t} catch(WaitHandleCannotBeOpenedException){\r\n\t\t\/\/ if we get in here then the mutex doesn't exist,\r\n\t\t\/\/ so we are the only instance running on this machine\r\n        mutex = new System.Threading.Mutex(true, &quot;test.martyndavis.com&quot;);\r\n\t}\r\n\ttry{\r\n\t\tConsole.WriteLine(&quot;Doing stuff... press a key&quot;);\r\n        Console.Read();\r\n\t} finally{\r\n\t\t\/\/ Although mutex implements IDisposable, it doesn't\r\n\t\t\/\/ call Release Mutex when being Disposed(),\r\n\t\t\/\/ hence usage of try\/finally instead of 'using'.\r\n\t\tmutex.ReleaseMutex();\r\n\t}\r\n}\r\n<\/pre>\n<p>Running this on Windows it behaves as one would expect. Run one instance you get &#8220;Doing stuff&#8230; press a key&#8221; output, and, if you try to run a second instance in another DOS box you get &#8220;Application is already running&#8221;.<\/p>\n<p>On Linux though it&#8217;s a different story &#8211; each instance runs as if it&#8217;s the only copy on the machine.<\/p>\n<p>Doing some digging turns up <a href=\"http:\/\/www.mono-project.com\/Release_Notes_Mono_2.8#Shared_handles_are_disabled_by_default\">this note in Mono&#8217;s release docs<\/a> &#8211; you can see that shared handles are now turned off by default &#8211; you can turn them on by setting the environment variable\u00a0<strong>MONO_ENABLE_SHM<\/strong>, but for the application I&#8217;m working on I think I&#8217;ll simply avoid mutexes and use something else to ensure one instance &#8211; a lock file in the application&#8217;s data directory will be simple, and work cross-platform.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s another little &#8220;gotcha&#8221; from my experimentations in running C# apps cross-platform. The following code implements a system-wide mutex to ensure that only one instance of the program is running at any one time: public static void Main(string&#x5B;] args){ Mutex mutex = null; try{ mutex = Mutex.OpenExisting(&quot;test.martyndavis.com&quot;); \/\/ if we *can* open it we&#8217;re an &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.martyndavis.com\/?p=440\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C#, Mono, and System-Wide Mutexes on Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/440"}],"collection":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=440"}],"version-history":[{"count":6,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":443,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/440\/revisions\/443"}],"wp:attachment":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}