1 | /* Copyright (C) 2012 by Mattia Monga <mattia.monga@unimi.it> */
|
---|
2 | /* $Id$ */
|
---|
3 |
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include <string.h>
|
---|
7 | #include <sys/types.h>
|
---|
8 | #include <sys/stat.h>
|
---|
9 | #include <fcntl.h>
|
---|
10 | #include <unistd.h>
|
---|
11 | #include <limits.h>
|
---|
12 |
|
---|
13 | void lsofd(const char* name){
|
---|
14 | int i;
|
---|
15 | for (i=0; i<_POSIX_OPEN_MAX; i++){
|
---|
16 | struct stat buf;
|
---|
17 | if (fstat(i, &buf) == 0){
|
---|
18 | printf("%s fd:%d i-node: %d\n", name, i, buf.st_ino);
|
---|
19 | }
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | main(){
|
---|
24 | pid_t pid;
|
---|
25 | int f, off;
|
---|
26 | char string[] = "Hello, world!\n";
|
---|
27 |
|
---|
28 | lsofd("padre (senza figli)");
|
---|
29 | printf("padre (senza figli) pipe *\n");
|
---|
30 | f = open("provaxxx.dat", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU);
|
---|
31 | if (f == -1){
|
---|
32 | perror("pipe");
|
---|
33 | exit(1);
|
---|
34 | }
|
---|
35 | lsofd("padre (senza figli)");
|
---|
36 | if (write(f, string, (strlen(string))) != (strlen(string)) ){
|
---|
37 | perror("write");
|
---|
38 | exit(1);
|
---|
39 | }
|
---|
40 |
|
---|
41 | off = lseek(f, 0, SEEK_CUR);
|
---|
42 | printf("padre (senza figli) seek: %d\n", off);
|
---|
43 |
|
---|
44 | printf("padre (senza figli) fork *\n");
|
---|
45 | if ( (pid = fork()) < 0){
|
---|
46 | perror("fork");
|
---|
47 | exit(1);
|
---|
48 | }
|
---|
49 | if (pid > 0){
|
---|
50 | lsofd("padre");
|
---|
51 | printf("padre close *\n");
|
---|
52 | printf("padre write *\n");
|
---|
53 | off = lseek(f, 0, SEEK_CUR);
|
---|
54 | printf("padre seek prima: %d\n", off);
|
---|
55 | if (write(f, string, (strlen(string))) != (strlen(string)) ){
|
---|
56 | perror("write");
|
---|
57 | exit(1);
|
---|
58 | }
|
---|
59 | lsofd("padre");
|
---|
60 | off = lseek(f, 0, SEEK_CUR);
|
---|
61 | printf("padre seek dopo: %d\n", off);
|
---|
62 | exit(0);
|
---|
63 | }
|
---|
64 | else {
|
---|
65 | lsofd("figlio");
|
---|
66 | printf("figlio close *\n");
|
---|
67 | printf("figlio write *\n");
|
---|
68 | off = lseek(f, 0, SEEK_CUR);
|
---|
69 | printf("figlio seek prima: %d\n", off);
|
---|
70 | if (write(f, string, (strlen(string))) != (strlen(string)) ){
|
---|
71 | perror("write");
|
---|
72 | exit(1);
|
---|
73 | }
|
---|
74 | lsofd("figlio");
|
---|
75 | off = lseek(f, 0, SEEK_CUR);
|
---|
76 | printf("figlio seek dopo: %d\n", off);
|
---|
77 | exit(0);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* Local Variables: */
|
---|
82 | /* compile-command: "make -k " */
|
---|
83 | /* End: */
|
---|