[SOLVED] Problem when a program tries to run two external programs using system()
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Problem when a program tries to run two external programs using system()
I want to ask what is the cause of the following problem. In our C program we can find that the following process isn't working well. We run this program on Linux
We have a program that executes two external programs such pr1.exe and pr2.exe (once these programs have started, they will run continuously) , this program execute the following steps:
1. Create a thread to run pr1.exe with the system() instruction
2. Create a thread to run pr2.exe with the system() instruction
But when we try to execute pr2.exe, we can see that the system instruction return the code 512 and the value of WEXITSTATUS is 2 and if I execute lsof instruction a can see that:
pr2 is associated with pr1
like that:
1509 /tmp/pr1.exe /tmp/pr2.exe
Only if we kill the pr1.exe process, pr2.exe starts running and then pr1.exe restarts (because the program is designed to restart a program if it cannot run)
Another solution is to put a wait time between processes. like that:
1. Create a thread to run pr1.exe with the system() instruction
2. wait(time)
3. Create a thread to run pr2.exe with the system() instruction
The same problem is reproduced if we change the order of execution of the programs. In this case pr2.exe is executed and pr1.exe is not executed because it is associated with pr2.exe.
If we change system() to a branch and some execl statements, we have the same problem
would be nice to see the actual code?
system() waits for the end of the execution, so if you want to start processes you need to do it differently. But it also may depend on the executables themselves.
It is my understanding that the system() call operates at a process level. So, it will block the entire process including all of its threads. I do not believe that this approach to running a child-process is intended to be thread-friendly. You will need to select another approach.
The problem with the system in this case was that the main thread had immediately launched the first thread where the first program was executed, decompressed the second program, created the exe using the fopen instruction, but what happened was that the first program It remained in possession of this second, and when launching the second thread so that the second program would be executed in parallel, the system told us that it could not execute it since it was in possession of the first.
The solutions have been to set a small waiting time before creating the exe and in the event that this time is not enough, then as the main thread knows if a program is running or not what it will do is delete the exe of the program that it doesn't run, unzip it again and run it again
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.