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;








0















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?










share|improve this question






























    0















    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?










    share|improve this question


























      0












      0








      0








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 17:24









      Simon Doppler

      721217




      721217










      asked Mar 22 at 15:55









      LabyrinthianLabyrinthian

      2716




      2716






















          1 Answer
          1






          active

          oldest

          votes


















          1














          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 :)






          share|improve this answer























          • 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 be volatile. See stackoverflow.com/questions/246127/…

            – Andrew Henle
            Mar 22 at 17:29











          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%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









          1














          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 :)






          share|improve this answer























          • 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 be volatile. See stackoverflow.com/questions/246127/…

            – Andrew Henle
            Mar 22 at 17:29















          1














          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 :)






          share|improve this answer























          • 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 be volatile. See stackoverflow.com/questions/246127/…

            – Andrew Henle
            Mar 22 at 17:29













          1












          1








          1







          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 :)






          share|improve this answer













          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 :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 16:31









          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 be volatile. 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






          • 1





            signal_received needs to be volatile. 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



















          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%2f55303462%2fpassing-a-value-to-a-childs-process-handler%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript