How do I wait inside a child process until the parent process reached a certain point?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















PSEUDO-CODE:



Child:
...
wait until parent prints "blabla"
...

Parent:
...
print "blabla"
...


I can't use wait, because that would be the other way around and furthermore, I want to continue the parent process after the print "blabla" parallel to the child process.
I could imagine to share with mmap a pthread_mutex_t variable and solve the issue with mutual exclusion, but I am not sure about that and wanted to ask if there might exists a very easy solution.










share|improve this question




















  • 2





    As you suspect, you need to use some form of interprocess communication. Injecting a message into the child's standard input is often one of the simpler ones to set up and fairly portable. But your question is really both too broad and too system dependent.

    – Chris Stratton
    Jan 3 at 5:03













  • @ChrisStratton okay, I am going to overthink it, thank you. Gonna get some sleep now ^^'

    – Lavair
    Jan 3 at 5:27











  • Depends on how you created the child. If the child was created with pthread_create, then all you need is a pthread_mutex_t. There's no need for mmap.

    – user3386109
    Jan 3 at 5:42






  • 2





    If it was created with pthread_create() it's not a child process but rather a thread.

    – Chris Stratton
    Jan 3 at 5:47




















1















PSEUDO-CODE:



Child:
...
wait until parent prints "blabla"
...

Parent:
...
print "blabla"
...


I can't use wait, because that would be the other way around and furthermore, I want to continue the parent process after the print "blabla" parallel to the child process.
I could imagine to share with mmap a pthread_mutex_t variable and solve the issue with mutual exclusion, but I am not sure about that and wanted to ask if there might exists a very easy solution.










share|improve this question




















  • 2





    As you suspect, you need to use some form of interprocess communication. Injecting a message into the child's standard input is often one of the simpler ones to set up and fairly portable. But your question is really both too broad and too system dependent.

    – Chris Stratton
    Jan 3 at 5:03













  • @ChrisStratton okay, I am going to overthink it, thank you. Gonna get some sleep now ^^'

    – Lavair
    Jan 3 at 5:27











  • Depends on how you created the child. If the child was created with pthread_create, then all you need is a pthread_mutex_t. There's no need for mmap.

    – user3386109
    Jan 3 at 5:42






  • 2





    If it was created with pthread_create() it's not a child process but rather a thread.

    – Chris Stratton
    Jan 3 at 5:47
















1












1








1








PSEUDO-CODE:



Child:
...
wait until parent prints "blabla"
...

Parent:
...
print "blabla"
...


I can't use wait, because that would be the other way around and furthermore, I want to continue the parent process after the print "blabla" parallel to the child process.
I could imagine to share with mmap a pthread_mutex_t variable and solve the issue with mutual exclusion, but I am not sure about that and wanted to ask if there might exists a very easy solution.










share|improve this question
















PSEUDO-CODE:



Child:
...
wait until parent prints "blabla"
...

Parent:
...
print "blabla"
...


I can't use wait, because that would be the other way around and furthermore, I want to continue the parent process after the print "blabla" parallel to the child process.
I could imagine to share with mmap a pthread_mutex_t variable and solve the issue with mutual exclusion, but I am not sure about that and wanted to ask if there might exists a very easy solution.







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:01







Lavair

















asked Jan 3 at 4:55









LavairLavair

1446




1446








  • 2





    As you suspect, you need to use some form of interprocess communication. Injecting a message into the child's standard input is often one of the simpler ones to set up and fairly portable. But your question is really both too broad and too system dependent.

    – Chris Stratton
    Jan 3 at 5:03













  • @ChrisStratton okay, I am going to overthink it, thank you. Gonna get some sleep now ^^'

    – Lavair
    Jan 3 at 5:27











  • Depends on how you created the child. If the child was created with pthread_create, then all you need is a pthread_mutex_t. There's no need for mmap.

    – user3386109
    Jan 3 at 5:42






  • 2





    If it was created with pthread_create() it's not a child process but rather a thread.

    – Chris Stratton
    Jan 3 at 5:47
















  • 2





    As you suspect, you need to use some form of interprocess communication. Injecting a message into the child's standard input is often one of the simpler ones to set up and fairly portable. But your question is really both too broad and too system dependent.

    – Chris Stratton
    Jan 3 at 5:03













  • @ChrisStratton okay, I am going to overthink it, thank you. Gonna get some sleep now ^^'

    – Lavair
    Jan 3 at 5:27











  • Depends on how you created the child. If the child was created with pthread_create, then all you need is a pthread_mutex_t. There's no need for mmap.

    – user3386109
    Jan 3 at 5:42






  • 2





    If it was created with pthread_create() it's not a child process but rather a thread.

    – Chris Stratton
    Jan 3 at 5:47










2




2





As you suspect, you need to use some form of interprocess communication. Injecting a message into the child's standard input is often one of the simpler ones to set up and fairly portable. But your question is really both too broad and too system dependent.

– Chris Stratton
Jan 3 at 5:03







As you suspect, you need to use some form of interprocess communication. Injecting a message into the child's standard input is often one of the simpler ones to set up and fairly portable. But your question is really both too broad and too system dependent.

– Chris Stratton
Jan 3 at 5:03















@ChrisStratton okay, I am going to overthink it, thank you. Gonna get some sleep now ^^'

– Lavair
Jan 3 at 5:27





@ChrisStratton okay, I am going to overthink it, thank you. Gonna get some sleep now ^^'

– Lavair
Jan 3 at 5:27













Depends on how you created the child. If the child was created with pthread_create, then all you need is a pthread_mutex_t. There's no need for mmap.

– user3386109
Jan 3 at 5:42





Depends on how you created the child. If the child was created with pthread_create, then all you need is a pthread_mutex_t. There's no need for mmap.

– user3386109
Jan 3 at 5:42




2




2





If it was created with pthread_create() it's not a child process but rather a thread.

– Chris Stratton
Jan 3 at 5:47







If it was created with pthread_create() it's not a child process but rather a thread.

– Chris Stratton
Jan 3 at 5:47














2 Answers
2






active

oldest

votes


















2














You can use Signals for this,
After completing a certain task parent process should send a signal to child process by using Kill system call and by updating the signal handler of the signal child process can do further task.



Child:
signal_handler()
{
...
}
main()
{
signal(SIGINT, signal_handler);
while(1);//wait until parent prints "blabla"
}



Parent:
...
print "blabla"
kill(pid_of_child,signal)
...





share|improve this answer


























  • That's possible but it's probably not the best way. Generally a signal would be used for something abnormal.

    – Chris Stratton
    Jan 3 at 5:46











  • @ChrisStratton while true that that could be not the best way, it is simple and reasonable. Signals are often used for things that are not at all abnormal, see for example SIGWINCH en.wikipedia.org/wiki/Signal_(IPC)#SIGWINCH

    – linuxfan
    Jan 3 at 6:11






  • 1





    This is going to be quite challenging to get right. If the parent sends the signal before the child is ready for it, the signal may get lost or cause mischief for the child. To do this reliably, the parent would have to wait for a signal from the child that indicates that the child is ready to accept the signal from the parent, otherwise race conditions and unpredictable behavior could result. For this reason, a pipe or socketpair is probably a better choice.

    – David Schwartz
    Jan 3 at 6:21





















0














Have two global variables,one for mutex lock and another for a signal variable like this.



pthread_cond_t signal = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_COND_INITIALIZER;


In your parent thread when you print blabla signal the child thread :



pthread_mutex_lock(&cond_mutex);
pthread_cond_signal(&signal);
pthread_mutex_unlock(&cond_mutex);


In your child thread wait for the signal :



struct timespec time_to_wait = {0, 0};
time_to_wait.tv_sec = time(NULL) + 10;//10 secs
pthread_mutex_lock(&cond_mutex);
status = pthread_cond_timedwait(&signal,&cond_mutex, &time_to_wait);
pthread_mutex_unlock(&cond_mutex);


You can extend this code to handle repeatedly wait for the condition to occur for every 't' seconds within a loop, or simply wait once for 't' seconds and quit.
Refer this doc for the return values.






share|improve this answer



















  • 2





    Threads are all peers and don't have parent/child relationships. In any event, this question wasn't about threads at all anyway -- it was about processes (which do have parent/child relationships).

    – David Schwartz
    Jan 3 at 6:15












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016556%2fhow-do-i-wait-inside-a-child-process-until-the-parent-process-reached-a-certain%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use Signals for this,
After completing a certain task parent process should send a signal to child process by using Kill system call and by updating the signal handler of the signal child process can do further task.



Child:
signal_handler()
{
...
}
main()
{
signal(SIGINT, signal_handler);
while(1);//wait until parent prints "blabla"
}



Parent:
...
print "blabla"
kill(pid_of_child,signal)
...





share|improve this answer


























  • That's possible but it's probably not the best way. Generally a signal would be used for something abnormal.

    – Chris Stratton
    Jan 3 at 5:46











  • @ChrisStratton while true that that could be not the best way, it is simple and reasonable. Signals are often used for things that are not at all abnormal, see for example SIGWINCH en.wikipedia.org/wiki/Signal_(IPC)#SIGWINCH

    – linuxfan
    Jan 3 at 6:11






  • 1





    This is going to be quite challenging to get right. If the parent sends the signal before the child is ready for it, the signal may get lost or cause mischief for the child. To do this reliably, the parent would have to wait for a signal from the child that indicates that the child is ready to accept the signal from the parent, otherwise race conditions and unpredictable behavior could result. For this reason, a pipe or socketpair is probably a better choice.

    – David Schwartz
    Jan 3 at 6:21


















2














You can use Signals for this,
After completing a certain task parent process should send a signal to child process by using Kill system call and by updating the signal handler of the signal child process can do further task.



Child:
signal_handler()
{
...
}
main()
{
signal(SIGINT, signal_handler);
while(1);//wait until parent prints "blabla"
}



Parent:
...
print "blabla"
kill(pid_of_child,signal)
...





share|improve this answer


























  • That's possible but it's probably not the best way. Generally a signal would be used for something abnormal.

    – Chris Stratton
    Jan 3 at 5:46











  • @ChrisStratton while true that that could be not the best way, it is simple and reasonable. Signals are often used for things that are not at all abnormal, see for example SIGWINCH en.wikipedia.org/wiki/Signal_(IPC)#SIGWINCH

    – linuxfan
    Jan 3 at 6:11






  • 1





    This is going to be quite challenging to get right. If the parent sends the signal before the child is ready for it, the signal may get lost or cause mischief for the child. To do this reliably, the parent would have to wait for a signal from the child that indicates that the child is ready to accept the signal from the parent, otherwise race conditions and unpredictable behavior could result. For this reason, a pipe or socketpair is probably a better choice.

    – David Schwartz
    Jan 3 at 6:21
















2












2








2







You can use Signals for this,
After completing a certain task parent process should send a signal to child process by using Kill system call and by updating the signal handler of the signal child process can do further task.



Child:
signal_handler()
{
...
}
main()
{
signal(SIGINT, signal_handler);
while(1);//wait until parent prints "blabla"
}



Parent:
...
print "blabla"
kill(pid_of_child,signal)
...





share|improve this answer















You can use Signals for this,
After completing a certain task parent process should send a signal to child process by using Kill system call and by updating the signal handler of the signal child process can do further task.



Child:
signal_handler()
{
...
}
main()
{
signal(SIGINT, signal_handler);
while(1);//wait until parent prints "blabla"
}



Parent:
...
print "blabla"
kill(pid_of_child,signal)
...






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 5:40

























answered Jan 3 at 5:30









Ganesh_ShindeGanesh_Shinde

483




483













  • That's possible but it's probably not the best way. Generally a signal would be used for something abnormal.

    – Chris Stratton
    Jan 3 at 5:46











  • @ChrisStratton while true that that could be not the best way, it is simple and reasonable. Signals are often used for things that are not at all abnormal, see for example SIGWINCH en.wikipedia.org/wiki/Signal_(IPC)#SIGWINCH

    – linuxfan
    Jan 3 at 6:11






  • 1





    This is going to be quite challenging to get right. If the parent sends the signal before the child is ready for it, the signal may get lost or cause mischief for the child. To do this reliably, the parent would have to wait for a signal from the child that indicates that the child is ready to accept the signal from the parent, otherwise race conditions and unpredictable behavior could result. For this reason, a pipe or socketpair is probably a better choice.

    – David Schwartz
    Jan 3 at 6:21





















  • That's possible but it's probably not the best way. Generally a signal would be used for something abnormal.

    – Chris Stratton
    Jan 3 at 5:46











  • @ChrisStratton while true that that could be not the best way, it is simple and reasonable. Signals are often used for things that are not at all abnormal, see for example SIGWINCH en.wikipedia.org/wiki/Signal_(IPC)#SIGWINCH

    – linuxfan
    Jan 3 at 6:11






  • 1





    This is going to be quite challenging to get right. If the parent sends the signal before the child is ready for it, the signal may get lost or cause mischief for the child. To do this reliably, the parent would have to wait for a signal from the child that indicates that the child is ready to accept the signal from the parent, otherwise race conditions and unpredictable behavior could result. For this reason, a pipe or socketpair is probably a better choice.

    – David Schwartz
    Jan 3 at 6:21



















That's possible but it's probably not the best way. Generally a signal would be used for something abnormal.

– Chris Stratton
Jan 3 at 5:46





That's possible but it's probably not the best way. Generally a signal would be used for something abnormal.

– Chris Stratton
Jan 3 at 5:46













@ChrisStratton while true that that could be not the best way, it is simple and reasonable. Signals are often used for things that are not at all abnormal, see for example SIGWINCH en.wikipedia.org/wiki/Signal_(IPC)#SIGWINCH

– linuxfan
Jan 3 at 6:11





@ChrisStratton while true that that could be not the best way, it is simple and reasonable. Signals are often used for things that are not at all abnormal, see for example SIGWINCH en.wikipedia.org/wiki/Signal_(IPC)#SIGWINCH

– linuxfan
Jan 3 at 6:11




1




1





This is going to be quite challenging to get right. If the parent sends the signal before the child is ready for it, the signal may get lost or cause mischief for the child. To do this reliably, the parent would have to wait for a signal from the child that indicates that the child is ready to accept the signal from the parent, otherwise race conditions and unpredictable behavior could result. For this reason, a pipe or socketpair is probably a better choice.

– David Schwartz
Jan 3 at 6:21







This is going to be quite challenging to get right. If the parent sends the signal before the child is ready for it, the signal may get lost or cause mischief for the child. To do this reliably, the parent would have to wait for a signal from the child that indicates that the child is ready to accept the signal from the parent, otherwise race conditions and unpredictable behavior could result. For this reason, a pipe or socketpair is probably a better choice.

– David Schwartz
Jan 3 at 6:21















0














Have two global variables,one for mutex lock and another for a signal variable like this.



pthread_cond_t signal = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_COND_INITIALIZER;


In your parent thread when you print blabla signal the child thread :



pthread_mutex_lock(&cond_mutex);
pthread_cond_signal(&signal);
pthread_mutex_unlock(&cond_mutex);


In your child thread wait for the signal :



struct timespec time_to_wait = {0, 0};
time_to_wait.tv_sec = time(NULL) + 10;//10 secs
pthread_mutex_lock(&cond_mutex);
status = pthread_cond_timedwait(&signal,&cond_mutex, &time_to_wait);
pthread_mutex_unlock(&cond_mutex);


You can extend this code to handle repeatedly wait for the condition to occur for every 't' seconds within a loop, or simply wait once for 't' seconds and quit.
Refer this doc for the return values.






share|improve this answer



















  • 2





    Threads are all peers and don't have parent/child relationships. In any event, this question wasn't about threads at all anyway -- it was about processes (which do have parent/child relationships).

    – David Schwartz
    Jan 3 at 6:15
















0














Have two global variables,one for mutex lock and another for a signal variable like this.



pthread_cond_t signal = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_COND_INITIALIZER;


In your parent thread when you print blabla signal the child thread :



pthread_mutex_lock(&cond_mutex);
pthread_cond_signal(&signal);
pthread_mutex_unlock(&cond_mutex);


In your child thread wait for the signal :



struct timespec time_to_wait = {0, 0};
time_to_wait.tv_sec = time(NULL) + 10;//10 secs
pthread_mutex_lock(&cond_mutex);
status = pthread_cond_timedwait(&signal,&cond_mutex, &time_to_wait);
pthread_mutex_unlock(&cond_mutex);


You can extend this code to handle repeatedly wait for the condition to occur for every 't' seconds within a loop, or simply wait once for 't' seconds and quit.
Refer this doc for the return values.






share|improve this answer



















  • 2





    Threads are all peers and don't have parent/child relationships. In any event, this question wasn't about threads at all anyway -- it was about processes (which do have parent/child relationships).

    – David Schwartz
    Jan 3 at 6:15














0












0








0







Have two global variables,one for mutex lock and another for a signal variable like this.



pthread_cond_t signal = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_COND_INITIALIZER;


In your parent thread when you print blabla signal the child thread :



pthread_mutex_lock(&cond_mutex);
pthread_cond_signal(&signal);
pthread_mutex_unlock(&cond_mutex);


In your child thread wait for the signal :



struct timespec time_to_wait = {0, 0};
time_to_wait.tv_sec = time(NULL) + 10;//10 secs
pthread_mutex_lock(&cond_mutex);
status = pthread_cond_timedwait(&signal,&cond_mutex, &time_to_wait);
pthread_mutex_unlock(&cond_mutex);


You can extend this code to handle repeatedly wait for the condition to occur for every 't' seconds within a loop, or simply wait once for 't' seconds and quit.
Refer this doc for the return values.






share|improve this answer













Have two global variables,one for mutex lock and another for a signal variable like this.



pthread_cond_t signal = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_COND_INITIALIZER;


In your parent thread when you print blabla signal the child thread :



pthread_mutex_lock(&cond_mutex);
pthread_cond_signal(&signal);
pthread_mutex_unlock(&cond_mutex);


In your child thread wait for the signal :



struct timespec time_to_wait = {0, 0};
time_to_wait.tv_sec = time(NULL) + 10;//10 secs
pthread_mutex_lock(&cond_mutex);
status = pthread_cond_timedwait(&signal,&cond_mutex, &time_to_wait);
pthread_mutex_unlock(&cond_mutex);


You can extend this code to handle repeatedly wait for the condition to occur for every 't' seconds within a loop, or simply wait once for 't' seconds and quit.
Refer this doc for the return values.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 5:52









vinoth hvinoth h

371110




371110








  • 2





    Threads are all peers and don't have parent/child relationships. In any event, this question wasn't about threads at all anyway -- it was about processes (which do have parent/child relationships).

    – David Schwartz
    Jan 3 at 6:15














  • 2





    Threads are all peers and don't have parent/child relationships. In any event, this question wasn't about threads at all anyway -- it was about processes (which do have parent/child relationships).

    – David Schwartz
    Jan 3 at 6:15








2




2





Threads are all peers and don't have parent/child relationships. In any event, this question wasn't about threads at all anyway -- it was about processes (which do have parent/child relationships).

– David Schwartz
Jan 3 at 6:15





Threads are all peers and don't have parent/child relationships. In any event, this question wasn't about threads at all anyway -- it was about processes (which do have parent/child relationships).

– David Schwartz
Jan 3 at 6:15


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016556%2fhow-do-i-wait-inside-a-child-process-until-the-parent-process-reached-a-certain%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory