Thread: Multiple commandlines accessing same instance of a program

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Multiple commandlines accessing same instance of a program

    I want to write a program that same instance of it can be accesed by multiple comandlines. What kind of a program it should be and how to write it?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You can't have several users using the exact same instance of a program in memory. Each terminal (command line) has it's own environment, resources, etc, which might end up affecting the single instance in unpredictable ways. Such a feature might also open up considerable problems with security, synchronization, race conditions and resource sharing. You can, however, get several instances of the same program to communicate with each other and share some of their data. This can be achieved with pipes, sockets or shared memory. Your best bet is probably to Google for Linux IPC tutorials (IPC = Inter-Process Communication).

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    If you mean you want the program image to be shared in main memory, then that's already done automatically for read-only parts of the image.

    If you mean you want the program image to be shared on disk, with two different names, make a link to it. Whatever it's invoked as will be argv[0]. I think this works for both hard and soft links.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by anduril462 View Post
    This can be achieved with pipes, sockets or shared memory. Your best bet is probably to Google for Linux IPC tutorials (IPC = Inter-Process Communication).
    You can set the program up so that when it starts, it uses IPC to check on a local socket. If something answers, it will know an instance of the program is already running, and this changes its behavior. If not, it takes over the socket and deals with any subsequent connections to it.

    That's how, eg, most web browsers and editors work. If you use "firefox http://somepage" on the command line and firefox is already running, that page opens in the running firefox, instead of starting a new instance. This is because the first instance launched a server which then responds to requests on its local socket, and all instances use the same socket -- when they start, they check to see if one is already running, if it is, they send a request and exit. You can do this on a per user or global system level.
    Last edited by MK27; 05-07-2011 at 06:04 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple processes accessing same shared library
    By yogeeshgv in forum Linux Programming
    Replies: 1
    Last Post: 07-28-2010, 02:18 PM
  2. prob with multiple forms accessing header file
    By silkyt in forum Windows Programming
    Replies: 7
    Last Post: 11-03-2007, 04:14 PM
  3. Replies: 2
    Last Post: 09-09-2007, 12:58 AM
  4. multiple instance of ADT
    By Kinasz in forum C Programming
    Replies: 4
    Last Post: 04-07-2004, 06:49 AM