LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-02-2010, 12:24 PM   #1
kralizec
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Rep: Reputation: 0
sort of execv command needed


Hi!

I have a problem, I need to launch another binary from my application, but I need to get the control back once the other binary is initialized.

fork/exec doesn't seem to give anything on that side

using pthreads I'm able to wait until the new thread exits, but that's not what I want

at the moment I'm using popen, but it doesn't seem to do the job right...

any help? what am I missing?
 
Old 07-02-2010, 05:44 PM   #2
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
Can't you just run the command in the background using the & character?

E.g.

Code:
 
system( "/mycommand.sh &");
 
Old 07-02-2010, 05:47 PM   #3
kralizec
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Original Poster
Rep: Reputation: 0
I could, but I'm looking for something at a lower level, like the exec functions family
 
Old 07-02-2010, 08:57 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
fork/exec doesn't seem to give anything on that side
After forking the parent keeps on executing, you don't have to wait for the child.
 
Old 07-03-2010, 01:44 AM   #5
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by kralizec View Post
I have a problem, I need to launch another binary from my application, but I need to get the control back once the other binary is initialized.
Sorry, but I don't totally understand your approach.

(1) Why do you need to get the control back once the new process is forked?

(2) When is the point of initialization reached in your sense? I mean, before main is called, after all the libraries are loaded?

(3) Maybe you could give some more details about what you want to really do. That could possibly help us to tell you alternatives


Quote:
Originally Posted by ntubski View Post
After forking the parent keeps on executing, you don't have to wait for the child.
I think the OP means the child, right?

Andi

Last edited by ForzaItalia2006; 07-03-2010 at 01:46 AM.
 
Old 07-03-2010, 02:20 AM   #6
kralizec
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Original Poster
Rep: Reputation: 0
yeah, I'm sorry... the fact is that I'm not so fluent in english and I can't explain the problem well.. here's an example:

Code:
int main(int argc, char *argv[]){
	
	char *child_args[] = {"/usr/bin/gcalctool", NULL};
	int cpid = fork()
	if (cpid == 0){
		execv(child_args[0],child_args);
                //perror
	} else if (cpid > 0){
		//parent stuff
	
	} else
		//perror and bla bla bla
}
In this case, I'd like to have the parent to be able to know when the calc has been launched (it's just an example) so, by initialized I think I mean when the main is called.

My parent could need stuff produced by the process called in the child, but sometimes it tries to get them while the process is still launching...


Anyway, thank you all!!

Last edited by kralizec; 07-03-2010 at 02:27 AM.
 
Old 07-03-2010, 02:44 AM   #7
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
This sounds like a scheduling issue where you want to make sure, the execv() call is finished in the child process before the parent process is scheduled again, right?

If that's what you want to do, i doubt it is possible. Of course, there are mechanisms to synchronize processes, but as you have no control over the child's code after the execv(), that doesn't apply here.

Can't you just check somehow in the parent for the things produced by the child (whatever these are) before using them, maybe waiting with some usleep() calls?
 
Old 07-03-2010, 02:48 AM   #8
kralizec
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Original Poster
Rep: Reputation: 0
Yeah! I think you got it right!

At the moment, I'm using pthread instead of the fork and doing a sleep(1), it works but it's an horrible solution by me... I'd like to make something better... thank you!
 
Old 07-03-2010, 05:00 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
well, your explanation isn't good...

if you use pthread you can use a condition variable and
pthread_cond_wait in the main thread. It will wait.
when the child thread is ready you pthread_cond_signal
and the main thread wakes.

You need to be careful using fork in threads too.


maybe you should buy a book.

Last edited by bigearsbilly; 07-03-2010 at 05:02 AM.
 
Old 07-03-2010, 05:07 AM   #10
kralizec
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bigearsbilly View Post
well, your explanation isn't good...

if you use pthread you can use a condition variable and
pthread_cond_wait in the main thread. It will wait.
when the child thread is ready you pthread_cond_signal
and the main thread wakes.

You need to be careful using fork in threads too.


maybe you should buy a book.
yeah, but how could I send the pthread_cond_signal if I still need to make an exec or sort of?

your solution works if I don't need to call an external program and just use internal functions...
 
Old 07-03-2010, 07:35 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it may be you are choosing the wrong method for your problem.
 
Old 07-03-2010, 08:24 AM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by kralizec View Post
I'd like to have the parent to be able to know when the calc has been launched (it's just an example) so, by initialized I think I mean when the main is called.
I don't think exactly that is possible. If it were possible, I don't think it is what you really want.

Quote:
My parent could need stuff produced by the process called in the child, but sometimes it tries to get them while the process is still launching.
Whether main() has started yet in the child should not be an interesting boundary. I assume whatever the child produces is not yet ready at the instant main() in the child starts.

So you need the parent to wait for the child to produce something. There are several different ways to synchronize work between a parent and child process. Some of those might be usable regardless of the separate method by which whatever the child produces reaches the parent. But it may be better to select a synchronization method specifically appropriate to the data flow:

What does the child "produce" and how does that reach the parent (In a pipe? A file? Shared memory? Or what?).
 
1 members found this post helpful.
Old 07-04-2010, 03:48 AM   #13
kralizec
LQ Newbie
 
Registered: Mar 2010
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post
I don't think exactly that is possible. If it were possible, I don't think it is what you really want.
yes, that was just an example, even if now, I would be interested in knowing how to do that too... just so

Quote:
What does the child "produce" and how does that reach the parent (In a pipe? A file? Shared memory? Or what?).
it's a file that could already exists before the child start, my problem is that I'm not the owner of the other process and I just have the exec file, not the source... thank you for your patience and help
 
Old 07-04-2010, 09:44 AM   #14
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
First, some nitpicking

Quote:
Originally Posted by kralizec View Post
it's a file that could already exists before the child start, my problem is that I'm not the owner of the other process and I just have the exec file, not the source... thank you for your patience and help
Sure you are the owner of the PROCESS, you started it

But then, if it's some file change you're waiting for, have a look at inotify. Never tried it myself, but it should be the solution to your synchronization problem.
 
Old 07-04-2010, 10:13 AM   #15
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by zirias View Post
Sure you are the owner of the PROCESS, you started it
I expect kralizec meant "owner" in the software development sense, meaning kralizec does not have the ability/right to modify the source code of the executable that will be run in that child process.

Quote:
But then, if it's some file change you're waiting for, have a look at inotify. Never tried it myself, but it should be the solution to your synchronization problem.
That looks like it could be a very elegant solution (I didn't read it carefully enough to be sure). But the crude solution of just stat'ing the file multiple times until it changes might be simpler.

I don't seem to have the right dev package installed for man 2 stat to work for me (I ought to fix that). But man 2 stat should give you basic doc on the function(s) you need for the crude approach.

Last edited by johnsfine; 07-04-2010 at 10:28 AM.
 
  


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
[SOLVED] sort command help anu_1 Linux - Newbie 4 01-29-2010 04:34 AM
sort command fallloveuni Programming 3 01-24-2010 08:31 PM
Sort command arya6000 Linux - Newbie 2 11-27-2007 07:50 PM
Sort Command saravanan1979 Programming 1 10-03-2004 11:36 AM
The SORT command Rezon Programming 2 10-30-2003 04:14 PM

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

All times are GMT -5. The time now is 02:35 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