Загрузка данных


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

int main(void)
{
    int fd[2];
    ssize_t nbytes;
    pid_t childpid;

    char string[] = "Hello, world!\n";
    char readbuffer[80];

    pipe(fd);

    if ((childpid = fork()) == -1) {
        perror("fork");
        exit(1);
    }

    if (childpid == 0) {
        close(fd[0]);
        write(fd[1], string, strlen(string));
        // close(fd[1]);   <-- убрали по заданию
        sleep(100);        // потомок "продолжает работать"
        exit(0);
    } else {
        close(fd[1]);
        while ((nbytes = read(fd[0], readbuffer, sizeof(readbuffer))) > 0) {
            printf("Received %zd bytes: %.*s", nbytes, (int)nbytes, readbuffer);
        }
        printf("read() returned %zd\n", nbytes);
        close(fd[0]);
        wait(NULL);
    }
    return 0;
}