read function never return 0 in C












1















I'm trying to read float values through a pipe as long as there is data in it. The problem is that the read function never returns 0 in my case, so my function never gets out of the loop.

Moreover, I cannot use library functions.



To give you the context : I want to create child processes which run the following function. Each float value is sent by the parent through the pipe and is read by one of the children processes which is supposed to sleep in order to let another child process read 1 other value and sleep. When a process wakes up, he's supposed to check if it can get a value from the pipe to sleep again, otherwise the loop ends and the child process is exited.



void worker(int * pip){
close(pip[1]);
float buffer;
while(read(pip[0], &buffer, sizeof(float)) != 0){
sleep(buffer);
}
exit(1);
}


Each value is written by the parent this way in a loop :



write(pip[1], &values[i], sizeof(float))









share|improve this question




















  • 1





    Learn about multiplexing system calls such as poll(2)

    – Basile Starynkevitch
    Nov 26 '18 at 10:17






  • 2





    Note that you are likely to go in an infinite loop on read failures (returning -1)

    – alamit
    Nov 26 '18 at 10:25
















1















I'm trying to read float values through a pipe as long as there is data in it. The problem is that the read function never returns 0 in my case, so my function never gets out of the loop.

Moreover, I cannot use library functions.



To give you the context : I want to create child processes which run the following function. Each float value is sent by the parent through the pipe and is read by one of the children processes which is supposed to sleep in order to let another child process read 1 other value and sleep. When a process wakes up, he's supposed to check if it can get a value from the pipe to sleep again, otherwise the loop ends and the child process is exited.



void worker(int * pip){
close(pip[1]);
float buffer;
while(read(pip[0], &buffer, sizeof(float)) != 0){
sleep(buffer);
}
exit(1);
}


Each value is written by the parent this way in a loop :



write(pip[1], &values[i], sizeof(float))









share|improve this question




















  • 1





    Learn about multiplexing system calls such as poll(2)

    – Basile Starynkevitch
    Nov 26 '18 at 10:17






  • 2





    Note that you are likely to go in an infinite loop on read failures (returning -1)

    – alamit
    Nov 26 '18 at 10:25














1












1








1








I'm trying to read float values through a pipe as long as there is data in it. The problem is that the read function never returns 0 in my case, so my function never gets out of the loop.

Moreover, I cannot use library functions.



To give you the context : I want to create child processes which run the following function. Each float value is sent by the parent through the pipe and is read by one of the children processes which is supposed to sleep in order to let another child process read 1 other value and sleep. When a process wakes up, he's supposed to check if it can get a value from the pipe to sleep again, otherwise the loop ends and the child process is exited.



void worker(int * pip){
close(pip[1]);
float buffer;
while(read(pip[0], &buffer, sizeof(float)) != 0){
sleep(buffer);
}
exit(1);
}


Each value is written by the parent this way in a loop :



write(pip[1], &values[i], sizeof(float))









share|improve this question
















I'm trying to read float values through a pipe as long as there is data in it. The problem is that the read function never returns 0 in my case, so my function never gets out of the loop.

Moreover, I cannot use library functions.



To give you the context : I want to create child processes which run the following function. Each float value is sent by the parent through the pipe and is read by one of the children processes which is supposed to sleep in order to let another child process read 1 other value and sleep. When a process wakes up, he's supposed to check if it can get a value from the pipe to sleep again, otherwise the loop ends and the child process is exited.



void worker(int * pip){
close(pip[1]);
float buffer;
while(read(pip[0], &buffer, sizeof(float)) != 0){
sleep(buffer);
}
exit(1);
}


Each value is written by the parent this way in a loop :



write(pip[1], &values[i], sizeof(float))






c pipe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 10:28









Sourav Ghosh

111k15132191




111k15132191










asked Nov 26 '18 at 10:14









Arthur KlipfelArthur Klipfel

2118




2118








  • 1





    Learn about multiplexing system calls such as poll(2)

    – Basile Starynkevitch
    Nov 26 '18 at 10:17






  • 2





    Note that you are likely to go in an infinite loop on read failures (returning -1)

    – alamit
    Nov 26 '18 at 10:25














  • 1





    Learn about multiplexing system calls such as poll(2)

    – Basile Starynkevitch
    Nov 26 '18 at 10:17






  • 2





    Note that you are likely to go in an infinite loop on read failures (returning -1)

    – alamit
    Nov 26 '18 at 10:25








1




1





Learn about multiplexing system calls such as poll(2)

– Basile Starynkevitch
Nov 26 '18 at 10:17





Learn about multiplexing system calls such as poll(2)

– Basile Starynkevitch
Nov 26 '18 at 10:17




2




2





Note that you are likely to go in an infinite loop on read failures (returning -1)

– alamit
Nov 26 '18 at 10:25





Note that you are likely to go in an infinite loop on read failures (returning -1)

– alamit
Nov 26 '18 at 10:25












1 Answer
1






active

oldest

votes


















3














You should call close(pip[1]); after parent write all data.



The following code could work:



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

void worker(int * pip){
close(pip[1]);
float buffer;
while(read(pip[0], &buffer, sizeof(float)) != 0){
printf("%fn", buffer);
}
exit(1);
}

int main() {
int pip[2];
pipe(pip);

if (fork() == 0) {
worker(pip);
exit(1);
}

close(pip[0]);
float a[4] = {1.1, 2.2, 3.3, 4.4};

for (int i = 0; i != sizeof(a) / sizeof(a[0]); ++i)
write(pip[1], a + i, sizeof(a[0]));
close(pip[1]);

wait(NULL);

return 0;
}





share|improve this answer
























    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%2f53478923%2fread-function-never-return-0-in-c%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









    3














    You should call close(pip[1]); after parent write all data.



    The following code could work:



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

    void worker(int * pip){
    close(pip[1]);
    float buffer;
    while(read(pip[0], &buffer, sizeof(float)) != 0){
    printf("%fn", buffer);
    }
    exit(1);
    }

    int main() {
    int pip[2];
    pipe(pip);

    if (fork() == 0) {
    worker(pip);
    exit(1);
    }

    close(pip[0]);
    float a[4] = {1.1, 2.2, 3.3, 4.4};

    for (int i = 0; i != sizeof(a) / sizeof(a[0]); ++i)
    write(pip[1], a + i, sizeof(a[0]));
    close(pip[1]);

    wait(NULL);

    return 0;
    }





    share|improve this answer




























      3














      You should call close(pip[1]); after parent write all data.



      The following code could work:



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

      void worker(int * pip){
      close(pip[1]);
      float buffer;
      while(read(pip[0], &buffer, sizeof(float)) != 0){
      printf("%fn", buffer);
      }
      exit(1);
      }

      int main() {
      int pip[2];
      pipe(pip);

      if (fork() == 0) {
      worker(pip);
      exit(1);
      }

      close(pip[0]);
      float a[4] = {1.1, 2.2, 3.3, 4.4};

      for (int i = 0; i != sizeof(a) / sizeof(a[0]); ++i)
      write(pip[1], a + i, sizeof(a[0]));
      close(pip[1]);

      wait(NULL);

      return 0;
      }





      share|improve this answer


























        3












        3








        3







        You should call close(pip[1]); after parent write all data.



        The following code could work:



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

        void worker(int * pip){
        close(pip[1]);
        float buffer;
        while(read(pip[0], &buffer, sizeof(float)) != 0){
        printf("%fn", buffer);
        }
        exit(1);
        }

        int main() {
        int pip[2];
        pipe(pip);

        if (fork() == 0) {
        worker(pip);
        exit(1);
        }

        close(pip[0]);
        float a[4] = {1.1, 2.2, 3.3, 4.4};

        for (int i = 0; i != sizeof(a) / sizeof(a[0]); ++i)
        write(pip[1], a + i, sizeof(a[0]));
        close(pip[1]);

        wait(NULL);

        return 0;
        }





        share|improve this answer













        You should call close(pip[1]); after parent write all data.



        The following code could work:



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

        void worker(int * pip){
        close(pip[1]);
        float buffer;
        while(read(pip[0], &buffer, sizeof(float)) != 0){
        printf("%fn", buffer);
        }
        exit(1);
        }

        int main() {
        int pip[2];
        pipe(pip);

        if (fork() == 0) {
        worker(pip);
        exit(1);
        }

        close(pip[0]);
        float a[4] = {1.1, 2.2, 3.3, 4.4};

        for (int i = 0; i != sizeof(a) / sizeof(a[0]); ++i)
        write(pip[1], a + i, sizeof(a[0]));
        close(pip[1]);

        wait(NULL);

        return 0;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 10:37









        Yunbin LiuYunbin Liu

        1,2762515




        1,2762515
































            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%2f53478923%2fread-function-never-return-0-in-c%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

            Wiesbaden

            Marschland

            Dieringhausen