-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
executable file
·318 lines (272 loc) · 8.55 KB
/
pipe.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* file sig_ex3.c: This is a more complex example of signals
Author: Ramesh Yerraballi
Attempt to mimic:
prompt>> top | grep firefox
The parent creates a pipe and two child processes with the
write end of the pipe serving as the stdout for top and
the read end serving as the stdin for grep.
The first child that exec's top creates a new session with itself a member leader
of the process group in it. The process group's id is same as the child's pid (pid_ch1).
The second child that exec's grep joins the process group that the first child created
Now when a Ctr-c is pressed the parent relays a SIGINT to both children using
kill(-pid_ch1,SIGINT); alternative you could call killpg(pid_ch1,SIGINT);
The two child processes receive the SIGINT and their default behavior is to terminate.
Once they do that the parent reaps their exit status (using wait), prints and exits.
When a Ctrl-z is pressed the the parent relays a SIGTSTP to both children using
kill(-pid_ch1,SIGTSTP);
The parent's waitpid() call unblocks when the child receives the STOP signal. The parent
waits for 4 secs and resumes the the child that STOPped. This happens once for each of
the two children.
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int pipefd[2];
int status, pid_ch1, pid_ch2, pid;
static void sig_int(int signo) {
printf("Sending signals to group:%d\n",pid_ch1); // group id is pid of first in pipeline
kill(-pid_ch1,SIGINT);
}
static void sig_tstp(int signo) {
printf("Sending SIGTSTP to group:%d\n",pid_ch1); // group id is pid of first in pipeline
kill(-pid_ch1,SIGTSTP);
}
int main(void) {
char ch[1]={0};
if (pipe(pipefd) == -1) {
perror("pipe");
exit(-1);
}
pid_ch1 = fork();
if (pid_ch1 > 0){
printf("Child1 pid = %d\n",pid_ch1);
// Parent
pid_ch2 = fork();
if (pid_ch2 > 0){
printf("Child2 pid = %d\n",pid_ch2);
if (signal(SIGINT, sig_int) == SIG_ERR)
printf("signal(SIGINT) error");
if (signal(SIGTSTP, sig_tstp) == SIG_ERR)
printf("signal(SIGTSTP) error");
close(pipefd[0]); //close the pipe in the parent
close(pipefd[1]);
int count = 0;
while (count < 2) {
// Parent's wait processing is based on the sig_ex4.c
pid = waitpid(-1, &status, WUNTRACED | WCONTINUED);
// wait does not take options:
// waitpid(-1,&status,0) is same as wait(&status)
// with no options waitpid wait only for terminated child processes
// with options we can specify what other changes in the child's status
// we can respond to. Here we are saying we want to also know if the child
// has been stopped (WUNTRACED) or continued (WCONTINUED)
if (pid == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("child %d exited, status=%d\n", pid, WEXITSTATUS(status));count++;
} else if (WIFSIGNALED(status)) {
printf("child %d killed by signal %d\n", pid, WTERMSIG(status));count++;
} else if (WIFSTOPPED(status)) {
printf("%d stopped by signal %d\n", pid,WSTOPSIG(status));
printf("Sending CONT to %d\n", pid);
sleep(4); //sleep for 4 seconds before sending CONT
kill(pid,SIGCONT);
} else if (WIFCONTINUED(status)) {
printf("Continuing %d\n",pid);
}
}
exit(1);
}else {
//Child 2
sleep(1);
setpgid(0,pid_ch1); //child2 joins the group whose group id is same as child1's pid
close(pipefd[1]); // close the write end
dup2(pipefd[0],STDIN_FILENO);
char *myargs[3];
myargs[0] = strdup("grep"); // program: "grep" (word count)
myargs[1] = strdup("firefox"); // argument: "firefox"
myargs[2] = NULL; // marks end of array
execvp(myargs[0], myargs); // runs word count
}
} else {
// Child 1
setsid(); // child 1 creates a new session and a new group and becomes leader -
// group id is same as his pid: pid_ch1
close(pipefd[0]); // close the read end
dup2(pipefd[1],STDOUT_FILENO);
char *myargs[2];
myargs[0] = strdup("top"); // program: "top" (writes to stdout which is now pipe)
myargs[1] = NULL;
execvp(myargs[0], myargs); // runs top
}
}
/*
char * reformCommand(char **args)
{
int i;
char reformedCommand[BUFSIZ];
for (i = 0; args[i] != '\0'; i++)
{
int j;
for (j = 0; args[i][j] != '\0'; j++)
{
append(reformedCommand, args[i][j]);
}
if (args[i + 1] != '\0')
{
append(reformedCommand, ' ');
}
}
return reformedCommand;
}
*/
/*
int redirectInput()
{
int in, out;
char *grep_args[] = {"grep", "Villanova", NULL};
// open input and output files
in = open("scores", O_RDONLY);
out = open("out", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
// replace standard input with input file
dup2(in, 0);
// replace standard output with output file
dup2(out, 1);
// close unused file descriptors
close(in);
close(out);
// execute grep
execvp("grep", grep_args);
}
*/
/*
if (outputRedirectIndex != -1)
{
char *commandArgs[25];
buildArgs(commands, commandArgs, (outputRedirectIndex - 1), ISDEBUG);
redirectOutput(commandArgs, commands[outputRedirectIndex + 1][0], ISDEBUG);
}
*/
//check for input redirect
/*
int inputRedirectIndex = containsCommand(commands, "<");
if (inputRedirectIndex != -1)
{
char *commandArgs[25];
buildArgs(commands, commandArgs, (inputRedirectIndex - 1), ISDEBUG);
redirectOutput(commandArgs, commands[inputRedirectIndex + 1][0], ISDEBUG);
}
*/
/*
int i;
for (i = 0; commands[i]; i++)
{
if (ISDEBUG){printf("DEBUG: Executing Command: '%s'\n", commands[i][0]);}
char *commandArgs[25];
buildArgs(commands, commandArgs, i, ISDEBUG);
profFunction();
*/
/*
pid_ch1 = fork();
setsid();
if (pid_ch1 == 0)
{
if (ISDEBUG) {printf("GOT HERE!\n");}
}
else if (pid_ch1 > 0)
{
//build args
buildArgs(commands, commandArgs, i, ISDEBUG);
//file output redirection
if (outputRedirectIndex != -1)
{
int out = open(commands[outputRedirectIndex + 1][0], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
dup2(out, 1);
execvp(commands[i][0], commandArgs);
i += outputRedirectIndex;
}
else
{
//regular execute
execvp(commands[i][0], commandArgs);
}
}
if (ISDEBUG){printf("DEBUG: Success!\n");}
*/
/*
}
*/
/*
void buildArgs(char *** commands, char ** commandArgs, int pos, bool ISDEBUG)
{
int j;
for (j = 0; j < 25; j++)
{
if (commands[pos][j] != '\0')
{
commandArgs[j] = strdup(commands[pos][j]);
}
else
{
commandArgs[j] = '\0';
}
}
if (ISDEBUG)
{
printf("With args: '");
for (j = 0; commandArgs[j] != '\0'; j++)
{
printf("%s ", commandArgs[j]);
}
printf("'\n");
}
}
*/
/*
char *userInput = malloc(250 * sizeof(char));
processJobTable[jobIndex].command;
int i;
bool containsBg = false;
for (i = 0; userInput[i] != '\0'; i++)
{
if (userInput[i] == '&')
{
containsBg = true;
}
}
if (containsBg)
{
waitpid(getCurrentProcessId(), &status, WNOHANG);
}
else
{
waitpid(getCurrentProcessId(), &status, WUNTRACED);
}
*/
//processJobTable[jobIndex].ground = '+';
//tcsetpgrp(STDOUT_FILENO, jobPid);
//tcsetpgrp(STDIN_FILENO, jobPid);
/*
pid_ch1 = fork();
if (pid_ch1 > 0)
{
setupSignals(ISDEBUG);
waitForSignals(-1, -1);
}
else
{
//RESET Signals to default. Just doing this to be 100% sure...
if (signal(SIGINT, SIG_DFL) == SIG_ERR)
if (ISDEBUG){printf("signal(SIGINT) error");}
if (signal(SIGTSTP, SIG_DFL) == SIG_ERR)
if (ISDEBUG){printf("signal(SIGTSTP) error");}
kill(jobPid, SIGCONT);
//pgid = setsid();
//execvp(commands[0][0], commands[0]);
}