# Control multiple program instances - open multiple files problem

**URL:** <https://community.unix.com/t/control-multiple-program-instances-open-multiple-files-problem/282287>\
**Category:** Programming\
**Created:** [February 5, 2011, 4:28pm UTC](https://community.unix.com/t/control-multiple-program-instances-open-multiple-files-problem/282287 "2011-02-05T16:28:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hakermania](https://community.unix.com/user_avatar/community.unix.com/hakermania/32/5803_2.png) [@hakermania](https://community.unix.com/u/hakermania)\
**Post date:** [February 5, 2011, 4:28pm UTC](https://community.unix.com/t/control-multiple-program-instances-open-multiple-files-problem/282287/1 "2011-02-05T16:28:39Z")

</div>

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 choose "Open With 'Default Application'" the it runs

```nohighlight
default_application path/to/the/selected/file1

```

So, if you select multiple files and select "Open With 'Default Application'" the system will call

```nohighlight
default_application path/to/the/selected/file1
default_application path/to/the/selected/file2
default_application path/to/the/selected/file3

```

So, this is a big problem for me, because I handle the concurrent processes from inside the program, so when another instance of the program is running, a warning message is appeared. So, each application's call will recognize the others as currently running applications and so it'll show the message. I'll end up with 3 Messages saying that another process of the program is running --\_--'  
My application handles multiple URLs this way:

```nohighlight
myapp path/to/the/selected/file1 path/to/the/selected/file2 path/to/the/selected/file3

```

How can I make my code handle all these multiple instances at the same time? Everything I've tried fails, because everything I've tried requires a check from the first instance called, which is too slow and other instances come app and all together are warning about concurrent processes of the same program :wall::wall::wall:

So, how can I fix this? is it system depended, or can I do something with the code?

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 6, 2011, 12:03am UTC](https://community.unix.com/t/control-multiple-program-instances-open-multiple-files-problem/282287/2 "2011-02-06T00:03:26Z")

</div>

Well, you'll need a way to communicate with the original process when an extra one is run, so it can check if it's first and if not, just send a message to the first and quit.

Message queues might work. They're convenient in having a property that their names are global across the system.

Have the process try to create a message queue with mq\_open("/username\_myapp", O\_RDONLY|O\_CREAT, 0600, NULL). If it succeeds in creating the message queue, it knows it's the first one, and sets up a thread to receive messages on it with mq\_receive() until the program quits. (Or you could use it in nonblocking mode and poll it.) Make sure to destroy the message queue with mq\_unlink when you quit, or it will continue to exist even after all your programs have quit!

If it fails to create it on the other hand, just have it open it with mq\_open("/username\_myapp", O\_WRONLY), send a message over it, and quit.

---

<div class="post-metadata">

**Author:** ![hakermania](https://community.unix.com/user_avatar/community.unix.com/hakermania/32/5803_2.png) [@hakermania](https://community.unix.com/u/hakermania)\
**Post date:** [February 6, 2011, 3:30am UTC](https://community.unix.com/t/control-multiple-program-instances-open-multiple-files-problem/282287/3 "2011-02-06T03:30:51Z")

</div>

Thanks for the suggestion. I'll have a try with DBUS messages.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [February 6, 2011, 12:06pm UTC](https://community.unix.com/t/control-multiple-program-instances-open-multiple-files-problem/282287/4 "2011-02-06T12:06:23Z")

</div>

DBUS? Well there goes any and all pretense of portability...
