-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_test.c
176 lines (135 loc) · 3.47 KB
/
pipe_test.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define R 0
#define W 1
int main()
{
int nRtn = 0;
int nPipeC2P[2];
int nPipeP2C[2];
int nRtnFcntl = 0;
int nStatus = 0;
char szBuff[65536];
pid_t nPid;
pid_t nPidWait;
nRtn = pipe( nPipeP2C );
if( nRtn < 0 ){
perror( "pipe()" );
exit( EXIT_FAILURE );
}
nRtn = pipe( nPipeC2P );
if( nRtn < 0 ){
perror( "pipe()" );
exit( EXIT_FAILURE );
}
/***
if(( nRtnFcntl = fcntl( nPipeP2C[0], F_GETFL, NULL ) ) < 0 ){
perror( "fcntl()" );
exit( EXIT_FAILURE );
}
nRtnFcntl |= O_NONBLOCK;
if( fcntl( nPipeP2C[0], F_SETFL, nRtnFcntl ) < 0 ){
perror( "fcntl()" );
exit( EXIT_FAILURE );
}
if(( nRtnFcntl = fcntl( nPipeP2C[1], F_GETFL, NULL ) ) < 0 ){
perror( "fcntl()" );
exit( EXIT_FAILURE );
}
nRtnFcntl |= O_NONBLOCK;
if( fcntl( nPipeP2C[1], F_SETFL, nRtnFcntl ) < 0 ){
perror( "fcntl()" );
exit( EXIT_FAILURE );
}
if(( nRtnFcntl = fcntl( nPipeC2P[0], F_GETFL, NULL ) ) < 0 ){
perror( "fcntl()" );
exit( EXIT_FAILURE );
}
nRtnFcntl |= O_NONBLOCK;
if( fcntl( nPipeC2P[0], F_SETFL, nRtnFcntl ) < 0 ){
perror( "fcntl()" );
exit( EXIT_FAILURE );
}
if(( nRtnFcntl = fcntl( nPipeC2P[1], F_GETFL, NULL ) ) < 0 ){
perror( "fcntl()" );
exit( EXIT_FAILURE );
}
nRtnFcntl |= O_NONBLOCK;
if( fcntl( nPipeC2P[1], F_SETFL, nRtnFcntl ) < 0 ){
perror( "fcntl()" );
exit( EXIT_FAILURE );
}
***/
nPid = fork();
if( nPid == -1 ){
/* error */
perror( "fork()" );
exit( EXIT_FAILURE );
} else if( nPid == 0 ){
/* child */
close( nPipeC2P[R] );
close( nPipeP2C[W] );
dup2( nPipeP2C[R], STDIN_FILENO );
dup2( nPipeC2P[W], STDOUT_FILENO );
close( nPipeP2C[R] );
close( nPipeC2P[W] );
sleep(2);
// nRtn = execlp( "telnet", "telnet", "me-simul", "80", NULL );
nRtn = execlp( "tr", "tr", "[:upper:]", "[:lower:]", NULL );
// nRtn = execlp( "echo", "echo", "test", NULL );
// nRtn = execlp( "./tcom", "./tcom", NULL );
// nRtn = execlp( "nkf", "nkf", "-MB", NULL );
if( nRtn < 0 ){
perror( "execlp()" );
exit( EXIT_FAILURE );
}
} else {
/* parent */
close( nPipeC2P[W] );
close( nPipeP2C[R] );
nRtn = write( nPipeP2C[W], "GET / HTTP/1.0\n\n", 16 );
close( nPipeP2C[W] );
printf("write:[%d]\n",nRtn);
memset( szBuff, 0x00, sizeof(szBuff) );
nRtn = read( nPipeC2P[R], szBuff, sizeof(szBuff) );
printf("read:[%d]\n",nRtn);
puts( szBuff );
// close( nPipeC2P[R] );
puts("-----------------------");
memset( szBuff, 0x00, sizeof(szBuff) );
nRtn = read( nPipeC2P[R], szBuff, sizeof(szBuff) );
printf("read:[%d]\n",nRtn);
puts( szBuff );
puts("-----------------------");
memset( szBuff, 0x00, sizeof(szBuff) );
nRtn = read( nPipeC2P[R], szBuff, sizeof(szBuff) );
printf("read:[%d]\n",nRtn);
puts( szBuff );
puts("-----------------------");
memset( szBuff, 0x00, sizeof(szBuff) );
nRtn = read( nPipeC2P[R], szBuff, sizeof(szBuff) );
printf("read:[%d]\n",nRtn);
puts( szBuff );
/* 子プロセスのPIDを指定して 終了を待つ(ブロックする) */
nPidWait = waitpid( nPid, &nStatus, 0 );
if( nPidWait < 0 ){
perror(" waitpid()" );
exit( EXIT_FAILURE );
}
if( WIFEXITED(nStatus) ){
/* 子プロセス正常終了 */
fprintf( stdout, "child is successful.\n" );
} else {
/* 子プロセス異常終了 */
fprintf( stderr, "child is failure.\n" );
}
}
exit( EXIT_SUCCESS );
}