post
poster: psYchotic
description: Piping: why not?
language: C
[download]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define IO_SIZE 4096

int main(int argc, char **argv) {
    int pipes[2];
    int pid;
    int file;
    int io;
    void *buf;

    if (argc != 2) {
        printf("Usage: %s <filename>\n", argv[0]);
        return 0;
    }
    
    if ((pid = fork()) == -1) {
        perror("Could not fork");
        return 1;
    }

    if (pipe(pipes) == -1) {
        perror("Could not open pipes");
        return 1;
    }

    if (pid == 0) {
        close(pipes[1]);
        if (dup2(pipes[0], fileno(stdin)) == -1) {
            perror("Could not set standard input of the child");
            return 1;
        }
        close(pipes[0]);
        /*
        fprintf(stderr, "Starting child process.\n");
        execlp("nl", "nl", "-l1", (char *) NULL);
        fprintf(stderr, "Whoops...\n");
        */

        char c;
        int lines = 1;
        int chars = 0;

        fprintf(stderr, "Consumer: Start reading shit.\n");
        while ((c = getchar()) != EOF) {
            if (c == '\n') lines++;
            chars++;
            fprintf(stderr, "Consumer: Read a char. %d thus far.\n", chars);
        }
        fprintf(stderr, "Consumer: Lines: %d\n", lines);
        fprintf(stderr, "Consumer: Chars: %d\n", chars);
    } else {
        int status;
        buf = malloc(sizeof(char) * IO_SIZE);
        file = open(argv[1], O_RDONLY);
        while ((io = read(file, buf, IO_SIZE)) > 0) {
            fprintf(stderr, "Producer: Read %dbytes\n", io);
            if ((io = write(pipes[1], buf, io)) == -1) {
                perror("Producer: Couldn't write to pipe");
                break;
            }
            fprintf(stderr, "Producer: Wrote %dbytes\n", io);
        }

        close(file);
        fprintf(stderr, "Producer: Closed input file.\n");
        free(buf);
        fprintf(stderr, "Producer: Freed buffer.\n");
        close(pipes[1]);
        fprintf(stderr, "Producer: Closed write-end of pipe.\n");
        fprintf(stderr, "Producer: Waiting for child to exit.\n");
        do {
            waitpid(pid, &status, 0);
        } while (!WIFEXITED(status));
        fprintf(stderr, "Producer: consumer exited, and so do I.\n");
    }

    return 0;
}