author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 27 Dec 2013 16:00:09 +0100 | |
changeset 167 | 7c6b221900bf |
parent 140 | 05d42a3737a4 |
permissions | -rw-r--r-- |
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 |
#include <stdio.h> |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
7 |
#include <stdlib.h> |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
8 |
#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
|
9 |
#include <sys/wait.h> |
107
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
10 |
#include <assert.h> |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
11 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
12 |
#define MAX 50 |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
13 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
14 |
int main (int argc, char **argv) |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
15 |
{ |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
16 |
int tube[2]; |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
17 |
int i, status, count = 0; |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
18 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
19 |
pid_t pid; |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
20 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
21 |
char buffer[80]; |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
22 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
23 |
for (i = 0; i < MAX; i++) { |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
24 |
if (pipe (tube) == -1) { |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
25 |
perror ("PIPE"); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
26 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
27 |
return EXIT_FAILURE; |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
28 |
} |
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 |
pid = fork (); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
31 |
switch (pid) { |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
32 |
case 0: |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
// we only want to write |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
34 |
close (tube[0]); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
35 |
|
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 |
* 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
|
38 |
* and the magic of synchronization works |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
39 |
*/ |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
40 |
for (i = 0; i < MAX; i++) |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
41 |
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
|
42 |
break; |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
43 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
44 |
case -1: |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
45 |
printf ("ERROR"); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
46 |
break; |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
47 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
48 |
default: |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
printf ("Son started as pid %d\n", pid); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
50 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
// we only want to read... |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
52 |
close (tube[1]); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
53 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
54 |
FILE *f = fdopen (tube[0], "r"); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
55 |
assert (f != NULL); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
56 |
while (fgets (buffer, sizeof (buffer), f) != NULL) { |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
57 |
printf ("FATHER RECEIVED [%4d]: %s", count++, buffer); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
58 |
} |
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 |
if (fclose (f) < 0) |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
61 |
perror ("fclose"); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
62 |
|
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
63 |
printf ("PID [%4d] has finished...\n", wait (&pid)); |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
64 |
} |
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 |
return EXIT_SUCCESS; |
244356bc3a20
Pipes and COPY-ON-WRITE
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
68 |
} |