ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have written one server program(daemon) in C/Fedora core II. I want to resrict the multiple instance of the same server program running in one machine? How to do that? Thanx in advance.
Well..........I can think of 2 options -
1. Put some kind of flag in the filesystem. OR
2. Scan thro the /proc directory to check if another instance is alive or not
there is this file called cmdline in every dir of /proc
which tells you the exact location of the executable and path used
so you could use that to identify your process
man proc for details
you could create some file in /tmp dir when ever the daemon is started
which could be checked when the process is starting.. so that even
if the exact same copy with a different name is executing you can stop.
Well but then there might be a situation when the process is killed and
the file may be left without deletion.. this might again cause problem.
It leaves a file sitting there (you probably want to put it in /tmp), but it only complains when another process actually has a lock on the file...just the fact that the file exists isn't enough to make it complain.
It leaves a file sitting there (you probably want to put it in /tmp), but it only complains when another process actually has a lock on the file...just the fact that the file exists isn't enough to make it complain.
I have created the oneproc.lock in my temp directory. For the lock file oneproc.lock do I need to give permission to the file as 666?
Depends who is running the prog. Remember perms are ugo (user/group/other). The only 'people' who need access to the file are those that are likely to be using the prog...
If it's always only run by users in one group, then 'other' aka world needs no perms on the file.
Distribution: Red Hat - 6.2,7.3,9.0,FC3,FC4, FC5, Debian-3.1, Ubuntu 7x,8x,10x, DSL
Posts: 14
Rep:
Linux can use semaphores as well. There is the old stye using semctl and the new POSIX style using sem_open. The old style is available on most all Linux boxes, but the new POSIX style may not be available.
For the old style, you use a ftok to get a key, then use the key to get a semaphore, using IPC_EXCL|IPC_CREAT. These semaphores must be explicitly removed with semctl and the flag IPC_RMID.
If you use the file locking above and want to be able to write to more than one byte of the file, set fl.l_len = 0; This is a special case and will lock the whole file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.