Re: translating "create Semaphore" to Linux



On 29 Ago, 13:28, GHUM <haraldarminma...@xxxxxxxxx> wrote:
hello,

in my application I am using

hSem = win32event.CreateSemaphore (None, 1,
1,"stringincludinginterfaceandport")
rt=win32event.WaitForSingleObject (hSem, 0)
if rt != win32event.WAIT_TIMEOUT:
   really_do_start_my_app()
else:
   print "application allready running"

to make sure that only ONE instance of the application is running at a
time. (as it implements a local webserver, that is necessary. Two
webservers listening on one port is bad)

Now I am going to make this application run on Linux. How can I get
similiar behaviour on Linux?

I know of the .pid files that get written by some server processes ...
BUT they do not get cleaned up on unclean shutdown of the application.

is there some better method?

Or some module which wraps the details of .pid-files quite nicely?
(like "trying to remove to check if other instance is still
running...., failing properly on missing write privs etc.)

best wishes,

Harald

The best way I know to do it is to use fnctl.flock or fcntl.lockf
functions. I never used it, so can just point you to the
official documentation. The idea is that your applications should take
exclusive access to one of the application files, so that if it is
started a second time, the second run will find the file locked and
understand that there is an instance started.
AFAIK, if the process locking a files dies, the OS releases the lock,
so there is no possibility of stale locks (check this!).

Anyway, you could simply use your server socket as lock. It is not
possible to have two processes accepting connections on the same port,
the second process would receive an error 'Address already in use'.
You could use it as signal that there is
already an instance of the application running. This method should be
available in both Windows and Linux (and various
Unix flavours too), so your code would be more portable.

Ciao
----
FB
.



Relevant Pages

  • Re: uCLinux on Samsung S3C4510B (ARM7TDMI) based wireless router
    ... serial port or printer port, ... linux or further. ... > router, access point and print server, all at the same time, preferably ... > and openap-ng) options as well as uCLinux. ...
    (comp.os.linux.embedded)
  • Re: Unix / Linux Program Port to OS/2 with GCC and LIBC: HowTo Support / Use of Unix Function "Symb
    ... The discussion of the MPlayer port comes up with the above subject. ... OS/2 port on Hobbes: http://hobbes.nmsu.edu/cgi-bin/h-search?key=coreutils_5.93 ... The Linux symbolic link function is introduced ...
    (comp.os.os2.multimedia)
  • Re: TOP Network Interface Port of a Sun Ultra 30
    ... What I had was a PCI card in my Linux ... this device connects to an Ethernet port. ... on the Sun that makes it not as possible to support the DEVICES I have on ... significant speed difference if I were to connect this to a network card ...
    (comp.sys.sun.hardware)
  • Re: Serial port blues
    ... digital amateur radio on Linux, because I'm curious whether or not the ... the serial port can only be controled by the kernel. ... processes without needing to hit the reset button. ...
    (Linux-Kernel)
  • RE: seeking a better understanding
    ... were to breach that port, could they do more than deface my website? ... or do I need a middle box running some form of firewall ... Other boxes are Linux. ... use on a linux machine, and do the spot trojans as the MS ones do? ...
    (Security-Basics)