-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathes_7.c
100 lines (79 loc) · 1.58 KB
/
es_7.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#define READ 0
#define WRITE 1
int main(int argc, char *argv[])
{
int p0p1[2], p1p2[2],p2p3[2], pid;
char *punt,tempo[10];
double tot;
char stringa;
if (argc != 2)
{
printf("Numero argomenti sbagliato\n");
exit(1);
}
pipe(p0p1);
pid = fork();
if (pid == 0)
{
close(p0p1[READ]); // 0 = lettura 1 = scrittura
close(WRITE);
dup(p0p1[WRITE]);
close(p0p1[WRITE]);
execl("/usr/sbin/traceroute", "traceroute", argv[1], NULL);
return -1;
}
pipe(p1p2);
pid = fork();
if(pid == 0)
{
close(p0p1[WRITE]);
close(READ);
dup(p0p1[READ]);
close(p0p1[READ]);
close(p1p2[READ]);
close(WRITE);
dup(p1p2[WRITE]);
close(p1p2[WRITE]);
execl("/usr/bin/awk", "awk", "{print $4}", NULL);
return -1;
}
close(p0p1[READ]);
close(p0p1[WRITE]);
pipe(p2p3);
pid = fork();
if(pid == 0)
{
close(p1p2[WRITE]);
close(READ);
dup(p1p2[READ]);
close(p1p2[READ]);
close(p2p3[READ]);
close(WRITE);
dup(p2p3[WRITE]);
close(p2p3[WRITE]);
execl("/usr/bin/tail", "tail", "-n", "+2", NULL);
return -1;
}
close(p1p2[WRITE]);
close(p1p2[READ]);
close(p2p3[WRITE]);
while (read(p2p3[0], &stringa, sizeof(stringa)) > 0)
{
strncat(tempo, &stringa, sizeof(stringa));
if (stringa == '\n')
{
printf("Tempo parziale: %s", tempo);
tot = tot + strtod(tempo, &punt);
tempo[0] = '\0';
}
}
close(p2p3[0]);
printf("\nIl tempo totale impiegato è: %.2lf ms\n", tot);
return 0;
return 0;
}