Repository navigation
Expand file tree
/
Copy pathtest11.c
More file actions
162 lines (145 loc) · 3.97 KB
/
Copy pathtest11.c
File metadata and controls
162 lines (145 loc) · 3.97 KB
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
/*
* Test name: test11.c
*
* Objetive: The purpose of this test is to make sure that select works
* with pipes.
*
* Description: The select checks are divided in checks on writing for the
* parent process, which has the writing end of the pipe, and checks on reading
* and exception on the child process, which has the reading end of pipe. So
* when the first process is ready to write to the pipe it will request a string
* from the terminal and send it through the pipe. If the string is 'exit' then
* the pipe is closed. The child process is blocked in a select checking for read
* and exception. If there is data to be read then it will perform the read and
* prints the read data. If the pipe is closed (user typed 'exit'), the child
* process finishes.
*
* Jose M. Gomez
*/
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/asynchio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <signal.h>
void pipehandler(int sig)
{
}
void do_child(int data_pipe[])
{
/* reads from pipe and prints out the data */
char data[2048];
int retval;
fd_set fds_read;
fd_set fds_exception;
struct timeval timeout;
signal(SIGPIPE, pipehandler);
signal(SIGUSR1, pipehandler);
/* first, close the write part of the pipe, since it is not needed */
close(data_pipe[1]);
while(1) {
FD_ZERO(&fds_read);
FD_ZERO(&fds_exception);
FD_SET(data_pipe[0], &fds_read);
FD_SET(data_pipe[0], &fds_exception);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
retval = select(data_pipe[0]+1, &fds_read, NULL, &fds_exception, &timeout);
if (retval == -1) {
perror("select");
fprintf(stderr, "child: Error in select\n");
continue;
} else printf("child select: %d\n", retval);
if (FD_ISSET(data_pipe[0], &fds_exception)) {
printf("child: exception fd set. quitting.\n");
break;
}
if (FD_ISSET(data_pipe[0], &fds_read)) {
printf("child: read fd set. reading.\n");
if ((retval = read(data_pipe[0], data, sizeof(data))) < 0) {
perror("read");
fprintf(stderr, "child: couldn't read from pipe\n");
exit(-1);
}
if(retval == 0) {
fprintf(stderr, "child: eof on pipe\n");
break;
}
data[retval] = '\0';
printf("pid %d Pipe reads (%d): %s\n", getpid(), retval, data);
} else printf("child: no fd set\n");
}
/* probably pipe was broken, or got EOF via the pipe. */
exit(0);
}
void do_parent(int data_pipe[])
{
char data[1024];
int retval;
fd_set fds_write;
signal(SIGPIPE, pipehandler);
signal(SIGUSR1, pipehandler);
/* first, close the read part of pipe, since it is not needed */
close(data_pipe[0]);
/* now enter a loop of read user input, and writing it to the pipe */
while (1) {
FD_ZERO(&fds_write);
FD_SET(data_pipe[1], &fds_write);
printf("pid %d Waiting for pipe ready to write...\n", getpid());
retval = select(data_pipe[1]+1, NULL, &fds_write, NULL, NULL);
if (retval == -1) {
perror("select");
fprintf(stderr, "Parent: Error in select\n");
exit(-1);
}
printf("Input data: ");
if(!gets(data)) {
printf("parent: eof; exiting\n");
break;
}
if (!strcmp(data, "exit"))
break;
if (!FD_ISSET(data_pipe[1], &fds_write)) {
fprintf(stderr, "parent: write fd not set?! retrying\n");
continue;
}
retval = write(data_pipe[1], &data, 1024);
if (retval == -1) {
perror("write");
fprintf(stderr, "Error writing on pipe\n");
exit(-1);
}
}
/* got exit from user */
close(data_pipe[1]); /* close pipe, let child know we're done */
wait(&retval);
printf("Child exited with status: %d\n", retval);
exit(0);
}
void main(void) {
int pipes[2];
int retval;
int pid;
/* create the pipe */
retval = pipe(pipes);
if (retval == -1) {
perror("pipe");
fprintf(stderr, "Error creating the pipe\n");
exit(-1);
}
pid = fork();
if (pid == -1) {
fprintf(stderr, "Error forking\n");
exit(-1);
}
if (pid == 0) /* child proc */
do_child(pipes);
else
do_parent(pipes);
}