C pipe bad value from parent proccess












0















I have this simple program, but when I wanted to check the value that I get from the child it returns 1995694080 or similiar numbers. I'm a bit confused, how can I achieve to get the real value of msg? It looks like I'm getting some random trash value from somewhere else where I'm not supposed to. I made this pipe sendier based on a tutorial, and it's mostly works just when I'm sending back the data from the child it fails. Any idea or suggestion?



int pipefd[2];

int main()
{
signal(SIGTERM, handler);
signal(SIGUSR1, finishedOrder);
pid_t pid;


if (pipe(pipefd) == -1)
{
perror("Error while making pipe.n");
exit(1);
}

pid = fork();

if (pid < 0)
{
perror("Fork error.n");
exit(1);
}

if (pid > 0)
{
employer(pid, 4, false);
}
else
{
employee(getppid());
}
}

void employer(pid_t pid, int id, bool two)
{
int rec;

printf("[Szülő]: várok egy szerelő csapatra! n");
sleep(1);

kill(pid, SIGTERM);

pause();

if (two)
{
/.../
}
else
{
printf("[Szülő]: dolgozz a %d. feladatonn", id);
close(pipefd[0]);
write(pipefd[1], &id, sizeof(id)); //this is works.
close(pipefd[1]);
sleep(1);

kill(pid, SIGTERM);

pause();

kill(pid, SIGTERM);
}

pause();

close(pipefd[1]);
read(pipefd[0], &rec, sizeof(rec));

printf("%dn", rec); // here I get the strange value

if (rec == 4)
{
printf("[Szülő]: feljegyeztem a módosításokat.n");
if (two)
{
/**/
}
else
{
/**/
}
}

}

void employee(pid_t emp)
{
int jobs[2];
pause();

printf("[Gyerek]: munkára jelentkezekn");
sleep(1);

kill(emp, SIGTERM);

pause();

close(pipefd[1]);

read(pipefd[0], &jobs[0], sizeof(jobs[0]));
close(pipefd[0]);
printf("[Gyerek]: elkezdek dolgozni a %d. feladaton.n", jobs[0]);
sleep(1);
kill(emp, SIGUSR1);
pause();

sleep(1);
read(pipefd[0], &jobs[1], sizeof(jobs[1]));


if (jobs[1] != 0)
{
printf("[Gyerek]: elkezdek dolgozni a %d. feladaton.n", jobs[1]);
sleep(1);

kill(emp, SIGUSR1);
}

close(pipefd[0]);
fflush(NULL);
int msg = 4;
printf("%dn", msg);
write(pipefd[1], &msg, sizeof(msg));
close(pipefd[1]);

sleep(1);
kill(emp, SIGTERM);
}

void handler(int signum)
{
}

void finishedOrder()
{
}









share|improve this question























  • Maybe that read didn't succeed? How would you know if you ignore its return value?

    – rustyx
    Nov 27 '18 at 16:16
















0















I have this simple program, but when I wanted to check the value that I get from the child it returns 1995694080 or similiar numbers. I'm a bit confused, how can I achieve to get the real value of msg? It looks like I'm getting some random trash value from somewhere else where I'm not supposed to. I made this pipe sendier based on a tutorial, and it's mostly works just when I'm sending back the data from the child it fails. Any idea or suggestion?



int pipefd[2];

int main()
{
signal(SIGTERM, handler);
signal(SIGUSR1, finishedOrder);
pid_t pid;


if (pipe(pipefd) == -1)
{
perror("Error while making pipe.n");
exit(1);
}

pid = fork();

if (pid < 0)
{
perror("Fork error.n");
exit(1);
}

if (pid > 0)
{
employer(pid, 4, false);
}
else
{
employee(getppid());
}
}

void employer(pid_t pid, int id, bool two)
{
int rec;

printf("[Szülő]: várok egy szerelő csapatra! n");
sleep(1);

kill(pid, SIGTERM);

pause();

if (two)
{
/.../
}
else
{
printf("[Szülő]: dolgozz a %d. feladatonn", id);
close(pipefd[0]);
write(pipefd[1], &id, sizeof(id)); //this is works.
close(pipefd[1]);
sleep(1);

kill(pid, SIGTERM);

pause();

kill(pid, SIGTERM);
}

pause();

close(pipefd[1]);
read(pipefd[0], &rec, sizeof(rec));

printf("%dn", rec); // here I get the strange value

if (rec == 4)
{
printf("[Szülő]: feljegyeztem a módosításokat.n");
if (two)
{
/**/
}
else
{
/**/
}
}

}

void employee(pid_t emp)
{
int jobs[2];
pause();

printf("[Gyerek]: munkára jelentkezekn");
sleep(1);

kill(emp, SIGTERM);

pause();

close(pipefd[1]);

read(pipefd[0], &jobs[0], sizeof(jobs[0]));
close(pipefd[0]);
printf("[Gyerek]: elkezdek dolgozni a %d. feladaton.n", jobs[0]);
sleep(1);
kill(emp, SIGUSR1);
pause();

sleep(1);
read(pipefd[0], &jobs[1], sizeof(jobs[1]));


if (jobs[1] != 0)
{
printf("[Gyerek]: elkezdek dolgozni a %d. feladaton.n", jobs[1]);
sleep(1);

kill(emp, SIGUSR1);
}

close(pipefd[0]);
fflush(NULL);
int msg = 4;
printf("%dn", msg);
write(pipefd[1], &msg, sizeof(msg));
close(pipefd[1]);

sleep(1);
kill(emp, SIGTERM);
}

void handler(int signum)
{
}

void finishedOrder()
{
}









share|improve this question























  • Maybe that read didn't succeed? How would you know if you ignore its return value?

    – rustyx
    Nov 27 '18 at 16:16














0












0








0








I have this simple program, but when I wanted to check the value that I get from the child it returns 1995694080 or similiar numbers. I'm a bit confused, how can I achieve to get the real value of msg? It looks like I'm getting some random trash value from somewhere else where I'm not supposed to. I made this pipe sendier based on a tutorial, and it's mostly works just when I'm sending back the data from the child it fails. Any idea or suggestion?



int pipefd[2];

int main()
{
signal(SIGTERM, handler);
signal(SIGUSR1, finishedOrder);
pid_t pid;


if (pipe(pipefd) == -1)
{
perror("Error while making pipe.n");
exit(1);
}

pid = fork();

if (pid < 0)
{
perror("Fork error.n");
exit(1);
}

if (pid > 0)
{
employer(pid, 4, false);
}
else
{
employee(getppid());
}
}

void employer(pid_t pid, int id, bool two)
{
int rec;

printf("[Szülő]: várok egy szerelő csapatra! n");
sleep(1);

kill(pid, SIGTERM);

pause();

if (two)
{
/.../
}
else
{
printf("[Szülő]: dolgozz a %d. feladatonn", id);
close(pipefd[0]);
write(pipefd[1], &id, sizeof(id)); //this is works.
close(pipefd[1]);
sleep(1);

kill(pid, SIGTERM);

pause();

kill(pid, SIGTERM);
}

pause();

close(pipefd[1]);
read(pipefd[0], &rec, sizeof(rec));

printf("%dn", rec); // here I get the strange value

if (rec == 4)
{
printf("[Szülő]: feljegyeztem a módosításokat.n");
if (two)
{
/**/
}
else
{
/**/
}
}

}

void employee(pid_t emp)
{
int jobs[2];
pause();

printf("[Gyerek]: munkára jelentkezekn");
sleep(1);

kill(emp, SIGTERM);

pause();

close(pipefd[1]);

read(pipefd[0], &jobs[0], sizeof(jobs[0]));
close(pipefd[0]);
printf("[Gyerek]: elkezdek dolgozni a %d. feladaton.n", jobs[0]);
sleep(1);
kill(emp, SIGUSR1);
pause();

sleep(1);
read(pipefd[0], &jobs[1], sizeof(jobs[1]));


if (jobs[1] != 0)
{
printf("[Gyerek]: elkezdek dolgozni a %d. feladaton.n", jobs[1]);
sleep(1);

kill(emp, SIGUSR1);
}

close(pipefd[0]);
fflush(NULL);
int msg = 4;
printf("%dn", msg);
write(pipefd[1], &msg, sizeof(msg));
close(pipefd[1]);

sleep(1);
kill(emp, SIGTERM);
}

void handler(int signum)
{
}

void finishedOrder()
{
}









share|improve this question














I have this simple program, but when I wanted to check the value that I get from the child it returns 1995694080 or similiar numbers. I'm a bit confused, how can I achieve to get the real value of msg? It looks like I'm getting some random trash value from somewhere else where I'm not supposed to. I made this pipe sendier based on a tutorial, and it's mostly works just when I'm sending back the data from the child it fails. Any idea or suggestion?



int pipefd[2];

int main()
{
signal(SIGTERM, handler);
signal(SIGUSR1, finishedOrder);
pid_t pid;


if (pipe(pipefd) == -1)
{
perror("Error while making pipe.n");
exit(1);
}

pid = fork();

if (pid < 0)
{
perror("Fork error.n");
exit(1);
}

if (pid > 0)
{
employer(pid, 4, false);
}
else
{
employee(getppid());
}
}

void employer(pid_t pid, int id, bool two)
{
int rec;

printf("[Szülő]: várok egy szerelő csapatra! n");
sleep(1);

kill(pid, SIGTERM);

pause();

if (two)
{
/.../
}
else
{
printf("[Szülő]: dolgozz a %d. feladatonn", id);
close(pipefd[0]);
write(pipefd[1], &id, sizeof(id)); //this is works.
close(pipefd[1]);
sleep(1);

kill(pid, SIGTERM);

pause();

kill(pid, SIGTERM);
}

pause();

close(pipefd[1]);
read(pipefd[0], &rec, sizeof(rec));

printf("%dn", rec); // here I get the strange value

if (rec == 4)
{
printf("[Szülő]: feljegyeztem a módosításokat.n");
if (two)
{
/**/
}
else
{
/**/
}
}

}

void employee(pid_t emp)
{
int jobs[2];
pause();

printf("[Gyerek]: munkára jelentkezekn");
sleep(1);

kill(emp, SIGTERM);

pause();

close(pipefd[1]);

read(pipefd[0], &jobs[0], sizeof(jobs[0]));
close(pipefd[0]);
printf("[Gyerek]: elkezdek dolgozni a %d. feladaton.n", jobs[0]);
sleep(1);
kill(emp, SIGUSR1);
pause();

sleep(1);
read(pipefd[0], &jobs[1], sizeof(jobs[1]));


if (jobs[1] != 0)
{
printf("[Gyerek]: elkezdek dolgozni a %d. feladaton.n", jobs[1]);
sleep(1);

kill(emp, SIGUSR1);
}

close(pipefd[0]);
fflush(NULL);
int msg = 4;
printf("%dn", msg);
write(pipefd[1], &msg, sizeof(msg));
close(pipefd[1]);

sleep(1);
kill(emp, SIGTERM);
}

void handler(int signum)
{
}

void finishedOrder()
{
}






c pipe fork






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 16:04









Bence SzabariBence Szabari

377




377













  • Maybe that read didn't succeed? How would you know if you ignore its return value?

    – rustyx
    Nov 27 '18 at 16:16



















  • Maybe that read didn't succeed? How would you know if you ignore its return value?

    – rustyx
    Nov 27 '18 at 16:16

















Maybe that read didn't succeed? How would you know if you ignore its return value?

– rustyx
Nov 27 '18 at 16:16





Maybe that read didn't succeed? How would you know if you ignore its return value?

– rustyx
Nov 27 '18 at 16:16












2 Answers
2






active

oldest

votes


















4














read(pipefd[0], &rec, sizeof(rec));


You don't check the value returned by read to see how much data you've received. If you did, you'll probably find that it is not what you expect because earlier on in your code you did this...



    close(pipefd[0]);


You can't read from a file descriptor after you've closed it. And somewhat more importantly, the "pipe" you created isn't bi-directional. If you want to send and receive, you need to do two calls to pipe to get two pairs of file descriptors.



Given that you also do close(pipefd[1]) before the read which means that you've closed both ends of the pipe, it looks like you've misunderstood what the purpose of closeing is for in this context. The child and parent close the end of the pipe that they're not using - that's it. It doesn't signify anything about who is reading or writing to the pipe.






share|improve this answer































    1














    In your employer() function, you close the read end of the pipe with close(pipefd[0]); then try to read from it later with read(pipefd[0], &rec, sizeof(rec));



    Trying to read from a closed fd probably causes weird values. will do what Johnathan Leffler says it will do.



    rustyx is right in the comments - checking the return value of read() is good idea, because it would uncover this kind of error.






    share|improve this answer





















    • 1





      Reading from a closed fd simply causes the read to fail rather quickly — return value is -1 and errno is EBADF. It doesn't modify the array passed to hold the data. The code doesn't, but should, check the return values from read and write.

      – Jonathan Leffler
      Nov 27 '18 at 16:50













    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%2f53503630%2fc-pipe-bad-value-from-parent-proccess%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









    4














    read(pipefd[0], &rec, sizeof(rec));


    You don't check the value returned by read to see how much data you've received. If you did, you'll probably find that it is not what you expect because earlier on in your code you did this...



        close(pipefd[0]);


    You can't read from a file descriptor after you've closed it. And somewhat more importantly, the "pipe" you created isn't bi-directional. If you want to send and receive, you need to do two calls to pipe to get two pairs of file descriptors.



    Given that you also do close(pipefd[1]) before the read which means that you've closed both ends of the pipe, it looks like you've misunderstood what the purpose of closeing is for in this context. The child and parent close the end of the pipe that they're not using - that's it. It doesn't signify anything about who is reading or writing to the pipe.






    share|improve this answer




























      4














      read(pipefd[0], &rec, sizeof(rec));


      You don't check the value returned by read to see how much data you've received. If you did, you'll probably find that it is not what you expect because earlier on in your code you did this...



          close(pipefd[0]);


      You can't read from a file descriptor after you've closed it. And somewhat more importantly, the "pipe" you created isn't bi-directional. If you want to send and receive, you need to do two calls to pipe to get two pairs of file descriptors.



      Given that you also do close(pipefd[1]) before the read which means that you've closed both ends of the pipe, it looks like you've misunderstood what the purpose of closeing is for in this context. The child and parent close the end of the pipe that they're not using - that's it. It doesn't signify anything about who is reading or writing to the pipe.






      share|improve this answer


























        4












        4








        4







        read(pipefd[0], &rec, sizeof(rec));


        You don't check the value returned by read to see how much data you've received. If you did, you'll probably find that it is not what you expect because earlier on in your code you did this...



            close(pipefd[0]);


        You can't read from a file descriptor after you've closed it. And somewhat more importantly, the "pipe" you created isn't bi-directional. If you want to send and receive, you need to do two calls to pipe to get two pairs of file descriptors.



        Given that you also do close(pipefd[1]) before the read which means that you've closed both ends of the pipe, it looks like you've misunderstood what the purpose of closeing is for in this context. The child and parent close the end of the pipe that they're not using - that's it. It doesn't signify anything about who is reading or writing to the pipe.






        share|improve this answer













        read(pipefd[0], &rec, sizeof(rec));


        You don't check the value returned by read to see how much data you've received. If you did, you'll probably find that it is not what you expect because earlier on in your code you did this...



            close(pipefd[0]);


        You can't read from a file descriptor after you've closed it. And somewhat more importantly, the "pipe" you created isn't bi-directional. If you want to send and receive, you need to do two calls to pipe to get two pairs of file descriptors.



        Given that you also do close(pipefd[1]) before the read which means that you've closed both ends of the pipe, it looks like you've misunderstood what the purpose of closeing is for in this context. The child and parent close the end of the pipe that they're not using - that's it. It doesn't signify anything about who is reading or writing to the pipe.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 16:23









        Chris TurnerChris Turner

        7,26811118




        7,26811118

























            1














            In your employer() function, you close the read end of the pipe with close(pipefd[0]); then try to read from it later with read(pipefd[0], &rec, sizeof(rec));



            Trying to read from a closed fd probably causes weird values. will do what Johnathan Leffler says it will do.



            rustyx is right in the comments - checking the return value of read() is good idea, because it would uncover this kind of error.






            share|improve this answer





















            • 1





              Reading from a closed fd simply causes the read to fail rather quickly — return value is -1 and errno is EBADF. It doesn't modify the array passed to hold the data. The code doesn't, but should, check the return values from read and write.

              – Jonathan Leffler
              Nov 27 '18 at 16:50


















            1














            In your employer() function, you close the read end of the pipe with close(pipefd[0]); then try to read from it later with read(pipefd[0], &rec, sizeof(rec));



            Trying to read from a closed fd probably causes weird values. will do what Johnathan Leffler says it will do.



            rustyx is right in the comments - checking the return value of read() is good idea, because it would uncover this kind of error.






            share|improve this answer





















            • 1





              Reading from a closed fd simply causes the read to fail rather quickly — return value is -1 and errno is EBADF. It doesn't modify the array passed to hold the data. The code doesn't, but should, check the return values from read and write.

              – Jonathan Leffler
              Nov 27 '18 at 16:50
















            1












            1








            1







            In your employer() function, you close the read end of the pipe with close(pipefd[0]); then try to read from it later with read(pipefd[0], &rec, sizeof(rec));



            Trying to read from a closed fd probably causes weird values. will do what Johnathan Leffler says it will do.



            rustyx is right in the comments - checking the return value of read() is good idea, because it would uncover this kind of error.






            share|improve this answer















            In your employer() function, you close the read end of the pipe with close(pipefd[0]); then try to read from it later with read(pipefd[0], &rec, sizeof(rec));



            Trying to read from a closed fd probably causes weird values. will do what Johnathan Leffler says it will do.



            rustyx is right in the comments - checking the return value of read() is good idea, because it would uncover this kind of error.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 '18 at 18:42

























            answered Nov 27 '18 at 16:29









            GandhiGandhiGandhiGandhi

            1045




            1045








            • 1





              Reading from a closed fd simply causes the read to fail rather quickly — return value is -1 and errno is EBADF. It doesn't modify the array passed to hold the data. The code doesn't, but should, check the return values from read and write.

              – Jonathan Leffler
              Nov 27 '18 at 16:50
















            • 1





              Reading from a closed fd simply causes the read to fail rather quickly — return value is -1 and errno is EBADF. It doesn't modify the array passed to hold the data. The code doesn't, but should, check the return values from read and write.

              – Jonathan Leffler
              Nov 27 '18 at 16:50










            1




            1





            Reading from a closed fd simply causes the read to fail rather quickly — return value is -1 and errno is EBADF. It doesn't modify the array passed to hold the data. The code doesn't, but should, check the return values from read and write.

            – Jonathan Leffler
            Nov 27 '18 at 16:50







            Reading from a closed fd simply causes the read to fail rather quickly — return value is -1 and errno is EBADF. It doesn't modify the array passed to hold the data. The code doesn't, but should, check the return values from read and write.

            – Jonathan Leffler
            Nov 27 '18 at 16:50




















            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%2f53503630%2fc-pipe-bad-value-from-parent-proccess%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)