Running two instances of a same C/C++ program on two different input data sets
Once an object file of a program is created does it matter if I run only one or multiple instances of it on different input data sets? Is it possible that outputs corresponding to these runs may be correlated to eac开发者_JAVA技巧h other?
With regards newbie
For a properly designed program, having multiple simultaneous instances should be fine. Each process is separate from all others, with its own address space, its own file descriptors and its own part of all other resources. As long as the output files are separate, all should work in parallel as they would on their own - the only exception possibly being the execution time.
That said, there are exceptions based on the used resources and system services. Some of them are relatively benign, since the program would fail properly with an error message if the code performs the needed status checks when allocating resources. E.g.:
Listening network ports: multiple processes cannot listen on the same protocol port on the same interface. I.e. if an instance is listening on tcp/80, no other instance can do the same.
Write-locking the same file.
Others can cause your program to fail subtly, possibly with both processes producing incorrect output or doing the wrong thing. E.g.:
Accessing the same file with each process assuming that it's the only writer for that file. This is especially common with programs that use hard coded temporary file names. Output files are also a common victim.
Searching the process table by name - having multiple instances could cause confusion with regard to which process is the requested one. This is a common pitfall in ill-designed service control programs and scripts that will happily kill the wrong server instance.
UI confusion: If you have a notification tray applet, which process does it belong to? Is it possible for two instances of the same applet to exist in the tray? Which one belongs to which process?
Message passing: Depending on the OS and the message passing method in use, you may have a mess in your hands if a program sets up listeners with hard-coded IDs.
There are about a million different ways that two processes could cause each other to fail. Each time you are using an external resource, you should take time to consider what would happen if a second program asked for the same resource. If you are lucky, you will have a nice status code to tell you about it - in other cases you may have to explicitly protect your program.
If a program cannot handle multiple instances, document this limitation thoroughly and then use locking code to prevent multiple instances from being launched.
精彩评论