LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2023, 07:49 AM   #1
Anim11111
LQ Newbie
 
Registered: Nov 2023
Posts: 2

Rep: Reputation: 0
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
 
Old 11-02-2023, 08:08 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,031

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
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.
 
Old 11-02-2023, 08:30 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
You might try `system("nohup program1.exe </dev/null >/dev/null 2>/dev/null &")`

Note: system("pr2") returning 512 might mean errcode=2 from pr2

Last edited by NevemTeve; 11-02-2023 at 08:36 AM.
 
Old 11-02-2023, 05:52 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,688
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 11-03-2023, 12:37 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
@OP: Nonetheless you are allowed to produce a little example program that helps us reproduce the problem.

Last edited by NevemTeve; 11-03-2023 at 12:38 AM.
 
Old 11-03-2023, 05:46 AM   #6
Anim11111
LQ Newbie
 
Registered: Nov 2023
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 11-03-2023, 07:17 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
I hope that you understand what you're saying.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tell when a program tries to run another program ultagod Linux - General 2 11-11-2011 06:55 PM
LXer: aTunes tries to be the best of two worlds LXer Syndicated Linux News 0 05-13-2008 06:10 AM
SUSE 9.3: Firefox 1.0.7 tries to open two windows brianbahr SUSE / openSUSE 4 02-25-2006 05:07 AM
Error after editing /etc/rc/rc.local, Fedora freezes when tries to run the script simeandrews Fedora 1 06-24-2005 10:52 AM
How do change program Mozilla download manager tries to open downloading files with? jdruin Linux - Software 1 04-18-2004 06:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration