How to know there are multiple instances inside application itself,not through ssh


 
Thread Tools Search this Thread
Top Forums Programming How to know there are multiple instances inside application itself,not through ssh
# 1  
Old 09-15-2009
How to know there are multiple instances inside application itself,not through ssh

hi,

As you know, in Windows Programming, in WinMain method you will give n argument which you can know if this is another instance of the application or just the fresh execution.
How this functionality can be achieved in Linux application? Most of suggestions i have seen here in threads are done through ssh and shell scripting not inside application itself.

Regards,
Behzad
# 2  
Old 09-15-2009
Probably that should be controlled and/or configured via master application, which will take care of whether to spawn a new instance of application or not based on load and other parameters.

With that, master application has the knowledge of all its instances it has spawned and if required can pass the nth instance info to a specific instance.
# 3  
Old 09-15-2009
Quote:
Originally Posted by Sedighzadeh
hi,

As you know, in Windows Programming, in WinMain method you will give n argument which you can know if this is another instance of the application or just the fresh execution.
Remember, this is Windows. Just because they designed functionality doesn't mean it works. From MSDN:
Code:
hPrevInstance
    [in] Handle to the previous instance of the application. This parameter is always NULL.

Quote:
How this functionality can be achieved in Linux application?
Probably much the same as you must do in Windows, or for that matter, in those shell threads: Create a lockfile of some sort.
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>

// Have this called at exit
void cleanup(void)
{
        unlink("pidfile");
}

int get_lockfile(void)
{
        char buf[64];
        int len;
        // O_EXCL tells it to fail if the file already exists.
        int fd=open("pidfile", O_RDWR|O_CREAT|O_EXCL);
        if(fd < 0)
                return(-1);

        fprintf(stderr, "PID %d has the lockfile.\n", getpid());
        // Write the PID to the file
        len=sprintf(buf, "%d", getpid());
        write(fd, len, buf);
        close(fd);

        // Have cleanup() called automatically when main() returns
        atexit(cleanup);
        return(0);
}

int main(void)
{
        if(get_lockfile() < 0)
        {
                fprintf(stderr, "PID %d couldn't get lockfile\n", getpid());
                return(1);
        }

        sleep(10);
        fprintf(stderr, "PID %d releasing lockfile\n", getpid());
        return(0);
}

Depending on what system you're on, there could be lots of other ways of doing it too. Cooperative locking would be one, just try to make a cooperative lock on your own executable file...

Last edited by Corona688; 09-15-2009 at 04:14 PM.. Reason: forgot a return value
# 4  
Old 09-16-2009
When you startup, you could traverse through the process list to see if there is another process with the same name. For that to happen, you need to also check if your process is running as the original name. This ofcourse is limited to those OS'es that have the /proc available.

Since you are familiar with the Windows Programming, you could use something similar to named events. See this link for events in Windows and Linux - Port Windows IPC apps to Linux, Part 2: Semaphores and events
# 5  
Old 09-17-2009
On Solaris, you can also do an FUSER variant of the utssys system call on the executable and see how many processes have the file open for execution. Look at sys/utssys.h and sys/syscall.h and the source code for the "fuser" utility at OpenSolaris.org to see how you can do that.

Or your can just fork off a subprocess using system() to run "fuser" and parse the output. That could also work on Linux by using "lsof".

I do not recommend replicating what "lsof" does in your own source code. Last time I looked at that source code, Linux did not have the equivalent of the Solaris utssys/fuser system call, so instead of a single system call "lsof" was forced to walk the entire /proc process tree looking for results. That added up to a lot of source code. Replicating that functionality in your app would add a lot of complexity along with dependencies on how Linux implements things in /proc, something you probably don't want to do.
# 6  
Old 09-17-2009
Suggestion:

Create a named semaphore with sem_open. This has one disadvantage - if the program crashes the semaphore persists. If you perceive this as a problem for you then
create a program special option (like -f) to force the program to call sem_unlink() before it does anything else. The option is not provided under normal operation.

1. sem_open() - then check the semaphore setting to see if it is less than the maximum limit you want to impose.
2. if it is at max, then exit, (Windows does something like this) else increment the sempahore.
3. register an atexit function:
decrement the semaphore
if the semaphore is now zero, call sym_unlink()
4. run the rest of your code
5. exit (#3 handles the sempahore for you.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Connecting to multiple instances on the same server

I have a db server with two instances...trying to create an expect script that will scan both the listener and the alert log files and retrieve their sizes. Issue we have is that the .profile is hard coded with one of the databases name and script is always pulling from the same instance (testdb1)... (1 Reply)
Discussion started by: donpasscal
1 Replies

2. Shell Programming and Scripting

Executing multiple ssh commands inside a shell simultaneously

I would like to execute a commands in four different servers through ssh at a single instance(simultaneously). Below are the details with examples, ssh user1@server1 "grep xxxx logs" ssh user1@server2 "grep xxxx logs" ssh user1@server3 "grep xxxx logs" Each statement will take some... (4 Replies)
Discussion started by: Amutha
4 Replies

3. Programming

Multiple instances of pthread

Suppose I declare pthread_t clear_thread; and then pthread_create(&clear_thread, &detach, clear_message, this); the thread is supposed to go away, perform the service it is intended to procide, and then kill itself. A little while later, I require this service again, so I say ... (2 Replies)
Discussion started by: clerew
2 Replies

4. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

5. Shell Programming and Scripting

Multiple instances of a job.

Could you please let me know how to create/make a multiple instances of a job/process in ksh(shell scripting). i.e., at present the parent script is calling another child/dependent script for only once. What we want is, the parent script itself has to execute multiple times, and in each one it... (1 Reply)
Discussion started by: Gangegowda
1 Replies

6. Solaris

ypbind - multiple instances starting

I have built this Solaris 10 server, uses NIS. When the server starts up, two instances of ypbind start. This prevents the server from binding to any domain. The SMF in turn prevents any other network services (sshd and the like) from starting up. Has anyone seen this problem before? (0 Replies)
Discussion started by: blowtorch
0 Replies

7. Shell Programming and Scripting

detecting multiple instances

Hi Gurus I have a requirement like this. i use solaris OS.. if there are 2 instances of the same ksh file running in the directory, i need to kill the ksh file that started to run latest. suppose ragha.ksh starts running thru cron in abc/xyz directory now ragha.ksh started running by any... (3 Replies)
Discussion started by: ragha81
3 Replies

8. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies

9. UNIX for Dummies Questions & Answers

Multiple file instances

I am capturing text based reports with a specific program, which works no problem. However, since I send report warehouse output as they are migrated from the database software, on occasion when two capture process' initiate simultaneously, the capture file locks up. Is there a way to setup (in... (1 Reply)
Discussion started by: gozer13
1 Replies

10. UNIX for Advanced & Expert Users

multiple instances of syslogd - is it possible?

I would like to start up multiple instances of syslog daemon. I am having a little difficulty. Is this at all possible? I have separate syslog.conf1.... syslog.conf5 files. I have linked the daemon to separate files syslogd1 ... syslogd5 I have arranged the rcd.2 start/stop scripts for... (9 Replies)
Discussion started by: Gary Dunn
9 Replies
Login or Register to Ask a Question