Allowing only one Instance of an application

Hi,

I need my application to run only one instance at a time.
I tried to use flock() or flockfile() but have not succeeded with this.

Can someone share a piece of code ?

Is there a better way to do that ?

PS : I’m using QNX 6.2.

TIA

You could try opening a AF_UNIX socket and binding it to a filename. That
should do it.

“Y.LEROUX” <A_ENLEVER_y.leroux@actris.com> wrote in message
news:bbmqmn$rgi$1@inn.qnx.com

Hi,

I need my application to run only one instance at a time.
I tried to use flock() or flockfile() but have not succeeded with
this.

Can someone share a piece of code ?

Is there a better way to do that ?

PS : I’m using QNX 6.2.

TIA




\

Thanks but how to do bind to a filename ?
A piece of code will help me

TIA


You could try opening a AF_UNIX socket and binding it to a filename. That
should do it.


Hi,

I need my application to run only one instance at a time.
I tried to use flock() or flockfile() but have not succeeded with
this.

Can someone share a piece of code ?

Is there a better way to do that ?

PS : I’m using QNX 6.2.

TIA

Using name_attach() for this purpose was suggested by David Gibbs and
explained by Xtang in the thread “resmgr_attach and RESMGR_FLAG_EXCL” in
the qdn.public.qnxrtp.os group. Here’s a tiny test program that
demonstrates the approach:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
#include <libgen.h> // basename
#include <unistd.h> // sleep

void runonce ()
{
extern char **_argv;

if (!name_attach(NULL, basename(_argv[0]), 0)) {
if (errno == EEXIST)
fprintf(stderr, “%s is already running\n”, basename(_argv[0]));
else
fprintf(stderr, “name_attach got error %d, %s\n”,
errno, strerror(errno));
exit(1);
}
}

int main (int argc, char *agrv[])
{
runonce();
sleep(60);
return 0;
}



“Y.LEROUX” wrote:

Hi,

I need my application to run only one instance at a time.
I tried to use flock() or flockfile() but have not succeeded with this.

Can someone share a piece of code ?

Is there a better way to do that ?

PS : I’m using QNX 6.2.

TIA

Thanks for your help it works perfectly :wink:

Have a nice day !

You wanna piece of code for every task that you need to accomplish? RTFM…

“Y.LEROUX” <A_ENLEVER_y.leroux@actris.com> wrote in message
news:bbn0ib$5cp$1@inn.qnx.com

Thanks but how to do bind to a filename ?
A piece of code will help me

TIA


You could try opening a AF_UNIX socket and binding it to a filename.
That
should do it.


Hi,

I need my application to run only one instance at a time.
I tried to use flock() or flockfile() but have not succeeded with
this.

Can someone share a piece of code ?

Is there a better way to do that ?

PS : I’m using QNX 6.2.

TIA

John A. Murphy wrote:

Using name_attach() for this purpose was suggested by David Gibbs and
explained by Xtang in the thread “resmgr_attach and RESMGR_FLAG_EXCL” in
the qdn.public.qnxrtp.os group.

Can you do that when not root?

John Nagle

John Nagle <nagle@downside.com> wrote in message
news:bcee59$kn$2@inn.qnx.com

John A. Murphy wrote:
Using name_attach() for this purpose was suggested by David Gibbs and
explained by Xtang in the thread “resmgr_attach and RESMGR_FLAG_EXCL” in
the qdn.public.qnxrtp.os group.

Can you do that when not root?

Yes. That’s actually the first reason name_attach()'d name is exclusive.
Cause if anybody
can attach a name, anybody can “hijack” your attach’d name if name_attach()
is not exclusive.

-xtang

“Xiaodan Tang” <xtang@qnx.com> wrote in message
news:bcf8lq$qre$1@nntp.qnx.com

John Nagle <> nagle@downside.com> > wrote in message
news:bcee59$kn$> 2@inn.qnx.com> …
John A. Murphy wrote:
Using name_attach() for this purpose was suggested by David Gibbs and
explained by Xtang in the thread “resmgr_attach and RESMGR_FLAG_EXCL”
in
the qdn.public.qnxrtp.os group.

Can you do that when not root?

Yes. That’s actually the first reason name_attach()'d name is exclusive.
Cause if anybody
can attach a name, anybody can “hijack” your attach’d name if
name_attach()
is not exclusive.

Anybody can still attach your name before you did, since they all go into
the same place. Using UDS you can put your file into a place (like your
installation directory) where only certain user or root can create files.
More secure and portable.

– igor