Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why doesn't this print 0 through 999?

On Linux, why does this print 0 and hang rather than printing 0 up to 999?

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main() {
    int n = 1000;
    int fromparent[2];
    int fromchild[2];
    int *x = 0;

    pipe(fromparent);
    pipe(fromchild);
    if (fork() == 0) {
        /* child */
        while (1) {
            read(fromparent[0], x, 1);
            write(fromchild[1], x, 1);
        }
    } else {
        /* parent */
        for (int i = 0; i < n; i++) {
            printf("%d\n",  i);
            write(fromparent[1], x, 1);
            read(fromchild[0], x, 1);
        }
        exit(0);
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem is simple: you pass a null pointer to read and write.

Change the definition of x from int *x = 0; to:

char x[1] = { 0 };

As posted, the behavior is somewhat counterintuitive:

  • passing a null pointer to write causes it to return -1 immediately, setting errno to EINVAL.

  • conversely, passing a null pointer to read causes it to wait for input and only return -1 and set errno to EINVAL once input is available. This is causing both processes to block on the read calls.

Here is a simple test to illustrate this behavior:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int main() {
    int n;
    errno = 0;
    n = read(0, NULL, 1);
    printf("n=%d, errno=%d\n", n, errno);
    return 0;
}

This program waits for input if run from the console and prints n=-1, errno=14 after the user hits enter.

Conversely, it prints n=0, errno=0 when standard input is redirected from /dev/null.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading