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.
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...
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?
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!
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.
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...
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?).
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.