pipe.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 05 Mar 2010 22:15:40 +0100
changeset 117 5f3a34ba6489
parent 113 397270b5d21a
child 140 05d42a3737a4
permissions -rw-r--r--
Makefile: common linker flags committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
107
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * pipe.c
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2009 Markus Broeker
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <unistd.h>
113
397270b5d21a sys/wait.h is the proper header, not wait.h
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 107
diff changeset
    10
#include <sys/wait.h>
107
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <assert.h>
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#define MAX 50
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
int main (int argc, char **argv)
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
{
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    int tube[2];
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    int i, status, count = 0;
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    pid_t pid;
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    char buffer[80];
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    for (i = 0; i < MAX; i++) {
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
        if (pipe (tube) == -1) {
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
            perror ("PIPE");
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
            return EXIT_FAILURE;
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
        }
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
        pid = fork ();
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        switch (pid) {
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        case 0:
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
            // we only want to write
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
            close (tube[0]);
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
            /**
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
             * copy on write: every forked process gets its own copy of i
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
             *                and the magic of synchronization works
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
             */
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
            for (i = 0; i < MAX; i++)
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
                status = write (tube[1], "MESSAGE FROM SON\n", 17);
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
            break;
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        case -1:
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
            printf ("ERROR");
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
            break;
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
        default:
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
            printf ("Son started as pid %d\n", pid);
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
            // we only want to read...
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
            close (tube[1]);
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
            FILE *f = fdopen (tube[0], "r");
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
            assert (f != NULL);
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
            while (fgets (buffer, sizeof (buffer), f) != NULL) {
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
                printf ("FATHER RECEIVED [%4d]: %s", count++, buffer);
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
            }
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
            if (fclose (f) < 0)
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
                perror ("fclose");
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
            printf ("PID [%4d] has finished...\n", wait (&pid));
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
        }
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    }
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    return EXIT_SUCCESS;
244356bc3a20 Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
}