Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hi, i want to write a program which should run as only one instance on linux. That is if iam already running an instance of the program/application and run the ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    7

    Detecting an instance of running process

    Hi,
    i want to write a program which should run as only one instance on linux.
    That is if iam already running an instance of the program/application and run the same program/application again ( by excuting it from shell/command root) then it should detect that the program is already running in background and it should display the user-interface of the background application program that is already running. The newly excuted program should not run as a new seperate process. This is easily to implement in windows platform. But i need to implement in the linux platfrom in C.

    One thing is that we can do with the system() call and use 'ps ax | grep progname as parameter. But the thing is that the grep will find now three process that are
    ./progname -- backgroudnd previous process
    ./progname -- newly created process
    grep progname -- the grep itself

    the thing is that we can find the process of the latest process using getpid() system call and filter the background process. but how to activate the background running application user interface to bring front ( that is display the applcation user inter face) and close the newly run process.

    It can be detected, the background process, but how do i bring the specific process window ( it it has GUI or ncurses based GUI) to fore front for the user to view.

    is there any way to do this.
    if there is any better way of doing this from c instead of using system() call for detecting the instance of program in background.

    Any help is worth commendable.

    Suresh Guduru

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    One common approach is this:

    When you start an instance of the program, have it check a predetermined socket. If somebody is already listening on this socket, send him a message (so that he can come to the foreground) and exit. If nobody is listening, then this is the first instance, and do the normal procedure. Don't forget to listen on this socket for messages.

    Does this approach make sense, or should I explain a bit further?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    7
    It can be done that way.
    Thanks for your reply.
    But in that approach iam wasting a port for an application for just checking the instance.
    Is there any way to do using just system calls or manipulaion of files.
    Files can be used for that matter but logic would be some what crude.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    in that approach iam wasting a port for an application for just checking the instance.
    You are actually only using one port for the whole system, for all instances of this application combined. That is not expensive.

    You are also using one open socket, one open file descriptor, for each instance of the application. That is not expensive either.

    I'd go with Cabhan's idea.

    But if you don't really want to do network programming, you can use a named pipe (FIFO). Each instance of the application can use fcntl(F_SETLK) on that FIFO; only one will succeed, and that will be true until only until it quits or blows up for any reason. As long as one instance has that lock, the others will fail and, in that failure, will get the pid of the process holding the lock. That process can use select() to determine whether any input is waiting on that FIFO; other processes can write data to the FIFO, despite the lock, because the lock is only advisory. If the original instance died between the time a secondary instance discovered that the lock was in use and the time it tried to write to the FIFO, it would get a broken pipe status (and possibly a broken pipe signal).

    But I like Cabhan's idea better.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Jan 2008
    Posts
    7
    ok
    Thank you.

    but even if i detected the instance of application (that is one running background), how do i bring it to front to display on the screen. That is i detected the application and sent a message to the background application that it should be displayed in foreground. If iam doing GUI with ncurses how do i enable the application to be on top of all windows.

    any clues????

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm a bit confused here. You have several applications, different processes, all using ncurses to put windows on the same screen?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Ah...I had been assuming that you were using something like Gtk, where you had complete control over the window...

    Does ncurses have some function that requests it to receive focus? Basically, if you find that an instance of the program already exists, you send it a message. The instance receives the message and requests focus, which would (in Gtk or Qt, at least) move the window to the top.

    If ncurses has something like this, great. Otherwise, I don't know enough about ncurses to say.
    DISTRO=Arch
    Registered Linux User #388732

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm assuming that gsuresh_01 has some sort of way to have different processes use ncurses to put different windows on the same screen. I didn't know that could be done, and I'm waiting for him to say how it's done.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  9. #9
    Just Joined!
    Join Date
    Jan 2008
    Posts
    7
    i try to elaborate the situation.
    Its very well ok if iam using Qt or GTK for my application, and it is easy to activate the window and bring it to front on the screen. But as per ncurses concerned, idid not find any such functon to be.

    I want to clarify Bill that iam using right now one application which is written in ncurses GUI just similar to iptraf utility in linux. You can think that my application is just the same as iptraf. My application has to have the capability to run in background without GUI as well as GUI with ncurses if needed.

    Now you assume that when iam running my application in background( that is there is no terminal/console in which this ncurses is displayed, but the only instance of process is running in background).

    Now if i try to excute the same command in any terminal/console , i have to detect that there is already another instance running in background and i want to display the ncurses GUI of background instance in the same terminal/console.

    I think iam clear to all of you.

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I think this is doable.

    Your second process, the one which discovers that there's already an instance running, will have to communicate to that first instance the tty name of the second instance, so that the first instance will know where to send output and get input. The second instance can discover its tty name with a ttyname() call.

    The first instance, instead of doing an initscr() call, does a newterm() for that second instance's tty name followed by a set_term(). Then the first instance does all the ncurses things it wants to on the second instance's screen. It should do endwin() when it's finished.

    Meanwhile, the second instance needs to keep running, perhaps by sleeping, until the first instance is done performing ncurses operations on the second instance's screen. Otherwise, if the second instance exits prematurely, then bash gets control again and bad things are likely to happen. I'm not sure what the exact extent of the badness is; just avoid it.

    The second instance can simply wait for the first instance to notify it somehow that the first instance is done. With the network connection recommended previously by Cabhan, this is of course trivial.

    Of course, the first instance and the second instance should both be the same user; otherwise the first instance might have difficulty writing to and reading from the second instance's screen.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...