Passing a value to a child's process handler Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Why is volatile needed in C?Program run in child process doesn't loopDifference between the address space of parent process and its child process in Linux?process termination doesn't affect waitpid()Problems with signal handlingKilling a child process from a signal handlerUnix and Signal Handlers (C)fork and signal: how to send signals from parent process to specific child processSignal Handler BehaviorWhy is SIGTSTP not unblocking in child?Using signals in a child process
My admission is revoked after accepting the admission offer
A strange hotel
What is /etc/mtab in Linux?
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
Co-worker works way more than he should
What is the ongoing value of the Kanban board to the developers as opposed to management
Multiple options vs single option UI
Can I criticise the more senior developers around me for not writing clean code?
"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"
Implementing 3DES algorithm in Java: is my code secure?
What is a 'Key' in computer science?
What if Force was not Mass times Acceleration?
Function to calculate red-edgeNDVI in Google Earth Engine
Passing args from the bash script to the function in the script
How to not starve gigantic beasts
Are these square matrices always diagonalisable?
All ASCII characters with a given bit count
Could Neutrino technically as side-effect, incentivize centralization of the bitcoin network?
What was Apollo 13's "Little Jolt" after MECO?
With indentation set to `0em`, when using a line break, there is still an indentation of a size of a space
Putting Ant-Man on house arrest
How to keep bees out of canned beverages?
Married in secret, can marital status in passport be changed at a later date?
Israeli soda type drink
Passing a value to a child's process handler
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Why is volatile needed in C?Program run in child process doesn't loopDifference between the address space of parent process and its child process in Linux?process termination doesn't affect waitpid()Problems with signal handlingKilling a child process from a signal handlerUnix and Signal Handlers (C)fork and signal: how to send signals from parent process to specific child processSignal Handler BehaviorWhy is SIGTSTP not unblocking in child?Using signals in a child process
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Let's say i create via fork a child process from a father process and i pass an X value to child process using a pipe.At first the child is on pause and i start it using a SIGINT signal.What i want to do is pass the value X to the signal handler used in pipe.Also,the value of i will change during the running of father process and i will have to pass it multiple times so i don't think making it global would work.Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
void handler()
//i would like to print the value of i here.
int main()
int fd[2], s;
pipe(fd);
pid_t c = fork();
if (c == 0)
signal(SIGINT, handler);
pause();
read(fd[0], &s, sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if (c > 0)
int i = 2;
sleep(1); //i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c, SIGINT);
write(fd[1], &i, sizeof(i));
How could this be done?
c operating-system
add a comment |
Let's say i create via fork a child process from a father process and i pass an X value to child process using a pipe.At first the child is on pause and i start it using a SIGINT signal.What i want to do is pass the value X to the signal handler used in pipe.Also,the value of i will change during the running of father process and i will have to pass it multiple times so i don't think making it global would work.Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
void handler()
//i would like to print the value of i here.
int main()
int fd[2], s;
pipe(fd);
pid_t c = fork();
if (c == 0)
signal(SIGINT, handler);
pause();
read(fd[0], &s, sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if (c > 0)
int i = 2;
sleep(1); //i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c, SIGINT);
write(fd[1], &i, sizeof(i));
How could this be done?
c operating-system
add a comment |
Let's say i create via fork a child process from a father process and i pass an X value to child process using a pipe.At first the child is on pause and i start it using a SIGINT signal.What i want to do is pass the value X to the signal handler used in pipe.Also,the value of i will change during the running of father process and i will have to pass it multiple times so i don't think making it global would work.Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
void handler()
//i would like to print the value of i here.
int main()
int fd[2], s;
pipe(fd);
pid_t c = fork();
if (c == 0)
signal(SIGINT, handler);
pause();
read(fd[0], &s, sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if (c > 0)
int i = 2;
sleep(1); //i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c, SIGINT);
write(fd[1], &i, sizeof(i));
How could this be done?
c operating-system
Let's say i create via fork a child process from a father process and i pass an X value to child process using a pipe.At first the child is on pause and i start it using a SIGINT signal.What i want to do is pass the value X to the signal handler used in pipe.Also,the value of i will change during the running of father process and i will have to pass it multiple times so i don't think making it global would work.Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
void handler()
//i would like to print the value of i here.
int main()
int fd[2], s;
pipe(fd);
pid_t c = fork();
if (c == 0)
signal(SIGINT, handler);
pause();
read(fd[0], &s, sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if (c > 0)
int i = 2;
sleep(1); //i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c, SIGINT);
write(fd[1], &i, sizeof(i));
How could this be done?
c operating-system
c operating-system
edited Mar 22 at 17:24
Simon Doppler
721217
721217
asked Mar 22 at 15:55
![](https://lh3.googleusercontent.com/-szorfCjco-U/AAAAAAAAAAI/AAAAAAAAAIc/pa4YqRkaD9Y/photo.jpg?sz=32)
![](https://lh3.googleusercontent.com/-szorfCjco-U/AAAAAAAAAAI/AAAAAAAAAIc/pa4YqRkaD9Y/photo.jpg?sz=32)
LabyrinthianLabyrinthian
2716
2716
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Add a global variable with value 0 at the start of the child. Put a while loop in the child. When the signal comes turn it to 1. In the while loop test for the value of the global variable if it turns to one, if so read and print and turn back the variable to 0.
Example
#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/wait.h>
int signal_received = 0;
void handler()
signal_received = 1;
int main()
int fd[2],s;
pipe(fd);
pid_t c=fork();
if(c==0)
signal(SIGINT,handler);
while (1)
if (signal_received)
read(fd[0],&s,sizeof(s));
printf("%d",s);
signal_received = 0;
pause();
read(fd[0],&s,sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if(c>0)
int i=2;
sleep(1);//i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c,SIGINT);
write(fd[1],&i,sizeof(i));
Of course, this is a stub to what you want, there is more.
You can add concurrent protection on the global variable.
If you have a more complex system, then a messaging queue would be more appropriate.
Hope it helps :)
Thank you for your answer,it solves my problem.Although i would like to print s inside the handler if possible .Is there a way to pass it from the child to the function?
– Labyrinthian
Mar 22 at 16:47
1
signal_received
needs to bevolatile
. See stackoverflow.com/questions/246127/…
– Andrew Henle
Mar 22 at 17:29
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303462%2fpassing-a-value-to-a-childs-process-handler%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add a global variable with value 0 at the start of the child. Put a while loop in the child. When the signal comes turn it to 1. In the while loop test for the value of the global variable if it turns to one, if so read and print and turn back the variable to 0.
Example
#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/wait.h>
int signal_received = 0;
void handler()
signal_received = 1;
int main()
int fd[2],s;
pipe(fd);
pid_t c=fork();
if(c==0)
signal(SIGINT,handler);
while (1)
if (signal_received)
read(fd[0],&s,sizeof(s));
printf("%d",s);
signal_received = 0;
pause();
read(fd[0],&s,sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if(c>0)
int i=2;
sleep(1);//i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c,SIGINT);
write(fd[1],&i,sizeof(i));
Of course, this is a stub to what you want, there is more.
You can add concurrent protection on the global variable.
If you have a more complex system, then a messaging queue would be more appropriate.
Hope it helps :)
Thank you for your answer,it solves my problem.Although i would like to print s inside the handler if possible .Is there a way to pass it from the child to the function?
– Labyrinthian
Mar 22 at 16:47
1
signal_received
needs to bevolatile
. See stackoverflow.com/questions/246127/…
– Andrew Henle
Mar 22 at 17:29
add a comment |
Add a global variable with value 0 at the start of the child. Put a while loop in the child. When the signal comes turn it to 1. In the while loop test for the value of the global variable if it turns to one, if so read and print and turn back the variable to 0.
Example
#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/wait.h>
int signal_received = 0;
void handler()
signal_received = 1;
int main()
int fd[2],s;
pipe(fd);
pid_t c=fork();
if(c==0)
signal(SIGINT,handler);
while (1)
if (signal_received)
read(fd[0],&s,sizeof(s));
printf("%d",s);
signal_received = 0;
pause();
read(fd[0],&s,sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if(c>0)
int i=2;
sleep(1);//i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c,SIGINT);
write(fd[1],&i,sizeof(i));
Of course, this is a stub to what you want, there is more.
You can add concurrent protection on the global variable.
If you have a more complex system, then a messaging queue would be more appropriate.
Hope it helps :)
Thank you for your answer,it solves my problem.Although i would like to print s inside the handler if possible .Is there a way to pass it from the child to the function?
– Labyrinthian
Mar 22 at 16:47
1
signal_received
needs to bevolatile
. See stackoverflow.com/questions/246127/…
– Andrew Henle
Mar 22 at 17:29
add a comment |
Add a global variable with value 0 at the start of the child. Put a while loop in the child. When the signal comes turn it to 1. In the while loop test for the value of the global variable if it turns to one, if so read and print and turn back the variable to 0.
Example
#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/wait.h>
int signal_received = 0;
void handler()
signal_received = 1;
int main()
int fd[2],s;
pipe(fd);
pid_t c=fork();
if(c==0)
signal(SIGINT,handler);
while (1)
if (signal_received)
read(fd[0],&s,sizeof(s));
printf("%d",s);
signal_received = 0;
pause();
read(fd[0],&s,sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if(c>0)
int i=2;
sleep(1);//i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c,SIGINT);
write(fd[1],&i,sizeof(i));
Of course, this is a stub to what you want, there is more.
You can add concurrent protection on the global variable.
If you have a more complex system, then a messaging queue would be more appropriate.
Hope it helps :)
Add a global variable with value 0 at the start of the child. Put a while loop in the child. When the signal comes turn it to 1. In the while loop test for the value of the global variable if it turns to one, if so read and print and turn back the variable to 0.
Example
#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/wait.h>
int signal_received = 0;
void handler()
signal_received = 1;
int main()
int fd[2],s;
pipe(fd);
pid_t c=fork();
if(c==0)
signal(SIGINT,handler);
while (1)
if (signal_received)
read(fd[0],&s,sizeof(s));
printf("%d",s);
signal_received = 0;
pause();
read(fd[0],&s,sizeof(s));
//if use printf("%d",s) here s=2 correctly.
if(c>0)
int i=2;
sleep(1);//i don't want the SIGINT signal to terminate the child process so i wait for it to reach pause
kill(c,SIGINT);
write(fd[1],&i,sizeof(i));
Of course, this is a stub to what you want, there is more.
You can add concurrent protection on the global variable.
If you have a more complex system, then a messaging queue would be more appropriate.
Hope it helps :)
answered Mar 22 at 16:31
![](https://lh4.googleusercontent.com/-zJ8ZQkUCOkY/AAAAAAAAAAI/AAAAAAAAAAA/ACevoQPi5r4NvVpJ2GSVZ8oJRDY9NmfU8w/mo/photo.jpg?sz=32)
![](https://lh4.googleusercontent.com/-zJ8ZQkUCOkY/AAAAAAAAAAI/AAAAAAAAAAA/ACevoQPi5r4NvVpJ2GSVZ8oJRDY9NmfU8w/mo/photo.jpg?sz=32)
Meher KhiariMeher Khiari
1048
1048
Thank you for your answer,it solves my problem.Although i would like to print s inside the handler if possible .Is there a way to pass it from the child to the function?
– Labyrinthian
Mar 22 at 16:47
1
signal_received
needs to bevolatile
. See stackoverflow.com/questions/246127/…
– Andrew Henle
Mar 22 at 17:29
add a comment |
Thank you for your answer,it solves my problem.Although i would like to print s inside the handler if possible .Is there a way to pass it from the child to the function?
– Labyrinthian
Mar 22 at 16:47
1
signal_received
needs to bevolatile
. See stackoverflow.com/questions/246127/…
– Andrew Henle
Mar 22 at 17:29
Thank you for your answer,it solves my problem.Although i would like to print s inside the handler if possible .Is there a way to pass it from the child to the function?
– Labyrinthian
Mar 22 at 16:47
Thank you for your answer,it solves my problem.Although i would like to print s inside the handler if possible .Is there a way to pass it from the child to the function?
– Labyrinthian
Mar 22 at 16:47
1
1
signal_received
needs to be volatile
. See stackoverflow.com/questions/246127/…– Andrew Henle
Mar 22 at 17:29
signal_received
needs to be volatile
. See stackoverflow.com/questions/246127/…– Andrew Henle
Mar 22 at 17:29
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55303462%2fpassing-a-value-to-a-childs-process-handler%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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