Re: My way to check if a progam is already running



On a sunny day (Thu, 21 Jun 2007 11:52:13 -0700) it happened David Schwartz
<davids@xxxxxxxxxxxxx> wrote in
<1182451933.333284.25880@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>:

On Jun 21, 4:17 am, Jan Panteltje <pNaonStpealm...@xxxxxxxxx> wrote:

That will prevent _that_ problem,

I thought that was 'the problem'.

but not the problem that you still have 2
the same applications running where the user is supposed to use one.

Why not?

Well, you'd want to be nice and say: "Hey Dummy, you already started WhatEver." :-)


No way does it solve some practical problem where data is dynamically updated.

Sure it does.

Ok I see your point, but all this file locking is it not more complicated
then the 10 lines of code at the start of main() that I proposed?
Especially as many files (not just one) are involved?

/* This assumes there is a directory ~/.MyProgram/

struct passwd *userinfo;
char temp[4096];
FILE *fptr;
int a, b;

/* get user info */
userinfo = getpwuid(getuid() );

/* get home directory */
home_dir = strsave(userinfo -> pw_dir);

/* test if already a MyProgram running */
sprintf(temp, "pidof MyProgram > %s/.MyProgram/pids", home_dir);
ftr = popen(temp, "w");
pclose(ftr);

sprintf(temp, "%s/.MyProgram/pids", home_dir);
fptr = fopen(temp, "r");
if(fptr)
{
a = fscanf(fptr, "%d %d", &b, &b);
fclose(fptr);

if(a == 2)
{
fprintf(stderr, "MyProgram: An other MyProgram is already running, aborting.\n");

exit(1);
}
}


.



Relevant Pages