Share number of child process to parent process. Exit() & Wait() or global variable












0














I have the task that the parent process needs to output exit code of the child process. This exit code is supposed to be the sum of the child process id, with an added variable k and modulo 100 of the whole. I have tried two approaches to save the exit-code from the child process:




  • exit(exit-code) in child process and saving in parent process with wait(). You should still this in the comments

  • saving exit-code in global variable and outputting exit-code after wait() in parent process


However, both did not work. Could you help me how I could achieve it? Thank you!



#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

//globale Variable
int out;

int main()
{
//Nutzereingabe von k:
int k=0;
scanf("%d",&k);
//Erzeugen eines Kindprozesses:
if(fork()==0)
{
//Kindprozess liegt vor
int zaehler=1;
char ausgabe[256]={0};
while(zaehler<=k){
//printf("%dt"
int pid=getpid();
int ppid=getppid();
sprintf(ausgabe, "%d %c %d %c %dn", pid,' ', ppid,' ',zaehler);
write(STDOUT_FILENO, ausgabe, strlen(ausgabe));
sleep(1);
zaehler++;
}
//write(STDOUT_FILENO, (getpid()+k)%100, strlen((getpid()+k)/100));
//printf("%dn", (getpid()+k)%100);
out=(getpid()+k)%100;
printf("%i", out);
exit((getpid()+k)%100);
}
else
{
//Elternprozess liegt vor
time_t curtime;
time(&curtime);
printf("Start: %s", ctime(&curtime));

}
//int exitcode=wait(NULL);
wait(NULL);
//exitcode to String casten:
char str[24];
sprintf(str, "Exit-Code: %in", out);
//Ausgabe und exitcode zu einem String zusammenfuegen: (vorher concat())
//char* s = concat("Exit-Code: ", str);
//strncat(*str,"Exit-Code: ",str);
//Ausgabe des Exitcodes:
write(STDOUT_FILENO, str, strlen(str));
time_t curtime;
time(&curtime);
printf("Ende: %sn", ctime(&curtime));
return 0;
}









share|improve this question



























    0














    I have the task that the parent process needs to output exit code of the child process. This exit code is supposed to be the sum of the child process id, with an added variable k and modulo 100 of the whole. I have tried two approaches to save the exit-code from the child process:




    • exit(exit-code) in child process and saving in parent process with wait(). You should still this in the comments

    • saving exit-code in global variable and outputting exit-code after wait() in parent process


    However, both did not work. Could you help me how I could achieve it? Thank you!



    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <string.h>
    #include <time.h>

    //globale Variable
    int out;

    int main()
    {
    //Nutzereingabe von k:
    int k=0;
    scanf("%d",&k);
    //Erzeugen eines Kindprozesses:
    if(fork()==0)
    {
    //Kindprozess liegt vor
    int zaehler=1;
    char ausgabe[256]={0};
    while(zaehler<=k){
    //printf("%dt"
    int pid=getpid();
    int ppid=getppid();
    sprintf(ausgabe, "%d %c %d %c %dn", pid,' ', ppid,' ',zaehler);
    write(STDOUT_FILENO, ausgabe, strlen(ausgabe));
    sleep(1);
    zaehler++;
    }
    //write(STDOUT_FILENO, (getpid()+k)%100, strlen((getpid()+k)/100));
    //printf("%dn", (getpid()+k)%100);
    out=(getpid()+k)%100;
    printf("%i", out);
    exit((getpid()+k)%100);
    }
    else
    {
    //Elternprozess liegt vor
    time_t curtime;
    time(&curtime);
    printf("Start: %s", ctime(&curtime));

    }
    //int exitcode=wait(NULL);
    wait(NULL);
    //exitcode to String casten:
    char str[24];
    sprintf(str, "Exit-Code: %in", out);
    //Ausgabe und exitcode zu einem String zusammenfuegen: (vorher concat())
    //char* s = concat("Exit-Code: ", str);
    //strncat(*str,"Exit-Code: ",str);
    //Ausgabe des Exitcodes:
    write(STDOUT_FILENO, str, strlen(str));
    time_t curtime;
    time(&curtime);
    printf("Ende: %sn", ctime(&curtime));
    return 0;
    }









    share|improve this question

























      0












      0








      0







      I have the task that the parent process needs to output exit code of the child process. This exit code is supposed to be the sum of the child process id, with an added variable k and modulo 100 of the whole. I have tried two approaches to save the exit-code from the child process:




      • exit(exit-code) in child process and saving in parent process with wait(). You should still this in the comments

      • saving exit-code in global variable and outputting exit-code after wait() in parent process


      However, both did not work. Could you help me how I could achieve it? Thank you!



      #include <stdio.h>
      #include <stdlib.h>
      #include <sys/types.h>
      #include <sys/wait.h>
      #include <unistd.h>
      #include <string.h>
      #include <time.h>

      //globale Variable
      int out;

      int main()
      {
      //Nutzereingabe von k:
      int k=0;
      scanf("%d",&k);
      //Erzeugen eines Kindprozesses:
      if(fork()==0)
      {
      //Kindprozess liegt vor
      int zaehler=1;
      char ausgabe[256]={0};
      while(zaehler<=k){
      //printf("%dt"
      int pid=getpid();
      int ppid=getppid();
      sprintf(ausgabe, "%d %c %d %c %dn", pid,' ', ppid,' ',zaehler);
      write(STDOUT_FILENO, ausgabe, strlen(ausgabe));
      sleep(1);
      zaehler++;
      }
      //write(STDOUT_FILENO, (getpid()+k)%100, strlen((getpid()+k)/100));
      //printf("%dn", (getpid()+k)%100);
      out=(getpid()+k)%100;
      printf("%i", out);
      exit((getpid()+k)%100);
      }
      else
      {
      //Elternprozess liegt vor
      time_t curtime;
      time(&curtime);
      printf("Start: %s", ctime(&curtime));

      }
      //int exitcode=wait(NULL);
      wait(NULL);
      //exitcode to String casten:
      char str[24];
      sprintf(str, "Exit-Code: %in", out);
      //Ausgabe und exitcode zu einem String zusammenfuegen: (vorher concat())
      //char* s = concat("Exit-Code: ", str);
      //strncat(*str,"Exit-Code: ",str);
      //Ausgabe des Exitcodes:
      write(STDOUT_FILENO, str, strlen(str));
      time_t curtime;
      time(&curtime);
      printf("Ende: %sn", ctime(&curtime));
      return 0;
      }









      share|improve this question













      I have the task that the parent process needs to output exit code of the child process. This exit code is supposed to be the sum of the child process id, with an added variable k and modulo 100 of the whole. I have tried two approaches to save the exit-code from the child process:




      • exit(exit-code) in child process and saving in parent process with wait(). You should still this in the comments

      • saving exit-code in global variable and outputting exit-code after wait() in parent process


      However, both did not work. Could you help me how I could achieve it? Thank you!



      #include <stdio.h>
      #include <stdlib.h>
      #include <sys/types.h>
      #include <sys/wait.h>
      #include <unistd.h>
      #include <string.h>
      #include <time.h>

      //globale Variable
      int out;

      int main()
      {
      //Nutzereingabe von k:
      int k=0;
      scanf("%d",&k);
      //Erzeugen eines Kindprozesses:
      if(fork()==0)
      {
      //Kindprozess liegt vor
      int zaehler=1;
      char ausgabe[256]={0};
      while(zaehler<=k){
      //printf("%dt"
      int pid=getpid();
      int ppid=getppid();
      sprintf(ausgabe, "%d %c %d %c %dn", pid,' ', ppid,' ',zaehler);
      write(STDOUT_FILENO, ausgabe, strlen(ausgabe));
      sleep(1);
      zaehler++;
      }
      //write(STDOUT_FILENO, (getpid()+k)%100, strlen((getpid()+k)/100));
      //printf("%dn", (getpid()+k)%100);
      out=(getpid()+k)%100;
      printf("%i", out);
      exit((getpid()+k)%100);
      }
      else
      {
      //Elternprozess liegt vor
      time_t curtime;
      time(&curtime);
      printf("Start: %s", ctime(&curtime));

      }
      //int exitcode=wait(NULL);
      wait(NULL);
      //exitcode to String casten:
      char str[24];
      sprintf(str, "Exit-Code: %in", out);
      //Ausgabe und exitcode zu einem String zusammenfuegen: (vorher concat())
      //char* s = concat("Exit-Code: ", str);
      //strncat(*str,"Exit-Code: ",str);
      //Ausgabe des Exitcodes:
      write(STDOUT_FILENO, str, strlen(str));
      time_t curtime;
      time(&curtime);
      printf("Ende: %sn", ctime(&curtime));
      return 0;
      }






      c linux






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 14:21









      SQLLearner

      85




      85
























          1 Answer
          1






          active

          oldest

          votes


















          1














          From man wait:




          pid_t wait(int *status);



          If status is not NULL, wait() and waitpid() store status information in the int to which it points.



          WEXITSTATUS(status)
          returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) ....




          So use:



          }
          // warte fur unserer kind
          int exitstatus;
          wait(&exitstatus);
          // caste exitcode to string casten
          char str[24];
          sprintf(str, "Exit-Code: %dn", WEXITSTATUS(exitstatus));





          share|improve this answer





















          • Thank you this helped! Maybe could you also shortly explain why my approach with the global variables did not work? Thank you!
            – SQLLearner
            Nov 21 '18 at 14:51










          • There are no global variables between processes. Each process is separate - there is nothing connecting between them, except for waitpid and fork return value. Each process has a separate out variable. There two processes, they do not "interconnect" in any way. You can create a "shared memory" (pool) in which you will place your variable (the name "shared" means it is the memory that shared between processes) or use threads (pthreads) that create threads - one process, multiple executions.
            – Kamil Cuk
            Nov 21 '18 at 14:53











          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%2f53414146%2fshare-number-of-child-process-to-parent-process-exit-wait-or-global-varia%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














          From man wait:




          pid_t wait(int *status);



          If status is not NULL, wait() and waitpid() store status information in the int to which it points.



          WEXITSTATUS(status)
          returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) ....




          So use:



          }
          // warte fur unserer kind
          int exitstatus;
          wait(&exitstatus);
          // caste exitcode to string casten
          char str[24];
          sprintf(str, "Exit-Code: %dn", WEXITSTATUS(exitstatus));





          share|improve this answer





















          • Thank you this helped! Maybe could you also shortly explain why my approach with the global variables did not work? Thank you!
            – SQLLearner
            Nov 21 '18 at 14:51










          • There are no global variables between processes. Each process is separate - there is nothing connecting between them, except for waitpid and fork return value. Each process has a separate out variable. There two processes, they do not "interconnect" in any way. You can create a "shared memory" (pool) in which you will place your variable (the name "shared" means it is the memory that shared between processes) or use threads (pthreads) that create threads - one process, multiple executions.
            – Kamil Cuk
            Nov 21 '18 at 14:53
















          1














          From man wait:




          pid_t wait(int *status);



          If status is not NULL, wait() and waitpid() store status information in the int to which it points.



          WEXITSTATUS(status)
          returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) ....




          So use:



          }
          // warte fur unserer kind
          int exitstatus;
          wait(&exitstatus);
          // caste exitcode to string casten
          char str[24];
          sprintf(str, "Exit-Code: %dn", WEXITSTATUS(exitstatus));





          share|improve this answer





















          • Thank you this helped! Maybe could you also shortly explain why my approach with the global variables did not work? Thank you!
            – SQLLearner
            Nov 21 '18 at 14:51










          • There are no global variables between processes. Each process is separate - there is nothing connecting between them, except for waitpid and fork return value. Each process has a separate out variable. There two processes, they do not "interconnect" in any way. You can create a "shared memory" (pool) in which you will place your variable (the name "shared" means it is the memory that shared between processes) or use threads (pthreads) that create threads - one process, multiple executions.
            – Kamil Cuk
            Nov 21 '18 at 14:53














          1












          1








          1






          From man wait:




          pid_t wait(int *status);



          If status is not NULL, wait() and waitpid() store status information in the int to which it points.



          WEXITSTATUS(status)
          returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) ....




          So use:



          }
          // warte fur unserer kind
          int exitstatus;
          wait(&exitstatus);
          // caste exitcode to string casten
          char str[24];
          sprintf(str, "Exit-Code: %dn", WEXITSTATUS(exitstatus));





          share|improve this answer












          From man wait:




          pid_t wait(int *status);



          If status is not NULL, wait() and waitpid() store status information in the int to which it points.



          WEXITSTATUS(status)
          returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) ....




          So use:



          }
          // warte fur unserer kind
          int exitstatus;
          wait(&exitstatus);
          // caste exitcode to string casten
          char str[24];
          sprintf(str, "Exit-Code: %dn", WEXITSTATUS(exitstatus));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:31









          Kamil Cuk

          8,9181523




          8,9181523












          • Thank you this helped! Maybe could you also shortly explain why my approach with the global variables did not work? Thank you!
            – SQLLearner
            Nov 21 '18 at 14:51










          • There are no global variables between processes. Each process is separate - there is nothing connecting between them, except for waitpid and fork return value. Each process has a separate out variable. There two processes, they do not "interconnect" in any way. You can create a "shared memory" (pool) in which you will place your variable (the name "shared" means it is the memory that shared between processes) or use threads (pthreads) that create threads - one process, multiple executions.
            – Kamil Cuk
            Nov 21 '18 at 14:53


















          • Thank you this helped! Maybe could you also shortly explain why my approach with the global variables did not work? Thank you!
            – SQLLearner
            Nov 21 '18 at 14:51










          • There are no global variables between processes. Each process is separate - there is nothing connecting between them, except for waitpid and fork return value. Each process has a separate out variable. There two processes, they do not "interconnect" in any way. You can create a "shared memory" (pool) in which you will place your variable (the name "shared" means it is the memory that shared between processes) or use threads (pthreads) that create threads - one process, multiple executions.
            – Kamil Cuk
            Nov 21 '18 at 14:53
















          Thank you this helped! Maybe could you also shortly explain why my approach with the global variables did not work? Thank you!
          – SQLLearner
          Nov 21 '18 at 14:51




          Thank you this helped! Maybe could you also shortly explain why my approach with the global variables did not work? Thank you!
          – SQLLearner
          Nov 21 '18 at 14:51












          There are no global variables between processes. Each process is separate - there is nothing connecting between them, except for waitpid and fork return value. Each process has a separate out variable. There two processes, they do not "interconnect" in any way. You can create a "shared memory" (pool) in which you will place your variable (the name "shared" means it is the memory that shared between processes) or use threads (pthreads) that create threads - one process, multiple executions.
          – Kamil Cuk
          Nov 21 '18 at 14:53




          There are no global variables between processes. Each process is separate - there is nothing connecting between them, except for waitpid and fork return value. Each process has a separate out variable. There two processes, they do not "interconnect" in any way. You can create a "shared memory" (pool) in which you will place your variable (the name "shared" means it is the memory that shared between processes) or use threads (pthreads) that create threads - one process, multiple executions.
          – Kamil Cuk
          Nov 21 '18 at 14:53


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53414146%2fshare-number-of-child-process-to-parent-process-exit-wait-or-global-varia%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'